0

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.

  • This is for creating email content. – Gir Aug 10 '15 at 20:41
3

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
  • is there a way to make your homeInfo and workInfo to use data objects instead? (changing source to object doesn't work) – Noor Alam Aug 7 at 18:00
0

I solved this using server side javascript to extract the nested JSON objects and place them into their own AMPScript variables. Once there, I could use dataobject tags. E.g.

<script runat=server>
function set(key, val) {
  var newVal = val != null ? Stringify(val) : "{}";
  Variable.SetValue(key, newVal);
}
    Platform.Load("Core","1");
    var Json=Variable.GetValue("@Json2");
    var _JsonObject = eval('(' + Json + ')');
    set("@WorkInfo", _JsonObject.workInfo);
    set("@HomeInfo", _JsonObject.homeInfo);
</script>

Then to access them, first declare the dataobject WorkInfoVar

{{.dataobject WorkInfoVar type=variable source=@WorkInfo maxrows=20}}
     {{.data}} 
          {"target": "@WorkInfo"} 
     {{/data}}
{{/dataobject}}

And then use it

{{#WorkInfoVar}} {{emailaddress}} {{/WorkInfoVar}} 

Ditto for HomeInfo.

References: Sharing Variables between Javascript and AMPScript: http://help.exacttarget.com/en/documentation/exacttarget/content/server_side_javascript/using_server_side_javascript_with_ampscript/

  • One more note. Based on my tests and online research, it appears that as of aug 2015, there is no way of accessing nested objects. Hence the need to put the subobjects into their own AMPScript variables. – Gir Aug 11 '15 at 1:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.