Navigate back to the homepage

How to get array from json object in javascript?

Infinitbility
JavaScript
May 16th, 2022 · 1 min read

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 response
2let 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};
11
12// try to acccess status
13console.log("response status", response.status)
14
15// try to acccess message
16console.log("response message", response.message)
17
18// try to access data
19console.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.

JavaScript, get array from json object example
JavaScript, get array from json object example

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.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How to convert array of strings to comma separated string in Javascript?

Example of convert array of strings to comma separated string in javascript with step by step guide.

May 15th, 2022 · 1 min read

How to remove duplicates from array of strings in javascript?

Example of remove duplicates from array of strings in javascript with step by step guide.

May 15th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]