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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.