0

I'm working on an app where a function needs to validate if a provided string parameter is valid json. But the function also needs to handle JSON which has been copied from jira. For example, the json could look like this:

"{
    "query": {
        "size": 0,
        "aggregations": {
            "ipTag": {
                "terms": {
                    "field": "ipTag",
                    "size": 1001
                }
            }
        }
    },
    "table": "ip_info_table"
}"

Not sure if the following format of the string param is becuase it was copied/pasted from Jira, or if it has to do with MUI Input controls. But anyway, this is what the format looks like when I copy/paste the variable into a CDT watch:

"{\n    \"query\": {\n        \"size\": 0,\n ...."
  • JSON.parse(param) throws error
  • JSON.parse(JSON.stringify(param)) return success
  • But JSON.parse(JSON.stringify('junk')) also returns success although a single word of junk is not considered valid json

So what kind of code routine would you recommend to handle this scenario? Is there some logic that I can use to format the sample input parameter value above, so that JSON.parse() will properly validate the param value as JSON?

react0r
  • 95
  • 7
  • 2
    Are the leading and trailing double quotes part of the string? A single string or number is valid JSON, according to [RFC 7158](https://datatracker.ietf.org/doc/html/rfc7158). `"junk"` is valid JSON. – jabaa May 05 '22 at 21:34
  • no i think that the outer quotes are simply added by the IDE in the variable hover panel – react0r May 05 '22 at 21:38
  • 2
    I would use `JSON.parse` to validate JSON: `try { JSON.parse(str); console.log('valid'); } catch { console.log('invalid'); }`. I saw a similar answer that was downvoted and deleted. I don't know why. I was going to upvote it. – jabaa May 05 '22 at 21:44
  • Does this answer your question? [How to check if a string is a valid JSON string?](https://stackoverflow.com/questions/3710204/how-to-check-if-a-string-is-a-valid-json-string) – jabaa May 18 '22 at 10:11

1 Answers1

2

I would use JSON.parse to validate JSON:

let valid = false;
try {
  JSON.parse(str);
  console.log('valid');
  valid = true;
} catch {
  console.log('invalid');
}
console.log(valid);

Examples:

const json = `{
    "query": {
        "size": 0,
        "aggregations": {
            "ipTag": {
                "terms": {
                    "field": "ipTag",
                    "size": 1001
                }
            }
        }
    },
    "table": "ip_info_table"
}`;

try {
    JSON.parse(json);
    console.log('valid');
} catch {
    console.log('invalid');
}

const json = "{\n    \"query\": {\n        \"size\": 0\n} }";

try {
    JSON.parse(json);
    console.log('valid');
} catch {
    console.log('invalid');
}

const json = `{
    "query": {
        "size": 0,
        "aggregations": {
            "ipTag": {
                "terms": {
                    "field": "ipTag",
                    "size": 1001
                }
            }
        }
    },
    "table": "ip_info_table",
}`;

try {
    JSON.parse(json);
    console.log('valid');
} catch {
    console.log('invalid');
}
jabaa
  • 5,844
  • 3
  • 9
  • 30