0

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":"ta@gmail.com"
   }
}

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?

4

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}}
  • Your code works! Thank you! But #each and #with still give me an error How can loop through my json? – MrViSiOn Sep 26 '17 at 15:37
  • I did this work perfectly... thanks for your answer, it inspired me for getting my goal. – MrViSiOn Sep 26 '17 at 15:51
  • no worries. glad i could help. You can only use #each with {{dataobject}} in the arrays in json. – Brad Sapkota Sep 26 '17 at 23:30

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.