2

Actually I want to search an attribute's value in an json array for one of its child. Now one condition is that the attribute will not be there in all the child's of the array. This is my json array.

[{
      "heading1":"heading1",
      "heading2":"heading2",
      "heading3":"heading3",
      "heading4":"heading4",
      "heading5":"heading5",
      "heading6":"heading6"      
   },
   {
      "column1":65536,
      "column2":"school",
      "column3":"testing purpose",
      "column4":"DESKTOP",
      "column5":"ACTIVE",
      "column6":true,
      "column7":"a6cc82e0-a8d8-49b8-af62-cf8ca042c8bb"     

   },
   {
      "column1":98305,
      "column2":"Nikhil",
      "column3":"Test",
      "column4":"LAPTOP",
      "column5":"ACTIVE",
      "column6":true,
      "column7":"a6cc82e0-a8d8-49b8-af62-cf8ca042c8bb"

   }]

So presently I am working with the each loop but like this

var obj = $.parseJSON(JSON.stringify(response));
$.each(obj, function () {
    console.log("heading1", this['heading1']);
});

Here response comes from mserver and it is the json array

Now I want to know can I search for this attribute in the json array without using a loop in jQuery.

13
  • 2
    $.parseJSON(JSON.stringify(response));? Why? Commented Dec 25, 2013 at 13:09
  • @BlackSheep without using them it will not work. Commented Dec 25, 2013 at 13:10
  • Can you give us a sample search? What would be the attribute you need to search? Commented Dec 25, 2013 at 13:14
  • @Dalorzo Ya like if I want to search for heading 2 value then. Commented Dec 25, 2013 at 13:15
  • Something like this: obj.forEach(function(e, i) { if (e['heading2']) { console.log(e['heading2']) } }); (although I don't see why you need parse + stringify) Commented Dec 25, 2013 at 13:16

5 Answers 5

2

Based on your sample code what I understand you have is an array of objects and you want to find objects with one specific property and or value:

This will return true if the object has the property

var results= arr.filter(function(item){ return item.hasOwnProperty("column5");  });

Or you can perform additional action when you find the property:

arr.filter(function(item){ 
   if (item.hasOwnProperty("column5")) { 
        return item["column5"] === 'demo 01'; //or item.column5 === 'demo 01'
   } 
   return false;   
 });
Sign up to request clarification or add additional context in comments.

Comments

2

The you can check like

var obj = $.parseJSON(response);

$.each(obj, function (index,value) {

if(typeof obj[index].heading2 !== "undefined")
{
    alert(obj[index].heading2);
}

when in other object of array element not find then it returns undefined. and you can check like that.

you can check in this http://jsfiddle.net/gKRCH/

Comments

1

It's best to use a loop. But if the format of the JSON is regular, you could regex for the value in the response string.

I'm not recommending this method, just pointing out that it exists.

var value = "heading1";
if( (new RegExp('"' + value + '"')).test(response) ){
     // Found value
};

Here, we take the required value, wrap it in quotation marks and search for it in the response.

This has several issues, such as:

  1. It might find the pattern in a property name
  2. If the value could contain regex special characters, they'll need escaping.
  3. If your JSON contains values with escaped quotation marks, you could get a false positive from partial matches.

That's why it depends on you knowing the format of the data.

EDIT:

You can solve issue 2 by using this condition instead of regex. But it gives you less flexibility.

response.indexOf('"' + value + '"') !== -1

Comments

0

Try this,

$.each(object,function(key, value){
console.log(key);
console.log(value);
});

1 Comment

what does this accomplish? Initial object is an array, so htis will log index and an object for each element in array
0

You can use this JS lib; DefiantJS (http://defiantjs.com). This lib extends the global object JSON with the method "search" - with which, you can perform XPath queries on JSON structures. Like the one you have exemplified.

With XPath expressions (which is standardised query language), you can find whatever you're looking for and DefiantJS will do the heavy-lifting for you - allowing your code to be neat and clean.

Here is the fiddle of this code:
http://jsfiddle.net/hbi99/q8xst/

Here is the code:

    var data = [
       {
          "heading1": "heading1",
          "heading2": "heading2",
          "heading3": "heading3",
          "heading4": "heading4",
          "heading5": "heading5",
          "heading6": "heading6"
       },
       {
          "column1": 65536,
          "column2": "school",
          "column3": "testing purpose",
          "column4": "DESKTOP",
          "column5": "ACTIVE",
          "column6": true,
          "column7": "a6cc82e0-a8d8-49b8-af62-cf8ca042c8bb"
       },
       {
          "column1": 98305,
          "column2": "Nikhil",
          "column3": "Test",
          "column4": "LAPTOP",
          "column5": "ACTIVE",
          "column6": true,
          "column7": "a6cc82e0-a8d8-49b8-af62-cf8ca042c8bb"
       }
    ],
    res = JSON.search( data, '//*[column4="DESKTOP"]' );

console.log( res[0].column2 );
// school

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.