0

Sometimes there are a few values in JSON which are not present as name value pairs but as only names and then their properties and below for example in the JSON below objectOne, ObjectTwo and objectThree. The problem is that there names keep on changing how can I extract them if I don’t know in advance what these names are going to be? But the data structure will be same

 {
        "Number of objects": 3,
        "Devices": {
            "objectOne": {
                "name": "10",
                "name1": "50"
            },
            "objectTwo": {
                "name": "20",
                "name1": "30"
            },
            "objectThree": {
                "name": "40",
                "name1": "80"
            }
        }
    }
Imo
  • 1,455
  • 4
  • 28
  • 53
  • http://stackoverflow.com/questions/15523514/find-by-key-deep-in-nested-json-object – Jonathan.Brink Feb 05 '16 at 13:00
  • 1
    You can iterate over the properties of an object like this `for(var prop in obj.Devices)` to dynamically get the propertynames. – A1rPun Feb 05 '16 at 13:00
  • How are you going to use these dynamic device names? Do you need to print their properties one by one or you need to access a specific one? – AKS Feb 05 '16 at 13:00
  • Do you control the source? Normalize there if you do. What is use case? Also look into `Object.keys()` – charlietfl Feb 05 '16 at 13:02
  • @AKS for example if I want to extract device names then show them in a dropdown menu and then as I select one device name from menu value of name1 appears. – Imo Feb 05 '16 at 13:03
  • 1
    *"...not present as name value pairs but as only names and then their properties and below for example in the JSON below objectOne, ObjectTwo and objectThree..."* They're still name/value pairs. It's just that the value is an object. – T.J. Crowder Feb 05 '16 at 13:04
  • @T.J.Crowder: thank you for explaining that :) – Imo Feb 05 '16 at 13:13

1 Answers1

3

You can try to use Object.keys method.

Sample :

var yourJson = {
    "Number of objects": 3,
    "Devices": {
        "objectOne": {
            "name": "10",
            "name1": "50"
        },
        "objectTwo": {
            "name": "20",
            "name1": "30"
        },
        "objectThree": {
            "name": "40",
            "name1": "80"
        }
    }
}
var keys = Object.keys(yourJson.Devices); // Array with "objectOne", "objectTwo" and "objectThree"

UPDATE : Then you can access to objectTwo this way :

var objectTwo = yourJson.Devices[keys[1]];

If you need to iterate through all, this is better :

for (var key in keys) {
    // key = "objectOne", then "objectTwo", then "objectThree"
    var objectN = yourJson.Devices[key]; // the device object
}
Vinks
  • 156
  • 5
  • Thanks this is the answer I was looking for just more question after your code if I want to extract value of name1 how can I do that I tried to do var name1 = keys.name1 but it gives me undefined? – Imo Feb 05 '16 at 13:12
  • keys is an array holding the names of the objects. you can access their properties with yourJson[keys[0]].name1 – Mihai Răducanu Feb 05 '16 at 13:18
  • I just updated my answer. – Vinks Feb 05 '16 at 13:19