Hi Friends đź‘‹,
Welcome To Infinitbility! ❤️
Today, we will learn to get an array from a JSON object, here we will take an example of a JSON object have stored sample array data and we will try to extract an array from JSON.
Basically, to access any JSON data we have to use their key, here we will also do the same we will use the object keys which have a store array.
Note: IF you json object in a string please parse it using JSON.parse()
.
For example
1const myJSON = '{"name":"John", "age":30, "car":null}';2const myObj = JSON.parse(myJSON);
Let’s come to the topic.
Here, we will take the example of a sample object which looks like an API response, where we will get status
, message
, and data
.
The data
key has our array, and we will use the data
key to access the array.
let’s understand with an example.
1// sample API response2let response = {3 status: 1,4 message: "domains fetched successfully.",5 data: [6 {id: 1, name: "infinitbility", domain: "infinitbility.com"},7 {id: 2, name: "aguidehub", domain: "aguidehub.com"},8 {id: 3, name: "sortoutcode", domain: "sortoutcode.com"},9 ]10};1112// try to acccess status13console.log("response status", response.status)1415// try to acccess message16console.log("response message", response.message)1718// try to access data19console.log("response data", response.data)
Here, we have created a sample object which has a data
key with array data, and we will access the array using .data
.
let’s see the output.

All the best 👍.
Follow me on Twitter
Join our email list and get notified about new content
No worries, I respect your privacy and I will never abuse your email.
Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.