• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

REST API Tutorial

  • REST
  • JSON
  • Dark Mode
Home / JSON / JSON Array

JSON Array

Similar to other programming languages, an Array in JSON is a list of items surrounded in square brackets ([]). Each item in the array is separated by a comma.

  1. The array index begins with 0.
  2. The square brackets [ ] are used to declare JSON array.
  3. JSON array are ordered list of values.
  4. JSON array can store multiple value types.
  5. JSON array can store string, number, boolean, object or other array inside JSON array.
  6. In JSON array, values must be separated by comma.
  7. Arrays in JSON are almost the same as arrays in JavaScript.

For example, given is a JSON document which contains a JSON array rights.

{
	"name" : "Admin",
	"age" : 36,
	"rights" : [ "admin", "editor", "contributor" ]
}

Get Value from Array

You can access the array values by using the index number:

x = myObj.rights[0];

Program output.

admin

Delete Array Value

Use the delete keyword to delete items from an array:

delete myObj.rights[1];

Update Array Value

Use the index number to modify an array:

myObj.rights[1] = "blogger";

Looping through Array Values

We can access array values by using a for-in loop:

for (i in myObj.rights) {
    x = myObj.rights[i];
    console.log(x);
}

Program output.

admin
editor
contributor

Multi-dimensional array

We can store an array inside another JSON array. It is known as an array of arrays or multi-dimensional JSON array.

var siteInfo = {
    "name" 	 : "blogger",
    "users" : [ 
    			[ "admins", "1", "2" , "3"],
                 	[ "editors", "4", "5" , "6"],
               ]
} 

Iterating over multi-dimensional array

A simple for loop to iterate over a multi-dimensional array in JSON.

for (i in siteInfo .users) {

    for (j in siteInfo.users[i]) {
         
        x = siteInfo.users[i][j];

        console.log(x);
    }
}

Program Output:

admins
1
2
3
editors
4
5
6

Was this article helpful?

TwitterFacebookLinkedInReddit
Previous Tutorial:
JSON parse()
Next Tutorial:
JSON Object

Reader Interactions

Comments

  1. Gauri says

    December 25, 2018 at 10:36 am

    If I have multiple multi-dimentional array then how can I access it dynamically…If I take value from user and I want to match with JSON array and display all array values of array

    Reply

Leave a Reply Cancel reply

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

Primary Sidebar

Search Tutorials

JSON Tutorial

  • What is JSON
  • JSON Syntax
  • JSON Data Types
  • JSON Schema
  • JSON Object
  • JSON Array
  • JSON parse()
  • JSON stringify()
  • JSON vs XML
  • JSON with Ajax
  • JSONPath

Footer

References

  • The dissertation by Roy Thomas Fielding
  • Uniform Resource Identifier (URI, URL, URN) [RFC 3986]
  • Internet MediaTypes
  • Web Application Description Language (WADL)

Meta Links

  • About
  • Contact Us
  • Privacy Policy

Blogs

  • How To Do In Java

Copyright © 2020 · restfulapi.net · All Rights Reserved. | Sitemap