CRM and CX Blogs by SAP
Stay up-to-date on the latest developments and product news about intelligent customer experience and CRM technologies through blog posts from SAP experts.
Showing results for 
Search instead for 
Did you mean: 
In our third blog on using placeholders with the Emarsys API, we will look at using nested JSON objects.

Use case

For nested JSON objects, it’s hard to provide a plausible real life scenario because it depends a great deal on the setup of your website. You could argue that there is never a good reason to use nested objects, but if your website is built using JavaScript with a lot of nested objects, then it’ll automatically result in nested objects inside the JSON. So it’s worth knowing about it.

We'll look at a shop selling guitars, with both the customer information and the products nested inside the global object. And we'll throw in a foreach loop for good measure.

First let's look at the HTML:
<title>API Exercise 3 Example</title>
Exercise 3 - Nested Objects
Please find your order details below:
<li>First Name: {{}}</li>
<li>Last Name: {{}}</li>
<li>Membership Number: {{}}</li>
<li>House Number: {{}}</li>
<li>Street Name: {{}}</li>
<li>County: {{}}</li>
<li>Country: {{}}</li>
{% foreach record in %}
<li>Product Name: {{record.productName}} </li>
<li>Brand: {{record.brandName}}</li>
<li>Description: {{record.Description}}</li>
<li>Price: {{record.Price}}</li>
<img src="{{record.imageUrl}}" />
{% endforeach %}

We have two sections in this HTML.

  • The first section describes the user's personal details such as the first name, last name, membership number, etc. This is not an array or a loop, because the user will only have this information once.

  • The second section uses a foreach loop to loop through an array, to display the items purchased in this order. 

The nesting comes in the - you can see that in the first section which has the personal information, there is a sub-object called objects, which is part of the global object. 

The second part uses the same global object as the first part, but it has a sub-object called productName. 

We can nest as deep and as much as we want, using dot notation to separate each level. However, it is not common to see more than two levels deep, as things can get very unreadable very quickly inside the JSON.

Let's have a look at the JSON for this example:
"key_id": 3,
"external_id": "",
"firstName": "Elston",
"lastName": "Gunn",
"membershipNumber": "82364",
"houseNumber": 66,
"streetName": "Albert Street",
"County": "Kent",
"Country": "United Kingdom"
"productName": "Fender American",
"brandName": "Fender",
"Description": "A Fender American Original guitar",
"Price": "€1200,-",
"imageUrl": ""
"productName": "Gibson Les Paul Original",
"brandName": "Gibson",
"Description": "A Gibson Les Paul Guitar",
"Price": "€2000,-",
"imageUrl": ""
"productName": "PRS Original",
"brandName": "Paul Reed Smith",
"Description": "An authentic Paul Reed Smith guitar",
"Price": "$3195,-",
"imageUrl": ""

So inside the global object, we have the objects object AND we have the products object. In the HTML, this is defined with dot notation. Each dot indicates one level of nesting deeper. 

When we trigger this campaign, it gives the following result in an email: 

And that’s it – I hope you found it useful!