Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get nested JSON keys only

Example JSON

{
  "one": [{
    "A": {"name":"example"},
    "B": {}
  }],
  "two": [{
    "A": {"name":"booger"},
    "B": {}
  }]
}

Using this I can get keys "one" and "two" to be listed.

$.getJSON('blahblah.js', function(data){
  $.each(data, function(key){
    console.log(key);
  });
});

But using this, I can’t get "A" and "B" to be listed.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$.getJSON('blahblah.js', function(data){
  $.each(data.one, function(key){
    console.log(key);
  });
});

Why not? And how do I get those inner keys(is this the correct term?) to be listed.
Thanks

>Solution :

So…not sure what you’re seeing then…

var jsontext_withbrackets =
  '{"one":[{"A":{"name": "example"},"B":{}}],\
  "two":[{"A":{"name": "booger" },"B":{}}]}';

var obj1 = JSON.parse(jsontext_withbrackets);

var jsontext_withoutbrackets =
  '{"one":{"A":{"name": "example"},"B":{}},\
  "two":{"A":{"name": "booger" },"B":{}} }';

var obj2 = JSON.parse(jsontext_withoutbrackets);

console.log(obj1.one[0]);
console.log(obj2.one);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading