1

If I have this json:

{
    "name":"Tania",
    "invoice":{
      "city":"BCN",
      "zip_code":"1005",
      "amount":"56.13",
      "datetime":"05/09/2017",
      "address":"Calle",
      "name":"Tania  ",
      "invoice_code":"PF2017-259224",
      "email":"[email protected]"
   }
}

How could I get properties of invoice element?

I'm doing this in a cloudpage

%%{={{ }}=}%%

{{.dataobject JSONVar type=variable source=@json}}
    {{.data}}
        { "target" : "@json" }
    {{/data}}
{{/dataobject}}

{{#jsonvar}}
    Order name: {{ name }}

    {{#with invoice}}
        {{ email }}<br>
        {{ name }} <br>
        {{ address }} {{ zip_code }}  {{ city }}<br>
        Factura nº #<strong>{{ invoice_code }}</strong><br>
    {{/with}}

{{/jsonvar}}

But it fails in #with... when I remove that lines #with block it returns Order name: Tania, but I can't get sub-object invoice and its properties

EDIT: If I do only this: it doesn't fails but It does not print the data.

{{ JSONVar.invoice.email }}<br>
{{ JSONVar.invoice.name }} <br>
{{ JSONVar.invoice.address }} {{ JSONVar.invoice.zip_code }} {{ JSONVar.invoice.city }}<br>
Factura nº #<strong>{{ JSONVar.invoice.invoice_code }}</strong><br>

How can I do that?

1 Answer 1

6

In your situation I would try using the {{datasource}} within GTL. Please note in mind that documentation in GTL is not widely accurate, for example around the use of Block In helpers where {{@index}}, {{@first}} and others aren't functional. However if you have a simple structured JSON data then GTL is the best solution as it saves the time from using the SSJS.

Also if you notice in the code below I have used the datasource tag which uses a datasource defined inline as part of the template.

Please try the code below which works from my end:

%%{={{ }}=}%%

{{.datasource JSONVar source=@json type=variable}}
    {{.data}}
        { "target" : "@json" }
    {{/data}}

    Order name: {{ name }}

    {{.datasource invoice source=@json type=nested}}
        {{.data}}
          { "target" : "JSONVar.invoice" }
        {{/data}}

        {{ email }}<br>
        {{ name }} <br>
        {{ address }} {{ zip_code }}  {{ city }}<br>
        Factura nº #<strong>{{ invoice_code }}</strong><br>
    {{/datasource}}
{{/datasource}}
5
  • Your code works! Thank you! But #each and #with still give me an error How can loop through my json?
    – MrViSiOn
    Commented Sep 26, 2017 at 15:37
  • I did this work perfectly... thanks for your answer, it inspired me for getting my goal.
    – MrViSiOn
    Commented Sep 26, 2017 at 15:51
  • 1
    no worries. glad i could help. You can only use #each with {{dataobject}} in the arrays in json. Commented Sep 26, 2017 at 23:30
  • Can't wait to try this out, but I'm wondering what %%{={{ }}=}%% at the top of the script is meant to do. I've never seen this artefact. Commented Sep 10, 2020 at 14:15
  • Ah, found the answer to my question in the comment above: it's a method to set delimiters so that GTL may be used in CloudPages: salesforce.stackexchange.com/questions/129057/… Commented Sep 10, 2020 at 14:45

You must log in to answer this question.

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