I want to control whether given data is in JSON format.
For this control, I use JSON.parse
with try catch. As;
var str= {
"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]
}
try {
parsed = JSON.parse(str);
} catch (err) {
return "It is not valid JSON file";
}
But I want to stretch the rules. I want to accept the following file format where fields does not have to be written inside "" as;
{
employees:[
{firstName:"John", lastName:"Doe"},
{firstName:"Anna", lastName:"Smith"},
{firstName:"Peter", lastName:"Jones"}
]
}
How can I do this? JSON.parse
does not accept this format.