Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
99
rated 0 times [  100] [ 1]  / answers: 1 / hits: 24121  / 7 Years ago, wed, november 22, 2017, 12:00:00

So my json data is something like the following,



{
amazon: []
},
{
flipkart: {
product_store: Flipkart,
product_store_logo: logo url,
product_store_url: shop url,
product_price: 14999,
product_offer: ,
product_color: ,
product_delivery: 3-4,
product_delivery_cost: 0,
is_emi: 1,
is_cod: 1,
return_time: 10 Days
}
},
{
snapdeal: []
}


So here I want to loop through each shop and check if it is a jsonarray(amazon) or jsonobject(flipkart) then delete those empty array. And add it to a new array.



Just like the example all keys who have no values like amazon and snapdeal are arrays. And keys with values are jsonObject. I can't change the json.



So I want to know how can I detect JSONObject and JSONArray...



Thank you..


More From » json

 Answers
18

You can use Array.isArray to check if it is array:



if (Array.isArray(json['amazon'])) {
// It is array
}


and you can check if type of object is object to determine if it is an object. Please refer, for example, this answer for the reason why check for null is separate:



if (json['flipkart'] !== null && typeof (json['flipkart']) === 'object') {
// It is object
}

[#55866] Sunday, November 19, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
janettejordynm

Total Points: 550
Total Questions: 94
Total Answers: 98

Location: Senegal
Member since Fri, Aug 21, 2020
4 Years ago
;