How to JSON Parse in Javascript?

Nov 01, 2022 . Admin



Hello friends,

In this tutorial, I will show you Javascript JSON parses examples. you can understand the concept of how to receive data from the web server using JSON.parse(). step by step explain how to use the javascript function JSON. parse() to convert text into a javascript object. I would like to show you how to use the javascript object on your page

A common use of JSON is to exchange data to/from a web server, when receiving data from a web server, the data is always a string, parse the data with JSON.parse(), and the data becomes a JavaScript object, date objects are not allowed in JSON if you need to include a date, write it as a string.

So, let's see bellow solution:

Example: 1
index.html
<!DOCTYPE html>
<html>
<head>
    <title>Javascript JSON Parse Example</title>
</head>
<body>

    <h1>Javascript JSON Parse Example</h1>
    
    <p>Uppercase the value of "city":</p>

    <p id="demo"></p>

    <script>
        var text = '{ "name":"John", "age":"39", "city":"New York"}';
        var obj = JSON.parse(text, function (key, value) {
            if (key == "city") {
                return value.toUpperCase();
            } else {
                return value;
            }
        });
        document.getElementById("demo").innerHTML = obj.name + ", " + obj.city; 
    </script>

</body>
</html>
Output:

i hope it will help you...

#Javascript