How to check in node.js if variable is JSON object?


Question

I have variable for example

var x = "this is X value";

How to check in node.js if variable is JSON object ?

1
13
9/4/2012 6:45:31 PM

Accepted Answer

Your question is not clear, but assuming you meant to check if a variable has an unparsed JSON string:

try {
    JSON.parse(x);
} catch (e) {
    console.log("not JSON");
}
40
9/4/2012 6:39:48 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon