2

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'r
  • 32,482
  • 7
  • 61
  • 65
user971956
  • 2,800
  • 5
  • 25
  • 43
  • Will `198` be the only key in that level of the hierarchy? – Dominic Barnes Dec 16 '11 at 18:04
  • Dominic, No. But I do want to access always the first key. – user971956 Dec 16 '11 at 18:05
  • @user971956: While the keys in JSON might have an order, they are not ordered when converted to a JavaScript object, so you cannot access "the first property". Btw. your question has actually nothing to do with JSON, but with JavaScript objects. – Felix Kling Dec 16 '11 at 18:08
  • 1
    possible duplicate of [Access the first property of an object](http://stackoverflow.com/questions/983267/access-the-first-property-of-an-object) – Felix Kling Dec 16 '11 at 18:10
  • This is a really elegant little solution: http://stackoverflow.com/a/983298/231995 – graphicdivine Dec 16 '11 at 18:38

4 Answers4

5

A few lines will do it:

var obj = JSON.parse(your_json);
var lines = obj.Files.Lines;
var keys = Object.keys(lines);
var keyICareAbout = keys[0];
var info  = lines[keyICareAbout];

Note: This solution relies on some newer javascript features. For JSON to work in all browsers, you will need Douglas Crockford's json2.js from here. For Object.keys to work in all browsers, use this shim from Mozilla's JS docuentation:

if(!Object.keys) Object.keys = function(o){
 if (o !== Object(o))
      throw new TypeError('Object.keys called on non-object');
 var ret=[],p;
 for(p in o) if(Object.prototype.hasOwnProperty.call(o,p)) ret.push(p);
 return ret;
}

Edit: Try this jsfiddle. Now updated with cross browser compatability measures and accessing data dynamically.

benekastah
  • 5,421
  • 1
  • 31
  • 46
1

Unless you decide to specifically use an Array at the Lines level, there is no way to guarantee what order they will come out it.

Using for..in or Object.keys, you can enumerate the properties, and just exit out of the loop on the first iteration. But again, there is no guarantee what order they will come out in, it depends upon the implementation. (although many times, it'll be what you expect)

Dominic Barnes
  • 26,689
  • 7
  • 62
  • 88
1

If you don't know what the key will be, it might be useful to return your data as an array:

{
    "Files": {
        "Lines": [{
            "id": "198",
            "firstline": "sometext"}]
    }
}

var schema = JSON.parse(fileContents); 
console.log(schema.Files.Lines[0].firstline); //sometext
Dennis
  • 30,518
  • 9
  • 59
  • 75
1

Just do this:

schema.Files.Lines.properties[0].firstline

Live demo: http://jsfiddle.net/KBj2G/

(Yes, the 'properties' getter has to be implemented manually... )

Šime Vidas
  • 163,768
  • 59
  • 261
  • 366