How to Access JSON Object in JavaScript 2018

By | April 7, 2018

Example How to Access JSON Object in JavaScript – Below is a JSON string.

JSON String
{
    "name": "tiket",
    "age": 30,
    "address": {
        "streetAddress": "88 8nd Street",
        "city": "New York"
    },
    "phoneNumber": [
        {
            "type": "home",
            "number": "111 111-1111"
        },
        {
            "type": "fax",
            "number": "222 222-2222"
        }
    ]
}

 

To access the JSON object in JavaScript, parse it with JSON.parse(), and access it via “.” or “[]”.

JavaScript
<script> var data = '{"name": "tiket","age": 30,"address": {"streetAddress": "88 8nd Street","city": "New York"},"phoneNumber": [{"type": "home","number": "111 111-1111"},{"type": "fax","number": "222 222-2222"}]}'; var json = JSON.parse(data); alert(json["name"]); //tiket alert(json.name); //tiket alert(json.address.streetAddress); //88 8nd Street alert(json["address"].city); //New York alert(json.phoneNumber[0].number); //111 111-1111 alert(json.phoneNumber[1].type); //fax alert(json.phoneNumber.number); //undefined </script> 
We also have tutorial for convert java object to/from json with Jakson 2.x in this article.

Reference for JSON

  1. Wikipedia : JSON

One thought on “How to Access JSON Object in JavaScript 2018

  1. Pingback: How to Create JSON string in JavaScript ~ Cimblo.com

Leave a Reply

Your email address will not be published. Required fields are marked *