Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
134
rated 0 times [  135] [ 1]  / answers: 1 / hits: 37668  / 13 Years ago, thu, december 8, 2011, 12:00:00

I am using JSON object as an input in the textfield. Is there any way to validate this JSON object in JavaScript??


More From » json

 Answers
19

Building on the idea of @Quentin, you can just do something like:



function isValidJson(json) {
try {
JSON.parse(json);
return true;
} catch (e) {
return false;
}
}

console.log(isValidJson({})); // true
console.log(isValidJson(abc)); // false


This will require json2.js to be deployed in the page in order to ensure cross-browser support for the JSON Object.


[#88673] Wednesday, December 7, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kaitlynnb

Total Points: 402
Total Questions: 96
Total Answers: 109

Location: Trinidad and Tobago
Member since Fri, May 8, 2020
4 Years ago
kaitlynnb questions
;