JavaScript | Check if a string is a valid JSON string
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:
- JavaScript | Check if a string is a valid hex color representation
- How to pretty print JSON string in JavaScript ?
- JavaScript | Check if a key exists inside a JSON object
- JavaScript | Check if a variable is a string
- How to check if string contains only digits in JavaScript ?
- How to check a string is entirely made up of the same substring in JavaScript ?
- How to check empty/undefined/null string in JavaScript?
- JavaScript | Difference between String.slice and String.substring
- How to count string occurrence in string using JavaScript?
- JavaScript | Insert a string at position X of another string
- PHP | Program to check a string is a rotation of another string
- JavaScript JSON
- Javascript | JSON PHP
- JavaScript | JSON HTML
- JavaScript | JSON Arrays
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.



