In Marklogic's query console I need to write a JavaScript function where I can validate the object as a valid JSON with true/false output. How do I check it directly without using Strings?
I have tried to take the input and converted to string and then do a JSON.parse but the whole code is not right.
function isValidJson(json)
{
//var obj = JSON.stringify(json);
try
{
var x = JSON.parse(xdmp.toJSON(json));
return x; //returns the JSON object if true
}
catch(e)
{
fn.error(xs.QName("ERROR"), "Not a valid JSON")
}
}
var json = {"test":"data"};
//var json = {"abc"};
isValidJson(json);
I want to pass the value as a JSON object and not a JSON string. When it is passed, the function should return whether it is a valid JSON or not.
I can get the positive test case done but not negative. Instead I get compilation error.