I have JSON data that is in the format below. I need to get the store name corresponding to the one which has specialDeal as true in “Non Veg”. (NOTE: Only one of the stores will have specialDeal as true and also please note the content inside nonVeg is not an array). How will I retrieve it? Please help. Thanks in advance.
{
"Veg": {
"amtSpent":"", "shortDesc":"", "longDesc":"", "excTxt":"",
"discntType":"", "Store":"", "StoreType":"", "Fund":"",
"FundDetails":[
{"status":"", "discntVal":"", "FundVal":"", "FundBal":""},
{"status":"", "discntVal":"", "FundVal":"", "FundBal":""}
]
},
"Non Veg": {
"chicken":[
{
"amtSpent":"", "shortDesc":"", "longDesc":"", "excTxt":"",
"discntType":"", "Store":"", "StoreType":"", "Fund":"",
"specialDeal":"", "promoStatus":"",
"FundDetails":[
{"status":"", "discntVal":"", "FundVal":"", "FundBal":""},
{"status":"", "discntVal":"", "FundVal":"", "FundBal":""}
]
},
{
"amtSpent":"", "shortDesc":"", "longDesc":"", "excTxt":"",
"discntType":"", "Store":"", "StoreType":"", "Fund":"",
"specialDeal":"", "promoStatus":"",
"FundDetails":[
{"status":"", "discntVal":"", "FundVal":"", "FundBal":""},
{"status":"", "discntVal":"", "FundVal":"", "FundBal":""}
]
}
],
"fish":[
{
"amtSpent":"", "shortDesc":"", "longDesc":"", "excTxt":"",
"discntType":"", "Store":"", "StoreType":"", "Fund":"",
"specialDeal":"", "promoStatus":"",
"FundDetails":[
{"status":"", "discntVal":"", "FundVal":"", "FundBal":""},
{"status":"", "discntVal":"", "FundVal":"", "FundBal":""}
]
},
{
"amtSpent":"", "shortDesc":"", "longDesc":"", "excTxt":"",
"discntType":"", "Store":"", "StoreType":"", "Fund":"",
"specialDeal":"", "promoStatus":"",
"FundDetails":[
{"status":"", "discntVal":"", "FundVal":"", "FundBal":""},
{"status":"", "discntVal":"", "FundVal":"", "FundBal":""}
]
}
],
"egg":[
{
"amtSpent":"", "shortDesc":"", "longDesc":"", "excTxt":"",
"discntType":"", "Store":"", "StoreType":"", "Fund":"",
"specialDeal":"", "promoStatus":"",
"discntVal":"", "FundVal":"", "FundBal":""
},
{
"amtSpent":"", "shortDesc":"", "longDesc":"", "excTxt":"",
"discntType":"", "Store":"", "StoreType":"", "Fund":"",
"specialDeal":"", "promoStatus":"",
"discntVal":"", "FundVal":"", "FundBal":""
}
]
},
"isMember":"Y",
"orderId":""
}
Here’s the code I’ve tried:
var nonveg = DealsJSON.Non Veg; //where JSON is our json data
for (var key in nonveg) {
for (var i = 0; i < nonveg[key].length; i++) {
var amountObj = nonveg[key][i];
if (amountObj['specialDeal'] == true) {
console.log(amountObj['Store']);
}
}
}
The code looks fine other than:
…which is a syntax error. Property literals can’t have spaces in them, even though property names can. To use a property whose name is not a valid literal, you can use bracketed notation:
Details: In JavaScript, you can access an object property in two ways:
Dot notation and a literal for the name, e.g.
obj.foo.Bracketed notation and a string for the name, e.g.
obj["foo"]. And the string doesn’t have to be just a string literal, it can be the result of any expression.Separately, this is suspect:
Comparing things to
truewith==in JavaScript is usually not a good idea. If you just want to know ifspecialDealhas a value that isn’t0,false,"",undefined, ornull(and it can’t beundefined, JSON doesn’t support that), just remove the comparison:or of course
Also note that in your JSON,
specialDealis given as a string. If the string is always blank for things that aren’t special deals, that’s fine, because blank strings are falsey. But if the string may contain, for instance,"false", you have to be careful, because the string"false"is notfalse. We’d have to see an example of other possible values in order to advise you how to deal with that, but if it’s always""for false and some other string for true, the above is fine.If I change your JSON to have a
specialDealthat’strue, this code with the mods listed above works fine: Live Example | Source