1

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"
            }
        ]
    }
 }
]
2

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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/

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.