Skip to content

JavaScript read JSON Array | Example code

  • by

You can read JSON arrays the same way read normal objects in JavaScript via dot notation. This will work on only JSON Objects.

var json = {"STOCKQTY":20,"PHYSICALQTY":10};

console.log(json.PHYSICALQTY);

If you Have saved this json under another object You need to go deeper, eg.

var response = {AlreadySaved: [{"STOCKQTY":20,"PHYSICALQTY":10}] };

console.log(response.AlreadySaved[0].PHYSICALQTY);

have json (eg from the response) as string not object.

console.log(typeof(someJson)); //string

In that case, You need to parse this string into json using the JSON parse method.

var json = JSON.parse(someJsonAsString);

Source: stackoverflow.com

Example read JSON Array in JavaScript

Simple example code. You can parse this and then get the value. Of course, if you receive this like a string.

<!DOCTYPE html>
<html>
<body>

  <script>
    var json = [{
      "SNO": 1,
      "SNUMBER": "827",
      "STARTDATE": "11/12/2016",
      "STARTTIME": "03:06:50 PM",
      "ITEMNAME": "KBand",
      "ITEMCODE": "KB2541",
      "ITEMSKUNAME": "Band",
      "ITEMSKUCODE": "BT102",
      "SALESRATE": 100.0,
      "PURCHASERATE": 5.0,
      "DOE": "~",
      "STOCKQTY": 2.0,
      "PHYSICALQTY": 1.0
    }];


    console.log(typeof(json));
    console.log(json[0].ITEMNAME);

    console.log(json[0]);
  </script>

</body>
</html>

Output:

JavaScript read JSON Array

Do comment if you have any doubts or suggestions on the JS JSON Array topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

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