The JSON Arrays is similar to JavaScript Arrays.
Syntax of Arrays in JSON Objects:
// JSON Arrays Syntax
{
"name":"Peter parker",
"heroName": "Spiderman",
"friends" : ["Deadpool", "Hulk", "Wolverine"]
}
Accessing Array Values:
The Array values can be accessed using the index of each element in an Array.
<!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <p id="paraId"></p> <script> var spidermanDetail = { "name": "Peter parker", "heroName": "Spiderman", "friends": ["Deadpool", "Hulk", "Wolverine"] }; var x = spidermanDetail.friends[0]; document.getElementById("paraId").innerHTML = x; </script> </body> </html> |
Output:
Deadpool
Looping over an Array:
The for-in loop can be used for iterating through Array.
<!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <p id="paraId"></p> <script> var str = ""; var spidermanDetail = { "name": "Peter parker", "heroName": "Spiderman", "friends": ["Deadpool", "Hulk", "Wolverine"] }; for (i in spidermanDetail.friends) { str += spidermanDetail.friends[i] + "<br/>"; } document.getElementById("paraId").innerHTML = str; </script> </body> </html> |
Output:
Deadpool Hulk Wolverine
Modifing an Array Values:
An index number can be used for modification of values.
<!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <p id="paraId"></p> <script> var str = ""; var spidermanDetail = { "name": "Peter parker", "heroName": "Spiderman", "friends": ["Deadpool", "Hulk", "Wolverine"] }; spidermanDetail.friends[1] = "Iron Man"; for (i in spidermanDetail.friends) { str += spidermanDetail.friends[i] + "<br/>"; } document.getElementById("paraId").innerHTML = str; </script> </body> </html> |
Output:
Deadpool Iron Man Wolverine
Deleting Array Values:
The values of an Array can be deleted using delete keyword.
<!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <p id="paraId"></p> <script> var str = ""; var spidermanDetail = { "name": "Peter parker", "heroName": "Spiderman", "friends": ["Deadpool", "Hulk", "Wolverine"] }; delete spidermanDetail.friends[2]; for (i in spidermanDetail.friends) { str += spidermanDetail.friends[i] + "<br/>"; } document.getElementById("paraId").innerHTML = str; </script> </body> </html> |
Output:
Deadpool Hulk
Nested Arrays in JSON Objects:
In the nested array, another array can also be a value of an array.
<!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <p id="paraId"></p> <script> var str = ""; var spidermanDetail = { "name": "Peter parker", "heroName": "Spiderman", "friends": [{ "heroName": "Deadpool", "skills": ["Martial artist", "Assassin"] }, { "heroName": "Hulk", "skills": ["Superhuman Speed", "Superhuman Strength"] }, { "heroName": "Wolverine", "skills": ["Retractable bone claws", "Superhuman senses"] }] }; for (i in spidermanDetail.friends) { str += "<h3>" + spidermanDetail.friends[i].heroName + "</h3>"; for (j in spidermanDetail.friends[i].skills) { str += spidermanDetail.friends[i].skills[j] + "<br/>"; } } document.getElementById("paraId").innerHTML = str; </script> </body> </html> |
Output:

Recommended Posts:
- How to convert JSON string to array of JSON objects using JavaScript ?
- JSON | modify an array value of a JSON object
- Javascript | JSON PHP
- JavaScript JSON
- Converting JSON text to JavaScript Object
- JavaScript JSON stringify() Method
- JavaScript | JSON HTML
- Deserializing a JSON into a JavaScript object
- JavaScript JSON parse() Method
- JavaScript | Remove a JSON attribute
- JavaScript | Check if a string is a valid JSON string
- JavaScript | Convert an array to JSON
- JavaScript | Add new attribute to JSON object
- JavaScript | How to add an element to a JSON object?
- JavaScript | Check if a key exists inside a JSON object
- How to pretty print JSON string in JavaScript ?
- How to convert PHP array to JavaScript or JSON ?
- How to convert JSON data to a html table using JavaScript/jQuery ?
- How to Convert JS Object to JSON String in JQuery/Javascript?
- How to send a JSON object to a server using Javascript?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


