Open In App

JQuery | parseJSON() method

Last Updated : 11 Jul, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

This parseJSON() method in jQuery takes a well-formed JSON string and returns the resulting JavaScript value.

Syntax:

jQuery.parseJSON( json )

Parameters: The parseXML() method accepts only one parameter that is mentioned above and described below:

  • json: This parameter is the well-formed JSON string to be parsed.

Return Value:

  • It returns the following:
  • String
  • Number
  • Object
  • Array
  • Boolean

Example 1: In this example, we are using the above-explained approach.

html
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>JQuery | parseJSON() method</title>
    <script src=
"https://code.jquery.com/jquery-3.4.1.js">

    </script>
    <style>
        #a {
            color: blue;
        }

        #b {
            color: red;
        }

        h1 {
            color: green;
        }

        body {
            text-align: center;
        }
    </style>
</head>

<body>
    <h1>
        GeeksForGeeks
    </h1>

    <h3>JQuery | parseJSON() method</h3>
    <b id="a"></b>

    <script>
        let txt = '{"name":"John", "age":30, "city":"New York"}'
        let obj = jQuery.parseJSON(txt);
        document.getElementById("a").innerHTML =
            "Object Name : " + obj.name +
            "<br> Object Age : " + obj.age;
    </script>
</body>

</html>

Output:

Example 2: In this example, we are using the above-explained approach.

html
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>JQuery | parseJSON() method</title>
    <script src=
"https://code.jquery.com/jquery-3.4.1.js">
    </script>

</head>

<body style="text-align:center;">

    <h1 style="color: green">
        GeeksForGeeks
    </h1>

    <h3>JQuery | parseJSON() method</h3>
    <button onclick="gfg()">
        Click
    </button>

    <script>
        function gfg() {
            let txt = '{"name":"Shubham Singh", "age":21, "Cars":"ABC" }'
            let obj = jQuery.parseJSON(txt);
            alert(obj.name === "ABC");
        }
    </script>
</body>

</html>

Output:

jquery-52


Next Article

Similar Reads