1

I am failing a JSON data validation test, where I am supposed to create a JSON object persons with with properties Name, EmployeeID, Experience, Company and Designation and access it using loop. I am just learning JSON and I think the problem is that it requires knowledge of nodejs too here is the json file (data.json)

'{"Name":"someName","EmployeeID":123,"Experience":123,"Company":"somecompany","Designation":"someDesignation"}'

Here is the js file:

let jsonData = require('./data.json');
let persons=JSON.parse('./data.json', jsonData);
for(i in persons){
    console.log(persons.i);
}

and here is the validation file:

const Joi = require('joi');
const fss =require('fs');

const schema = Joi.object().keys({
    Name: Joi.string().required(),
    EmployeeID: Joi.number().required(),
    Experience: Joi.number().required(),
    Company: Joi.string().required(),
    Designation: Joi.string().required()
});

const personSchema=Joi.object().keys({
  persons:schema.required()
}).required();

var data;

try{
 data = require("./data.json");    
}catch(e)
{
 data={};
}

var XMLWriter = require('xml-writer');
    xw = new XMLWriter;




// You can also pass a callback which will be called synchronously with the validation result.
Joi.validate(data, personSchema, function (err, value) {
if(err==null)
{   
  console.log("JSON data is valid, Status: Passed");
}else{
    console.log("JSON data is invalid. Status: failed")
}

});

I am getting JSON data is invalid. Status: failed

11
  • 2
    Lose the surrounding '
    – Mitya
    Commented Oct 12, 2019 at 14:27
  • 1
    Employee ID with a space?
    – taygetos
    Commented Oct 12, 2019 at 14:29
  • absolutely not brother @taygetos , this I wrong typed here
    – bali
    Commented Oct 12, 2019 at 14:37
  • 2
    If, as you say, your JSON lives in and is being loaded form its own file, then the wrapping ' will absolutely be a problem. It may not be your only problem, however.
    – Mitya
    Commented Oct 12, 2019 at 14:41
  • 1
    From your example your EmployeeId and Experience are strings but you are validating for numbers.
    – Jan Swart
    Commented Oct 12, 2019 at 14:42

1 Answer 1

2

From the description of what you need to create, it seems you need an Array of those objects

So, the JSON should be

[{"Name":"someName","EmployeeID":123,"Experience":123,"Company":"somecompany","Designation":"someDesignation"}]

Then the "JS" would be

let persons=require('./data.json');
for(let i in persons){
    console.log(persons[i]);
}

And the validator would be

const Joi = require('joi');
const fss = require('fs');

const schema = Joi.object().keys({
        Name: Joi.string().required(),
        EmployeeID: Joi.number().required(),
        Experience: Joi.number().required(),
        Company: Joi.string().required(),
        Designation: Joi.string().required()
    });

const personSchema = Joi.array().items(schema.required()).required();

var data;

try {
    data = require("./data.json");
} catch (e) {
    data = [];
}

var XMLWriter = require('xml-writer');
xw = new XMLWriter;

// You can also pass a callback which will be called synchronously with the validation result.
Joi.validate(data, personSchema, function (err, value) {
    if (err == null) {
        console.log("JSON data is valid, Status: Passed");
    } else {
        console.log(err, "JSON data is invalid. Status: failed")
    }

});

if the validator file should be left untouched, then the JSON needs to be as follows

{"persons":{"Name":"someName","EmployeeID":123,"Experience":123,"Company":"somecompany","Designation":"someDesignation"}}
4
  • Here is the error @Bravo ``` [ { message: '"value" does not contain 1 required value(s)', path: [], type: 'array.includesRequiredUnknowns', context: [Object] } ], _object: [], annotate: [Function] } 'JSON data is invalid. Status: failed' ``` btw I wanna pass this hands-on by fair means only so if there is something that you can suggest except verify.js then thanks in advance
    – bali
    Commented Oct 12, 2019 at 15:45
  • there is no error - this passes @Krishnakant ... and ``` is not an error, it's a bunch of backticks
    – Bravo
    Commented Oct 12, 2019 at 15:45
  • yes this time your JSON is correct, but one thing still I don't get that when I tried your JSON as you wrote it didn't worked but when i tried it as "{\"persons\":{\"Name\":\"someName\",\"EmployeeID\":123,\"Experience\":123,\"Company\":\"somecompany\",\"Designation\":\"someDesignation\"}}" it worked, what is difference between these two data
    – bali
    Commented Oct 12, 2019 at 18:03
  • huh? you need \ in the .json file?
    – Bravo
    Commented Oct 13, 2019 at 0:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.