Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
What is JSON?
Next article icon

What is JSON Array?

Last Updated : 23 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

JSON Array is almost the same as JavaScript Array.

JSON array can store values of type string, array, boolean, number, object, or null. In JSON array, values are separated by commas. Array elements can be accessed using the [] operator.

JSON Array is of different types. Let’s understand them with the help of examples.

JSON Array of String

JSON array of Strings contains string elements only. For example, the array below has 6 string elements, “Ram”, “Shyam”, “Radhika”, “Akshay”, “Prashant” and “Varun”, each element is separated with a comma (,).

["Ram", "Shyam", "Radhika", "Akshay", "Prashant", "Varun"]

Example: Here we will assign a JSON Array of Strings to the key students in the jsonStringArray object. Then we access the first element of an array using the [ ] operator. 

HTML
<!DOCTYPE html> <html>  <head>     <title>JSON Array</title> </head>  <body>     <p id="para"></p>      <script>         let jsonStringArray = {              // Assigned a JSON array of strings             // to the key "students".             "students": ["Ram", "Shyam", "Radhika",                 "Akshay", "Prashant", "Varun"],         };          // It returned an array. Then we accessed         // the first index of the array         // (which is "Ram") using [] syntax.         let x = jsonStringArray.students[0];          // Set the inner HTML of "para" paragraph          // to the value of variable "x".          document.getElementById("para").innerHTML = x;     </script> </body>  </html> 

Output:

Ram

JSON Array of Numbers

JSON array of Numbers contains number elements only. For example the array below has 5 elements, 23, 44, 76, 34, 98.

[23, 44, 76, 34, 98]

Example: Here we assign a JSON Array of Numbers to the key marks in jsonNumberArray object. Then we access the first element of array using [ ] operator. 

HTML
<!DOCTYPE html> <html>  <head>     <title>JSON Array</title> </head>  <body>     <p id="para"></p>      <script>         let jsonNumberArray = {              // Assigned a JSON array of numbers             // to the key "marks".             "marks": [23, 44, 76, 34, 98],         };          // It returned an number array.          // Then we accessed the first         // index of the array         // (which is 23) using [] syntax.         let x = jsonNumberArray.marks[0];         // Set the inner HTML of "para" paragraph          // to the value of variable "x".          document.getElementById("para").innerHTML = x;     </script> </body>  </html> 

Output:

23

JSON Array of Booleans

JSON array of Booleans contains boolean elements only (either true or false). For example, the array below has 5 elements in it each one of that is either true or false.

[true, true, true, false, false, true]

Example: Here we assign a JSON Array of Booleans to the key boolean in jsonBooleanArray object. Then we access the first element of array using [ ] operator. 

HTML
<!DOCTYPE html> <html>  <head>     <title>JSON Array</title> </head>  <body>     <p id="para"></p>      <script>         let jsonBooleanArray = {              // Assigned a JSON array of boolean             // to the key "booleans".             "booleans": [true, true, true, false, false, true],         };          // Here we accessed the booleans property          // of jsonBooleanArray Object.         // It returned an boolean array. Then we accessed the         // first index of the array         // (which is true) using [] syntax.                let x = jsonBooleanArray.booleans[0];                // Set the inner HTML of "para" paragraph          // to the value of variable "x".          document.getElementById("para").innerHTML = x;     </script> </body>  </html> 

Output:

true

JSON Array of Objects

A JSON object is same as JavaScript object. We can also create a JSON Array containing many JSON objects in it, then we can iterate over that array or use the [ ] to get the object we need. In the example below, there are three JSON objects in the array assigned to key “books”. Each object has “name” and “author” property. 

{
"books":[
{"name":"Let Us C", "author":"Yashavant Kanetkar"},
{"name":"Rich Dad Poor Dad", "author":"Robert Kiyosaki "},
{"name":"Introduction to Algorithms", "author":"Cormen"},
]
}

Example: Here we assign a JSON Array of Objects to the key books in jsonObjectArray object. Then we access the first element of array using [ ] operator. 

HTML
<!DOCTYPE html> <html>  <head>     <title>JSON Array</title> </head>  <body>     <p id="para"></p>      <script>         let jsonObjectArray = {              // Assigned a JSON array of objects             // to the key "books"             "books": [                 {                     "name": "Let Us C",                     "author": "Yashavant Kanetkar"                 },                 {                     "name": "Rich Dad Poor Dad",                     "author": "Robert Kiyosaki "                 },                 {                     "name": "Introduction to Algorithms",                     "author": "Cormen"                 },             ]         };          // Here we accessed the books property of         // jsonObjectArray Object.         // It returned an object array. Then we          // accessed the first index of the array         // (which is an JSON object) using [] syntax          let x = jsonObjectArray.books[0];          // Set the inner HTML of "para" paragraph         // to the value of variable "x".          document.getElementById("para").innerHTML             = x.name + " by " + x.author;     </script> </body>  </html> 

Output:

Let Us C by Yashavant Kanetkar

JSON Array of Arrays OR JSON Multidimensional Array

It is also possible to create a JSON array containing other arrays as elements in it. In the example below we have a JSON array which contains arrays [“a”, “b”, “c”], [“d”, “e”, “f”], [“g” , “h”, “i”] in it. We can use [ ] operator to get the array at any index and use the [ ] operator again to get the element of the selected array.

{
"matrix": [
[ "a", "b", "c" ],
[ "d", "e", "f" ],
[ "g", "h", "i" ]
],
};

Example: Here we assign a JSON Array of Arrays to the key matrix in jsonMultiArray object. Then we access the first element of array using [ ] operator. 

HTML
<!DOCTYPE html> <html>  <head>     <title>JSON Array</title> </head>  <body>     <p id="para"></p>      <script>         let jsonMultiArray = {              // Assigned a JSON array of              // Arrays to the key "matrix".             "matrix": [                 ["a", "b", "c"],                 ["d", "e", "f"],                 ["g", "h", "i"]             ],         };          // Here we accessed the matrix property          // of jsonMultiArray Object.         // It returned an matrix(2D array). Then we          // accessed the first element of         // the first index of matrix using [] syntax.         let x = jsonMultiArray.matrix[0][0];          // Set the inner HTML of "para" paragraph          // to the value of variable "x".          document.getElementById("para").innerHTML = x;     </script> </body>  </html> 

Output:

a


Next Article
What is JSON?
author
vpsop
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • JavaScript-Questions
  • JSON

Similar Reads

  • What is JSON?
    JSON (JavaScript Object Notation) is a lightweight text-based format for storing and exchanging data. It is easy to read, write, and widely used for communication between a server and a client. Key points:JSON stores data in key-value pairs.It is language-independent but derived from JavaScript synt
    3 min read
  • What is JSON text ?
    JSON text refers to a lightweight, human-readable format for structuring data using key-value pairs and arrays. It is widely used for data interchange between systems, making it ideal for APIs, configuration files, and real-time communication. In today’s interconnected digital world, data flows seam
    4 min read
  • What is JSON-Java (org.json)?
    JSON(Javascript Object Notation) is a lightweight format of data exchange and it is independent of language. It is easily read and interpreted by humans and machines. Hence nowadays, everywhere JSON is getting used for transmitting data. Let us see how to prepare JSON data by using JSON.org JSON API
    5 min read
  • How to Encode Array in JSON PHP ?
    Encoding arrays into JSON format is a common task in PHP, especially when building APIs or handling AJAX requests. Below are the approaches to encode arrays into JSON using PHP: Table of Content Using json_encode()Encoding Associative ArraysCustom JSON SerializationUsing json_encode()PHP provides a
    2 min read
  • PHP array() Function
    The array() function is an inbuilt function in PHP which is used to create an array. There are three types of array in PHP: Indexed array: The array which contains numeric index. Syntax: array( val1, val2, val3, ... ) Associative array: The array which contains name as keys. Syntax: array( key=>v
    2 min read
  • JavaScript | JSON Arrays
    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 Ar
    2 min read
  • What is JSONB in PostgreSQL?
    PostgreSQL is a powerful object-relational database management system that excels at handling structured and semi-structured data, especially through its support for JSONB. JSONB (Binary JSON) allows efficient storage and querying of JSON data and making it ideal for applications that require quick
    5 min read
  • Loop through a JSON array in Python
    A JSON array is an ordered list of values that can store multiple values such as string, number, boolean, or object. The values in a JSON array must be separated by commas and enclosed in squares in brackets []. In this article, we will learn how we can loop through a JSON array in Python. Iterate o
    4 min read
  • PHP Arrays
    Arrays are one of the most important data structures in PHP. They allow you to store multiple values in a single variable. PHP arrays can hold values of different types, such as strings, numbers, or even other arrays. Understanding how to use arrays in PHP is important for working with data efficien
    5 min read
  • JavaScript Array Exercise
    In JavaScript, an Array is a versatile data structure that allows you to store multiple values in a single variable. Arrays can hold different data types, including strings, numbers, objects, and even other arrays. JavaScript arrays are dynamic, which means they can grow or shrink in size. [GFGTABS]
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences