Bucky
Bucky

Reputation: 1206

How to check if it is a JsonArray or JsonObject in Javascript

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

Upvotes: 5

Views: 33543

Answers (3)

YouneL
YouneL

Reputation: 8369

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);

Upvotes: 2

victor_reiner
victor_reiner

Reputation: 105

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
}

Upvotes: 3

Flying
Flying

Reputation: 4570

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
}

Upvotes: 17

Related Questions