Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i see if a big JSON object contains a value?

I'm using PHP to json encode a massive multi-dimensional array of events, so i get something like this:

var ents = {"7":{"event_id":"7","nn":"The Whisky Drifters","nn_url":"the-whisky-drifters",
  "venue":"The Grain Barge","date_num":"2010-06-11","date_txt":"Friday 11th June",
  "gig_club":"1","sd":"A New Acoustic String Band...","ven_id":"44",
  "art":0},"15":{"event_id":"15","nn":"Bass Kitchen","nn_url":"bass-kitchen",
  "venue":"Timbuk2","date_num":"2010-06-11","date_txt":"Friday 11th June",
  "gig_club":"2","sd":"Hexadecimal \/ DJ Derek \/ Id","ven_id":"21",
  "art":1},

the first dimension is the id, see

var ents = {"7":{

So it's possible to get the ids without examining the nested objects...

What's the fastest, most efficient way to check if my JSON contains an id?

like image 418
Haroldo Avatar asked Jun 11 '10 08:06

Haroldo


People also ask

How do you check if a JSON object contains a key or not?

Use below code to find key is exist or not in JsonObject . has("key") method is used to find keys in JsonObject . If you are using optString("key") method to get String value then don't worry about keys are existing or not in the JsonObject . Note that you can check only root keys with has(). Get values with get().

How large is too large for JSON?

One of the more frequently asked questions about the native JSON data type, is what size can a JSON document be. The short answer is that the maximum size is 1GB.

Can JSON key contains dot?

It's impossible to use a literal dot in a JSON key with FileMaker's JSON parsing functions.

What does JSON object contain?

A JSON object contains zero, one, or more key-value pairs, also called properties. The object is surrounded by curly braces {} . Every key-value pair is separated by a comma. The order of the key-value pair is irrelevant.


3 Answers

You can use the hasOwnProperty method:

if (ents.hasOwnProperty('7')) {   //.. } 

This method checks if the object contains the specified property regardless of its value.

Is faster than the in operator because it doesn't checks for inherited properties.

like image 138
Christian C. Salvadó Avatar answered Sep 20 '22 15:09

Christian C. Salvadó


Additionally to what CMS said: If you need all properties, you can loop over the porperties with for ... in:

for (prop in ents) {
  alert(prop); // Shows "7", "15", etc.
  // Accessing the sub-object:
  alert(ents[prop].nn); // Shows the names of each event
}

Also that isn't a "multi-dimensional array". It's an object (with more nested objects).

like image 36
RoToRa Avatar answered Sep 22 '22 15:09

RoToRa


yes it is possible but you have to loop through complete json object on client side.

var JSONobj = ents, yourid;
for(key in JSONobj)
{
   if(((typeof key) == 'number') && key==yourid )
         alert(key);
}

if you are using jQuery then you can use $.each method to fetchng keys from jsonObject

   var JSONobj = ents, yourid;
        $.each(JSONobj, function(key, value){     
             if(((typeof key) == 'number') && key==yourid )
                 alert(key);
           //ids.push(key);
        });
like image 42
Ayaz Alavi Avatar answered Sep 19 '22 15:09

Ayaz Alavi