2

Working on parsing some JSON in the HTML of a Marketing Cloud email using GTL and AMPscript and I've figured out how to do one degree of nesting, but can't figure out how to dig down deeper than that. This is a piece of the sample JSON:

   {
"section": {
    "id": "ABCDEFG",
    "heading": {
        "name": "John Smith"}}
    }

I can get it to pull "id", but not "name". What I used to successfully get to "id":

%%[
SET @title="TestTitle"
SET @JSON=Lookup("TestJSON","JSON", "Title", @title)
]%%  

{{.datasource JsonVar type=variable source=@Json maxrows=20}}
{{.data}}
{"target": "@Json"}
{{/data}}
    {{.datasource section type=nested source=@Json maxrows=20}}
 {{.data}}
      {"target": "JsonVar.section" }
  {{/data}}

%%[
SET @id = TreatAsContent('{{section.id}}')
]%%

  {{/datasource}} 
      {{/datasource}}

That works great, but when I try to reach further down to "name", I can't find a way to make that work. I've tried several AMPscript based things, and also tried adjusting to {"target": "JsonVar.section.heading" } and TreatAsContent('{{section.heading.name}}') but those didn't work. Is there a way to set up another layer of nesting within the above?

1
  • If you do this : %%[ SET @heading = TreatAsContent('{{section.heading}}') ]%% %%=v(@heading)=%% The result is 'JSON Object'. If you try to parse that again, may be you should get there. Good Luck!. Commented Oct 31, 2020 at 11:21

1 Answer 1

2

You are on the right track, but you shouldn't include a source attribute for nested objects.

The following code will work. As your key names are unique, it's not required to express them in dot notation in your TreatAsContent argument.

%%[
var @json
set @json= '{
    "section": {
    "id": "ABCDEFG",
    "heading": {
        "name": "John Smith"}
    }
}'
]%%  

{{.datasource obj type=variable source=@json}}
  {{.data}}
    {"target": "@json"}
  {{/data}}
    {{.datasource section type=nested}}
     {{.data}}
        {"target": "obj.section" }
      {{/data}}
      {{.datasource section type=nested}}
         {{.data}}
            {"target": "section.heading" }
          {{/data}}

%%[
var @id, @name
set @id = TreatAsContent('{{id}}')
set @name = TreatAsContent('{{name}}')
OutputLine(Concat(@id))
OutputLine(Concat(@name))
]%%

     {{/datasource}} 
  {{/datasource}}
{{/datasource}} 

Update

Here's an additional example of deeper object nesting (based on a comment to my answer). Note, you don't need to use TreatAsContent to set values as variables if you just want to display the values inline.

%%[
var @json
set @json=   '{
   "section":{
      "id":"ABCDEFG",
      "heading":{
         "name":"John Smith",
         "address":{
            "billingAddress":{
               "street":"3 Sample St",
               "city":"London",
               "country":"United Kingdom",
               "postcode":"E1 ABC"
            },
            "postalAddress":{
               "street":"3 Marketing Lane",
               "city":"London",
               "country":"United Kingdom",
               "postcode":"E2 DEF"
            }
         }
      }
   }
}'
]%%  

{{.datasource obj type=variable source=@json}}
  {{.data}}
    {"target": "@json"}
  {{/data}}
    {{.datasource section type=nested}}
     {{.data}}
        {"target": "obj.section" }
      {{/data}}
      {{.datasource heading type=nested}}
         {{.data}}
            {"target": "section.heading" }
          {{/data}}

          id: {{id}}
          name: {{name}}

         {{.datasource address type=nested}}
         {{.data}}
            {"target": "heading.address" }
          {{/data}}

             {{.datasource billingAddress type=nested}}
             {{.data}}
                {"target": "address.billingAddress" }
              {{/data}}

              street: {{street}}
              city: {{city}}
              country: {{country}}
              postcode: {{postcode}}

          {{/datasource}}

             {{.datasource postalAddress type=nested}}
             {{.data}}
                {"target": "address.postalAddress" }
              {{/data}}

              street: {{street}}
              city: {{city}}
              country: {{country}}
              postcode: {{postcode}}

          {{/datasource}} 
        {{/datasource}} 
     {{/datasource}} 
  {{/datasource}}
{{/datasource}} 
3
  • Thank you! This worked beautifully. Say there was another nested piece under "name", such as "location" -- how would I dig one layer more? I tried a few different combinations of things that didn't work, and would love to know how to reach multiple layers for future reference. Thanks again!
    – Marisa S
    Commented Nov 3, 2020 at 0:06
  • 1
    @MarisaS I've updated my answer to include an example of deeper nesting. Commented Nov 3, 2020 at 0:41
  • You are absolutely fantastic. Thank you so much!!
    – Marisa S
    Commented Nov 3, 2020 at 17:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .