How to Access JSON Object in JavaScript

By | April 7, 2018

Example How to Access JSON Object in JavaScript – Below is a sample for 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"
        }
    ]
}

How to Access JSON Object in JavaScript

How to Access JSON Object in JavaScript

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.
If you need more reference for JSON, Javascript or Java 8, 9, 10, 11, 12 you can go to the link below. All basic info about javascript, JSON and Java you can found and learn for free on wikipedia site! trust me :).

Reference for JSON

  1. Wikipedia : JSON
  2. Wikipedia : Basic Javascript
  3. Java StackOverflow

One thought on “How to Access JSON Object in JavaScript

  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 *