[SalesForce] How to access nested JSON (sub-objects) in data

I'm using the dataobject tag to parse JSON that is in a variable as described by:

https://help.exacttarget.com/en-US/documentation/guide_template_language/parsing_json/dataobject_tag_examples/

How do I access nested objects?


%%[
 var @Json2
 set @Json2 = ' { "workInfo" : {"emailaddress":"sam@example.com ","Region":"East","State":"North Carolina","City":"Charlotte" }, "homeInfo" : { "emailaddress":"same@home.com", "Region":"East", "State":"North C", "City": "Charlotte" } }' 
]%%
{{.dataobject JsonVar2 type=variable source=@Json2 maxrows=20}}
     {{.data}} 
          {"target": "@Json2"} 
     {{/data}}
{{/dataobject}}
{{#jsonvar2}}
work info : {{workInfo.emailaddress}}

{{/jsonvar2}}

I tried using {{workInfo.emailaddress}} but was not successful.

Best Answer

As part of the January 2016 release SFMC added a "nested" datasource type that can be used to extract nested data objects as you have done here with SSJS. The syntax is a little clunky as it requires you to create a new datasource for each layer of the JSON data that you access. Here is an example of how to use GTL to extract the data from your sample:

%%[
var @Json2
set @Json2 = ' { "workInfo" : {"emailaddress":"sam@example.com ","Region":"East","State":"North Carolina","City":"Charlotte" }, "homeInfo" : { "emailaddress":"same@home.com", "Region":"East", "State":"North C", "City": "Charlotte" } }'
]%%

{{.datasource JsonVar2 type=variable source=@Json2 maxrows=20}}
{{.data}}
{"target": "@Json2"}
{{/data}}

    {{.datasource workInfo type=nested maxRows = 20}}
    {{.data}}
    { "target" : "JsonVar2.workInfo" }
    {{/data}}
        work info : {{emailaddress}}<br>
    {{/datasource}}

    {{.datasource homeInfo type=nested maxRows = 20}}
    {{.data}}
    { "target" : "JsonVar2.homeInfo" }
    {{/data}}
        home info : {{emailaddress}}<br>
    {{/datasource}}

{{/datasource}}

And yields the following output:

work info : sam@example.com 
home info : same@home.com
Related Topic