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

Bucky
  • 1,116
  • 2
  • 18
  • 34
  • 1
    *"How to check if it is a JsonArray or JsonObject in Javascript"* -- there is no such thing like *JsonArray* or *JsonObject*. [JSON](https://en.wikipedia.org/wiki/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. – axiac Nov 22 '17 at 13:47
  • 2
    Well in modern JavaScript you've got [`Array.isArray()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray) – Pointy Nov 22 '17 at 13:47
  • Did you read the description of the tag `json` when you added it? – trincot Nov 22 '17 at 13:53

3 Answers3

15

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
}
Flying
  • 4,422
  • 2
  • 17
  • 25
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
}
victor_reiner
  • 107
  • 1
  • 8
1

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);
YouneL
  • 8,152
  • 2
  • 28
  • 50