5

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..

3
  • 1
    "How to check if it is a JsonArray or JsonObject in Javascript" -- there is no such thing like JsonArray or JsonObject. JSON is just text. It is the text representation of some data structure. Incidentally, it uses JavaScript notation for that. If you don't have it in a string then it is JavaScript and nothing else. If you have it in a string then you decode it to JavaScript data structures (arrays or objects) but they are not JSON and/or related to JSON in any way. Commented Nov 22, 2017 at 13:47
  • 2
    Well in modern JavaScript you've got Array.isArray() Commented Nov 22, 2017 at 13:47
  • Did you read the description of the tag json when you added it? Commented Nov 22, 2017 at 13:53

3 Answers 3

17

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
}
Sign up to request clarification or add additional context in comments.

2 Comments

typeof (json['amazon']) gives object too :)
Of course because Array is Object in JavaScript, that's why here is dedicated method to check specifically for Array was added
3

Array

If you are working with Jquery you can use isArray() Jquery function, doc here.

if($.isArray(your_array)){
   //You code
}

Without Jquery, only Javascript use Array.isArray, more info here.

if(Array.isArray(your_array)){
   //You code
}

Object

If you are working with Jquery you can use type() Jquery function, and compare with 'object' type, doc here.

if(jQuery.type(your_objet) === 'object'){
   //You code
}

Without Jquery, only Javascript use typeof, more info here.

if(typeof (your_object) === 'object'){
   //You code
}

Comments

2

In javascript arrays are objects too, so if you use typeof you will get the same result object in both cases, In your case I would use instanceof operator Or Array.isArray Like Flying's answer.

[] instanceof Array => true
{} instanceof Array => false

Array.isArray( [] ) => true
Array.isArray( {} ) => false

Here is a working example using filter method to retrieve only object instance from your json:

  var json = [
    {
      "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": []
    }
  ];
  
var result = json.filter( function (item) {
  // get the first property 
  var prop = item[ Object.keys(item)[0] ];
  return !(prop instanceof Array);
  // OR
  // return !Array.isArray(prop);
});

console.log(result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.