Search code examples
json2html

json2html, calling JSON data in an array


I'm using json2html and trying to figure out the correct syntax for calling JSON data within an array:

{ biographicData: [
    {
        firstName: 'John',
        lastName: 'Doe',
        birthDate: '10/15/1983',
        email: '[email protected]',
        workPhone: '678-901-2345',
        mobilePhone: '098-765-4321',
        homePhone: '123-456-7890'
    }
]}

In other cases, I've used something like {"tag":"div","html":"${biographicData.firstName}"} to get the values, but that doesn't seem to work when the data is in an array. What do I need to do to fix this call?


Solution

  • To access array data you can do something like this if you know the position of the array you are trying to access

    {"tag":"div","html":"${biographicData.0.firstName}"}
    

    or you could transform the entire array (if there are multiple elements) using an inline function and a transform

    {"tag":"div","children":function(){
       return( json2html.transform(this,bioDataTransform) );
    }}