I am getting the following response from a GET API call to Firebase database. It is a nested JSON objects.
I want to get all the values for key name
from each nested object into an array using JavaScript
GET REST API Response:
{
"barID1": {
"address": "4 East Terrace, Sydney NSW 2000",
"appStoreURL": "http://itunes.apple.com/app/idXXXXXXXXX",
"description": “description text”,
"imgURLs": [ "Https:url1”, "https:url2”, "https:url3” ],
"lat": -34.810585,
"lon": 138.616739,
"name": "Africola",
"phone": "(08) 8223 3885",
"status": "active",
"venueImgURL": "https:url”
},
"barID2": {
"address": "138/140 Gouger St, Sydney NSW 2000",
"appStoreURL": "http://itunes.apple.com/app/idXXXXXXXXX",
"description": “description text”,
"imgURLs": [ "Https:url1”, "https:url2”, "https:url3” ],
"lat": -34.848082,
"lon": 138.599813,
"name": "Disco Mexico Taqueria",
"phone": "0416 855 108",
"status": "active",
"venueImgURL": "https:url”
}
}
In order to get a key-value pair from a KiiObject, call the get() method of the KiiObject class. Specify the key for the value to get as the argument of the get() method. The value of the key at the first level of the JSON document hierarchy will be obtained.
A JSON object contains zero, one, or more key-value pairs, also called properties. The object is surrounded by curly braces {} . Every key-value pair is separated by a comma.
Use pd. read_json() to load simple JSONs and pd. json_normalize() to load nested JSONs. You can easily access values in your JSON file by chaining together the key names and/or indices.
Here I use for in
to loop each object in your JSON then push the name into the result array.
const data = {
"barID1": {
"address": "4 East Terrace, Sydney NSW 2000",
"appStoreURL": "http://itunes.apple.com/app/idXXXXXXXXX",
"description": "description text",
"imgURLs": ["Https:url1", "https: url2", "https:url3"],
"lat": -34.810585,
"lon": 138.616739,
"name": "Africola",
"phone": "(08) 8223 3885",
"status": "active",
"venueImgURL": "https:url"
},
"barID2": {
"address": "138/140 Gouger St, Sydney NSW 2000",
"appStoreURL": "http://itunes.apple.com/app/idXXXXXXXXX",
"description": "description text",
"imgURLs": ["Https:url1", "https: url2", "https:url3"],
"lat": -34.848082,
"lon": 138.599813,
"name": "Disco Mexico Taqueria",
"phone": "0416 855 108",
"status": "active",
"venueImgURL": "https:url"
}
}
let result = []
for (let i in data) {
result.push(data[i].name)
}
console.log(result)
It can be done using:
Using Array.reduce
to accumulate the name
values into a single array.
Using Object.keys
and Array.map
to iterate through the keys and map it to the name
array.
Using Object.values
and Array.map
Using Array.from
and utilizing the second mapping function parameter to map individual objects to an array of names
.
const obj = {"barID1":{"address":"4 East Terrace, Sydney NSW 2000","appStoreURL":"http://itunes.apple.com/app/idXXXXXXXXX","description":"description text","imgURLs":["Https:url1","https:url2","https:url3"],"lat":-34.810585,"lon":138.616739,"name":"Africola","phone":"(08) 8223 3885","status":"active","venueImgURL":"https:url"},"barID2":{"address":"138/140 Gouger St, Sydney NSW 2000","appStoreURL":"http://itunes.apple.com/app/idXXXXXXXXX","description":"description text","imgURLs":["Https:url1","https:url2","https:url3"],"lat":-34.848082,"lon":138.599813,"name":"Disco Mexico Taqueria","phone":"0416 855 108","status":"active","venueImgURL":"https:url"}};
//using Object.values & reduce
let name = Object.values(obj).reduce((acc, ele) =>{
return acc.concat(ele.name)
}, []);
console.log(name);
//using Object.keys & map
name = Object.keys(obj).map((ele) => obj[ele]['name']);
console.log(name);
//using Object.values & map
name = Object.values(obj).map((ele) => ele.name);
console.log(name);
//using Array.from
name = Array.from(Object.values(obj), ele => ele.name);
console.log(name);
You could extract the values of the input object via Object.values()
and then map()
the name
from each object
value as shown below to achieve this:
const data = {
barID1: {
address: "4 East Terrace, Sydney NSW 2000",
appStoreURL: "http://itunes.apple.com/app/idXXXXXXXXX",
description: "description text",
imgURLs: [ "Https:url1", "https:url2", "https:url3" ],
lat: -34.810585,
lon: 138.616739,
name: "Africola",
phone: "(08) 8223 3885",
status: "active",
venueImgURL: "https:url"
},
barID2: {
address: "138/140 Gouger St, Sydney NSW 2000",
appStoreURL: "http://itunes.apple.com/app/idXXXXXXXXX",
description: "description text",
imgURLs: [ "Https:url1", "https:url2", "https:url3" ],
lat: -34.848082,
lon: 138.599813,
name: "Disco Mexico Taqueria",
phone: "0416 855 108",
status: "active",
venueImgURL: "https:url"
}
}
console.log( Object.values(data).map(object => object.name) )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With