I'm currently working on validating JSON input from users and came across a challenge. I've found a way to check if the text a user enters is valid JSON using a simple function, like below:
function IsJsonString(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}
However, my issue lies with JSON data coming from Mongo, which often includes objects like ObjectId
and ISODate
, making it look like this:
{
"_id" : ObjectId("5733b42c66beadec3cbcb9a4"),
"date" : ISODate("2016-05-11T22:37:32.341Z"),
"name" : "KJ"
}
As this format isn't considered valid JSON, I'm trying to figure out how to validate JSON while still accommodating such structures. Any ideas or suggestions would be greatly appreciated!