In order to check the validity of a string whether it is a JSON string or not, We’re using the JSON.parse()method with few variations.
JSON.parse()
This method parses a JSON string, constructs the JavaScript value or object specified by the string. A reviver function may be provided to do a change on the resulting object before it’s returned.
Syntax:
JSON.parse(text[, revr])
parameters:
- text: This parameter is required. It specifies the string to be parsed as JSON.
- revr: This parameter is optional. It specifies how the original value produced by the function is transformed before it is returned.
Return Value: Object corresponding to the provided JSON text.
Example-1: This example checks the validity of JSON string by JSON.parse() method by creating a function.
<!DOCTYPE html> <html> <head> <title> JavaScript | Check if a string is a valid JSON string. </title> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="color:green;"> </p> <button onclick="gfg_Run()"> check </button> <p id="GFG_DOWN" style="color:green; font-size: 20px;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var string = '{ "prop_1": "val_1", "prop_2": "val_2" }'; el_up.innerHTML = string; function testJSON(text) { if (typeof text !== "string") { return false; } try { JSON.parse(text); return true; } catch (error) { return false; } } function gfg_Run() { el_down.innerHTML = testJSON(string); } </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Example-2: This example is returning false because the property value val_1 is not a string.
<!DOCTYPE html> <html> <head> <title> JavaScript | Check if a string is a valid JSON string. </title> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="color:green;"> </p> <button onclick="gfg_Run()"> check </button> <p id="GFG_DOWN" style="color:green; font-size: 20px;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var string = '{ "prop_1": val_1, "prop_2": "val_2" }'; el_up.innerHTML = string; function isJSON(str) { try { return (JSON.parse(str) && !!str); } catch (e) { return false; } } function gfg_Run() { el_down.innerHTML = isJSON(string); } </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Example-3: This example is returning true by using the JSON.parse() method alongwith typechecking of the text.
<!DOCTYPE html> <html> <head> <title> JavaScript | Check if a string is a valid JSON string. </title> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="color:green;"> </p> <button onclick="gfg_Run()"> check </button> <p id="GFG_DOWN" style="color:green; font-size: 20px;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var string = '{ "prop_1": 1, "prop_2": "val_2" }'; el_up.innerHTML = string; function isJSON(str) { try { return (JSON.parse(str) && !!str); } catch (e) { return false; } } function gfg_Run() { el_down.innerHTML = isJSON(string); } </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- How to convert JSON string to array of JSON objects using JavaScript ?
- JSON | modify an array value of a JSON object
- JavaScript | Check if a string is a valid hex color representation
- JavaScript | Check if a key exists inside a JSON object
- How to check a date is valid or not using JavaScript ?
- How to pretty print JSON string in JavaScript ?
- How to Convert JS Object to JSON String in JQuery/Javascript?
- Javascript | JSON PHP
- JavaScript JSON
- Converting JSON text to JavaScript Object
- JavaScript JSON stringify() Method
- JavaScript | JSON HTML
- JavaScript | JSON Arrays
- Deserializing a JSON into a JavaScript object
- JavaScript JSON parse() Method
- JavaScript | Remove a JSON attribute
- JavaScript | Convert an array to JSON
- JavaScript | Add new attribute to JSON object
- JavaScript | How to add an element to a JSON object?
- How to convert PHP array to JavaScript or JSON ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


