3

I'm using json2html to create a report for a given JSON file. I was wondering what exactly the syntax is to access nested objects and their fields. For example,

var jsonData = {
    "field1": "value1",
    "field2": "value2",

    "nestedObject": {
        "nestedField1": "nestedValue1",
        "nestedField2": "nestedValue2"
    }
}

What's the syntax to access "nestedField1"? The transform that I'm using is,

var transform = [ 
                  {tag : "h1", html : "${field1}"},
                  {tag : "article", html : "${field2}"},
                  {tag : "article", html : "${nestedObject}" }
                ]

The last statement html : "${nestedObject}" returns [Object object] as expected. But, I can't seem to access its fields.

2 Answers2

1
"${nestedObject.nestedField1}"

This should do the trick.

Just like how you'd access it in JS:

jsonData.nestedObject.nestedField1
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • 2
    Thanks, I realized this relatively simple thing just a moment before you posted. –  Jul 20 '15 at 06:32
0

I should have tried everything that seemed intuitive.

The simple way is,

{ tag : "article", html : "the first nested field is ${nestedObject.nestedField1}" }