1

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.

Mehul
  • 148
  • 9
  • 1
    JSON is a string, a JSON object is just a JavaScript object, semantics over. How would you pass an invalid object as you can't _create_ an invalid object? – George Apr 25 '19 at 09:28
  • [There's no such thing as a "JSON object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/) (@George) – Andreas Apr 25 '19 at 09:28
  • @Andreas Did you read my comment? _"JSON is a string, a JSON object is just a JavaScript object"_ – George Apr 25 '19 at 09:29
  • this should help: https://stackoverflow.com/questions/3710204/how-to-check-if-a-string-is-a-valid-json-string-in-javascript-without-using-try – pbialy Apr 25 '19 at 09:29
  • @George I agree. However, if you look at the second last line which is commented out, that is not a valid JSON. In cases like that I want to throw an error. – Mehul Apr 25 '19 at 09:36
  • 1
    @Mehul But that's not valid JavaScript either... – George Apr 25 '19 at 09:38
  • @George So what invalid input can I provide to test it out? – Mehul Apr 25 '19 at 09:43
  • Please provide a code snippet that runs in MarkLogic QueryConsole as is, showing the problem. – grtjn Apr 25 '19 at 10:23
  • @grtjn The code that I have posted above runs on valid JSON but on invalid it throws JS error on the input. This is the qconsole code that I run: function isValidJson(json) { //var jsonStr = json.toString(); try { var x = JSON.parse(xdmp.toJSON(json)); return x; } catch(e) { fn.error(xs.QName("ERROR"), "Not a valid JSON") } }; var json = {"test":"data"}; isValidJson(json); – Mehul Apr 25 '19 at 11:00

1 Answers1

0

Not sure I fully get what you are after, but a JSON 'object' is already parsed, and cannot be invalid. You have to check validity when it is still a string. Something like:

function isValidJson(json) {
  try {  
    return JSON.parse(json);  //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);

HTH!

grtjn
  • 20,254
  • 1
  • 24
  • 35