I have the following JSON:
{
"Files": {
"Lines": {
"198": {
"firstline": "sometext"
}
}
}
}
and the value “198” is dynamic and changes. How can I access the “firstline” property easily without knowing “198” in the following code:
var schema = JSON.parse(fileContents);
console.log(schema.Files.Lines.????.firstline);
A few lines will do it:
Note: This solution relies on some newer javascript features. For
JSONto work in all browsers, you will need Douglas Crockford’sjson2.jsfrom here. ForObject.keysto work in all browsers, use this shim from Mozilla’s JS docuentation:Edit: Try this jsfiddle. Now updated with cross browser compatability measures and accessing data dynamically.