JavaScript

How to Iterate loop through JSON array using JavaScript

β€œJSON Array” contains an ordered list of items enclosed in the square brackets and separated by a β€œcomma(,)”. The list of items can be of multiple data types including β€œobject”, β€œnumber”, β€œstring”, β€œboolean”, as well as another array inside the JSON array. The β€œJSON array” is basically the same as the JavaScript array. Also, it can be iterated as a loop like the JavaScript array. The iteration process of the JSON array is helpful to access each element that contains the JSON object to perform special tasks on them.

This guide will explain how to iterate a loop through a JSON array using JavaScript.

How to Iterate a Loop through a JSON Array Using JavaScript?

The β€œJSON array” values can be accessed via a loop. In-looping the JSON (JavaScript Object Notation) is considered the best technique to transport data in an array format. This is because it is a light format to store and transfer the required data from one place to another.

This section uses the commonly used β€œfor” loop to iterate a JSON array using JavaScript.

Syntax (JSON Array)

Array-name= [value1, value2,.....valueN]

Here, β€œvalue1”, β€œvalue2”, and β€œvalueN” refer to the values that need to be iterated.

Let’s perform the looping through a β€œJSON” array in JavaScript practically.

HTML Code

Let’s have a look at the following HTML code:

<h2>Looping through JSON Array</h2>
<p>A JSON array has zero, one, or more ordered elements, separated by a comma.</p>
<p id="sample"></p>

In the above lines of code:

  • The β€œ<h2> tag defines a subheading.
  • The β€œ<p>” tag creates a paragraph statement.
  • Lastly, the β€œ<p>” tag defines an empty paragraph having an id β€œsample” to display the JSON array values.

JavaScript Code

Next, move on to the below-provided code:

<script>
const JSONarray = '{"name":"Johnson", "age":35, "cars":["BMW", "Honda", "Corolla"]}';
const Obj = JSON.parse(JSONarray);

let text = "";
for (let k = 0; k < Obj.cars.length; k++) {
text += Obj.cars[k] + ", ";
}
document.getElementById("sample").innerHTML = text;
</script>

In this code block:

  • Define a JSON array named β€œJSONarray” with a β€œconst” keyword having an ordered list of values.
  • The β€œObj” object utilizes the β€œparse()” method that converts the specified JSON array text into the JavaScript object.
  • After that, the β€œtext” variable stores an empty value.
  • Next, apply a β€œfor” loop to iterate over the properties of β€œObj” concatenated with the included JSON array against the key β€œcar”.
  • Also, associate the β€œlength” property and increment the loop to carry out the iteration appropriately.
  • Lastly, apply the β€œgetElementById()” method to access the added empty paragraph using its id β€œsample”. It will display the JSON array values through the β€œinnerHTML” property.

Output

The output shows all the values of the added JSON array using the β€œfor” loop.

Conclusion

β€œJSON arrays” can be easily iterated using JavaScript with the help of the β€œfor” loop. This is a common process and is generally used in web development to retrieve the data in JSON format from the database or the API. This guide has explained a brief description to iterate a loop through a JSON array using JavaScript.

About the author

Talha Saif Malik

Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.