This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 8 years ago.
I'm very new to JSON, so please don't scald me. I'm trying to find a way to retrieve information from this bit of data, but I'm having zero luck.
var services = [ "Hernia Repair", "Hip Replacement" ... ];
var places = {
"place": [
{
"name": "The Whittington Hospital",
"service": [
"Hernia Repair",
{
"ratings": [
{ "name": "John Doe", "score": 4, "review": "Some text here." },
{ "name": "Jane Doe", "score": 5, "review": "Some more text." }
]
}
...
]
},
]
};
I'm trying to grab the name of the service and it's ratings's name and score and I've been trying to output it to the console like this:
places.place[0].service.ratings.name
places.place[0].service.ratings.score
But that doesn't seem to be working. If anyone could lend a hand to explain if my nesting is correct or if I'm retrieving them in the correct way.
Any help with this is appreciated.
P.S. Is there also a way to elegantly link the "Hernia Repair" string in the services array to the "Hernia Repair" string in the place object?
Service is a list, and ratings is a list, so this should do it:
places.place[0].service[1].ratings[0].name
Related
This question already has answers here:
How do I loop through or enumerate a JavaScript object?
(48 answers)
Closed 2 years ago.
I am trying to access nested property productTitle in below order JSON, however, could only manage to get the items using
JSON.stringify(orders[i].cart.items, null, 4);
I have tried the nested loop but to no avail. I have also tried
JSON.stringify(orders[i].cart.items.item, null, 4);
which is also not working. Missing something fundamental, I have spent hours looking through JSON docs and tutorials but could not find specific help.
[
{
"_id": "5ea5115224bf1f569c4fac96",
"user": "5ea19f4160539d264ca1e7c5",
"cart": {
"items": {
"5e92b488fc3e326b70be262d": {
"item": {
"_id": "5e92b488fc3e326b70be262d",
"productCode": "PROD002",
"productType": "Duvet",
"productTitle": "Kingsize Duvet",
"__v": 0
},
"qty": 1,
"price": 3
},
"5e92f5f9213dc863e0e97682": {
"item": {
"_id": "5e92f5f9213dc863e0e97682",
"productCode": "PROD006",
"productType": "Duvet",
"productTitle": "double-Duvet",
"__v": 0
},
"qty": 2,
"price": 5
}
},
"totalQty": 3,
"totalPrice": 8
},
"address": "London",
"name": "John Cena",
"paymentId": "ch_1Gc2KUIyQsnk8nhOTUR602KW",
"__v": 0
}
]
Please be gentle, I am a newbie!
Update:
Manage to get the nested keys and values using below iteration:
for(var i=0;i
for (var key of Object.keys(orders[i].cart.items)) {
console.log("Title: "+ orders[i].cart.items[key].item.productTitle );
console.log("Quanitity: "+ orders[i].cart.items[key].qty);
console.log(" Price: "+orders[i].cart.items[key].item.productPrice );
console.log(" Subtotal: "+orders[i].cart.items[key].item.productPrice * orders[i].cart.items[key].qty);
console.log("Total Quantity is :"+orders[i].cart.totalQty+" & Total Price: £ " + orders[i].cart.totalPrice);
}
I think you have missed fetching 5e92b488fc3e326b70be262d in your call dot reference call after items .
orders[i].cart.items['5e92f5f9213dc863e0e97682'].item.productTitle
This worked for me .
5e92b488fc3e326b70be262d is also an object in your json so you have to access 5e92b488fc3e326b70be262d object before accessing item .
'5e92f5f9213dc863e0e97682'has been added in your json as a map and not as an object. That's why not working with dot operator.
This is what I did .
this.http.get("assets/client.json").subscribe(data =>{
console.log(data[0].cart.items['5e92f5f9213dc863e0e97682'].item.productTitle);
var items = data[0].cart.items;
console.log(items['5e92f5f9213dc863e0e97682'].item.productTitle);
})
Both console statements gave double-Duvet as output .
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 3 years ago.
I have tried to parse this JSON file. But I see undefined.
I need to receive only value, where the key equals level1.
[{
"id": 2,
"name": "Peter",
"products": [{
"title": "first",
"price": 100
},
{
"title": "second",
"price": 200,
"desciption": [{
"level1": "good",
"level2": "bad"
},
{
"level3": "super",
"level4": "hell"
}
]
}
],
"country": "USA"
}]
const fs = require('fs');
let file = fs.readFileSync("./file.json");
let parsed = JSON.parse(file);
console.log(parsed["name"])
console.log(parsed.name);
and I see in the conlose "undefined"
Your JSON data represents an array of objects. If after parsing you want the property "name" of the first element, it's:
console.log(parsed[0]["name"])
or
console.log(parsed[0].name);
I am working with facebook JS SDK which returns user's information in JSON format. I know how to get the response like response.email which returns email address. But how to get an element from a nested array object? Example: user's education history may contain multiple arrays and each array will have an element such as "name" of "school". I want to get the element from the last array of an object.
This is a sample JSON I got:-
"education": [
{
"school": {
"id": "162285817180560",
"name": "Jhenaidah** School"
},
"type": "H**hool",
"year": {
"id": "14404**5610606",
"name": "2011"
},
"id": "855**14449421"
},
{
"concentration": [
{
"id": "15158**968",
"name": "Sof**ering"
},
{
"id": "20179020**7859",
"name": "Dig**ty"
}
],
"school": {
"id": "10827**27428",
"name": "Univer**g"
},
"type": "College",
"id": "9885**826013"
},
{
"concentration": [
{
"id": "108196**810",
"name": "Science"
}
],
"school": {
"id": "2772**996993",
"name": "some COLLEGE NAME I WANT TO GET"
},
"type": "College",
"year": {
"id": "1388*****",
"name": "2013"
},
"id": "8811215**16"
}]
Let's say I want to get "name": "some COLLEGE NAME I WANT TO GET" from the last array. How to do that with Javascript? I hope I could explain my problem. Thank you
Here is a JsFiddle Example
var json = '{}' // your data;
// convert to javascript object:
var obj = JSON.parse(json);
// get last item in array:
var last = obj.education[obj.education.length - 1].school.name;
// result: some COLLEGE NAME I WANT TO GET
If your json above was saved to an object called json, you could access the school name "some COLLEGE NAME I WANT TO GET" with the following:
json.education[2].school.name
If you know where that element is, then you can just select it as already mentioned by calling
var obj = FACEBOOK_ACTION;
obj.education[2].school.name
If you want to select specifically the last element, then use something like this:
obj.education[ obj.education.length - 1 ].scool.name
Try this,
if (myData.hasOwnProperty('merchant_id')) {
// do something here
}
where JSON myData is:
{
amount: "10.00",
email: "someone#example.com",
merchant_id: "123",
mobile_no: "9874563210",
order_id: "123456",
passkey: "1234"
}
This is a simple example for your understanding. In your scenario of nested objects, loop over your JSON data and use hasOwnProperty to check if key name exists.
This question already has answers here:
How do I loop through or enumerate a JavaScript object?
(48 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 6 years ago.
I'm working with a GeoJSON dataset that is formatted like this:
{
"type": "Feature",
"properties": {
"startcong": "109",
"district": "7",
"statename": "Pennsylvania",
"member": {
"112": {
"21168": {
"party": "Republican",
"name": "Meehan, Pat",
"district": "7"
}
},
"109": {
"15447": {
"party": "Republican",
"name": "Weldon, Curt", "district": "7"}
},
"110": {
"20744": {
"party": "Democrat",
"name": "Sestak, Joe",
"district": "7"
}
},
"111": {
"20744": {
"party": "Democrat",
"name": "Sestak, Joe",
"district": "7"
}
}
},
"endcong":
"112",
"id": "042109112007"
}
}
I'm struggling with how to access these nested objects. For instance, I can use feature.properties.member[112][21168] to access the party attribute. However:
That second numbered object ("21168") is not always consistently numbered.
That second numbered object is always the only nested object.
Is there a way to access that same party attribute using something like a wildcard? Ideally, something akin to feature.properties.member[112][*].party.
If the second number is the only nested object, you can find what the number is using the builtin Object.keys(obj), something like:
var obj = feature.properties.member[112],
key = Object.keys(obj)[0],
party = obj[key].party
Sadly there is no wildcards for property accessing, but you can find what the property names are fairly simply.
If I receive data from a server in plain JSON that looks like this:
{
"f223dc3c-946f-4da3-8e77-e8c1fe4d241b": {
"name": "Dave",
"age": 16,
"jobs": [{
"description": "Sweep the floor",
"difficulty": 4
},{
"description": "Iron the washing",
"difficulty": 6
}]
},
"84af889a-8fc9-499b-a6ea-97e7a483130c": {
...
}
}
Do I need to loop through all the jobs and convert them to Maps, then convert each object's jobs into a List, then the entire thing into a Map?
Or does ImmutableJS do this all recursively for me?
There is Immutable.fromJS() designed for exactly that.