Search code examples
javascripthtmljson2html

Access nested JSON in json2html


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.


Solution

  • "${nestedObject.nestedField1}"
    

    This should do the trick.

    Just like how you'd access it in JS:

    jsonData.nestedObject.nestedField1