user1513388
user1513388

Reputation: 7451

Searching JSON objects with JQuery

I have the following JSON object. Using JQuery I need to find the values of the following:

summary.nameValues.ID and detail.TypedNameValues.size

Could somebody please show how this can be achieved using JQuery?

[
{
    "path": "\\Users\\john.smith\\test",
    "summary": {
        "NameValues": [
            {
                "Name": "Id",
                "Values": [
                    "232639"
                ]
            },
            {
                "Name": "City",
                "Values": [
                    "London"
                ]
            }
        ]
    },
    "detail": {
        "String": "some data",
        "Result": 0,
        "TypedNameValues": [
            {
                "Name": "name1",
                "Type": "string",
                "Value": "data here!!"
            },
            {
                "Name": "size",
                "Type": "long",
                "Value": "434353"
            }
        ]
    }
 }
]

Upvotes: 1

Views: 7844

Answers (3)

Hakan Bilgin
Hakan Bilgin

Reputation: 1122

Performing this kind of queries on JSON structures are trivial using DefiantJS (http://defiantjs.com). This lib extends the global object JSON with the method "search" - with which one can execute XPath expressive searches.

Check out this fiddle;
http://jsfiddle.net/hbi99/kLE2v/

The code can look like this:

var data = [
       {
          "path": "\\Users\\john.smith\\test",
          "summary": {
             "NameValues": [
                {
                   "Name": "Id",
                   "Values": "232639"
                },
                {
                   "Name": "City",
                   "Values": "London"
                }
             ]
          },
          "detail": {
             "String": "some data",
             "Result": 0,
             "TypedNameValues": [
                {
                   "Name": "name1",
                   "Type": "string",
                   "Value": "data here!!"
                },
                {
                   "Name": "size",
                   "Type": "long",
                   "Value": "434353"
                }
             ]
          }
       }
    ],
    res = JSON.search( data, '//*[Name="size"]' );

console.log( res[0].Value );
// 434353

Upvotes: 1

Thalaivar
Thalaivar

Reputation: 23642

Some one else as already answered, either way here is my version for the same.

<textarea id="ta" style="display:none;">[
{
    "path": "\\Users\\john.smith\\test",
    "summary": {
        "NameValues": [
            {
                "Name": "Id",
                "Values": [
                    "232639"
                ]
            },
            {
                "Name": "City",
                "Values": [
                    "London"
                ]
            }
        ]
    },
    "detail": {
        "String": "some data",
        "Result": 0,
        "TypedNameValues": [
            {
                "Name": "name1",
                "Type": "string",
                "Value": "data here!!"
            },
            {
                "Name": "size",
                "Type": "long",
                "Value": "434353"
            }
        ]
    }
 }
]</textarea>

Parser

var obj = $.parseJSON($('#ta').val());
var nameValues = obj[0].summary.NameValues;
var typedNameValues = obj[0].detail.TypedNameValues;

function getObjByName(o, name) {
    for (var i = 0; i < o.length; i++) {
        if (o[i].Name == name) {
            return o[i];
        }
    }
    return null;
}

alert(getObjByName(nameValues, 'Id').Values.join(", "));
alert(getObjByName(typedNameValues, 'size').Value);

A working fiddle for you on the same.

http://jsfiddle.net/3EVE4/

Upvotes: 0

Luke Walsh
Luke Walsh

Reputation: 281

jQuery doesn't work on plain object literals. You can use the below function in a similar way to search all 'id's (or any other property), regardless of its depth in the object:

function getObjects(obj, key, val) {
    var objects = [];
    for (var i in obj) {
        if (!obj.hasOwnProperty(i)) continue;
        if (typeof obj[i] == 'object') {
            objects = objects.concat(getObjects(obj[i], key, val));
        } else if (i == key && obj[key] == val) {
            objects.push(obj);
        }
    }
    return objects;
}

Use like so:

getObjects(TestObj, 'id', 'A'); // Returns an array of matching objects

This answer taken from another thread. You may find more help here: use jQuery's find() on JSON object

Upvotes: 1

Related Questions