0

I have this json named test_json and I need to get all the authors out of it.

{
    name:'test',
    content:
    {
        "dynamicVar1":
        {
            "author":'bla'
        },
        "also-DynamicButOther-123-Name":
        {
            "author":'bla2'
        }
    }
}

The problem is that since "dynamicVar1" and "also-DynamicButOther-123-Name" have changing names - I can't access author.

I've tried this, but it doesn't work and I don't know where to go from here.

for (let x in test_jason.content){
    console.log(test_jason.content.x.author) //undefined
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
neoislife
  • 69
  • 8
  • 1
    JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. (If it *were* JSON, it would be invalid JSON. Property names must be in quotes.) – T.J. Crowder Sep 12 '17 at 11:02
  • 1
    `console.log(test_json.content[x].author)`, see the linked question's answers for why. (Note also the typo fix.) – T.J. Crowder Sep 12 '17 at 11:04
  • perfect it works - I understand. Much thanks for the answer! – neoislife Sep 12 '17 at 11:09

1 Answers1

0

var obj = {
  name:'test',
  content:
  {
    "dynamicVar1":
    {
        "author":'bla'
    },
    "also-DynamicButOther-123-Name":
    {
        "author":'bla2'
    }
  }
}

for (let x in obj.content){
  console.log(obj.content[x].author)
}
Jurij Jazdanov
  • 1,248
  • 8
  • 11
  • We don't need **yet another** answer to this question. – T.J. Crowder Sep 12 '17 at 11:13
  • We have only 1 answer. Comment != answer – Jurij Jazdanov Sep 12 '17 at 11:14
  • No, we have probably a couple of dozen answers to this question. [Thirteen of them are here](https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable), on the earlier question this was marked as a duplicate of, but there are many others strewn about as well. This isn't a discussion forum, we don't constantly re-answer the same question over and over. Instead, we point to the answers that already exist. – T.J. Crowder Sep 12 '17 at 11:15