Validate JSON from Mongo? Validate JSON from Mongo? mongodb mongodb

Validate JSON from Mongo?


you could replace the naked function calls with strings, something like this

function IsJsonLikeString(str) {  str = str.replace(/(\w+)\("([^"]+)"\)/g, '"$1(\"$2\")"');  try {    JSON.parse(str);  } ...

explanation from https://regex101.com/r/fW7iH4/#javascript :

/(\w+)\("([^"]+)"\)/g    1st Capturing group (\w+)        \w+ match any word character [a-zA-Z0-9_]            Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]    \( matches the character ( literally    " matches the characters " literally    2nd Capturing group ([^"]+)        [^"]+ match a single character not present in the list below            Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]            " a single character in the list " literally (case sensitive)    " matches the characters " literally    \) matches the character ) literally    g modifier: global. All matches (don't return on first match)


The issue you'll have isn't one of JSON validation, it's related to whether the database actually ACCEPTS the input data. You've got the right idea to check if the syntax is correct, but you then have to run the data through the mongo collection and check whether there are any errors.

Check out MongoDB db.collection.explain() to verify whether it is a valid Mongo query