JSON

Using JSON content allows you to store multiple pieces of data under one field, this is useful as it can be different for each contact and, as it is stored on Adestra, we don't need to worry about the remote connection failing (as is possible when using XML).

On this page:

Processing JSON Content

There are two methods you can use with JSON: valid_data and data.

json.valid_data

bool = json.valid_data(fieldname)

This method accepts a fieldname and will return a boolean value denoting whether the JSON is valid or not. This can be used to prevent errors in situations where you may have missing or invalid data for some contacts.

For example:

Copy
[* IF json.valid_data('data_source') *]
    Dear [* json.data('data_source').first_name *] [* json.data('data_source').surname *],
[* ELSE *]
    Dear Customer,
[* END *]

json.data

data_structure = json.data(fieldname)

This method accepts the name of the table field containing JSON and returns a decoded data structure. For example:

Copy
Dear [* json.data('data_source').first_name *] [* json.data('data_source').surname *],

This method caches decoded JSON, so it's safe to call it multiple times without a negative performance impact.

Examples

As an illustrative example, here is some JSON representing a recent browser session:

Browser Sessions

Copy
{
"title":"Mr",
"forename":"John",
"surname":"Smith",
"job_title":"copywriter",
"suggested_url":"http://example.com/new_bikes",
"viewed_items":[
    {
      "product":"Road Bike",
      "price":"399.99"    },
    {   
      "product":"Tour Bike",
      "price":"435.00"    }
  ]
}

Using the above JSON, a campaign might look like this:

Copy
Dear [* json.data('browser_session').title *] [* json.data('browser_session').surname *],

We notice that you were browsing our website and viewed these:

[* FOREACH item IN json.data('browser_session').viewed_items *]
    [* item.product *] (�[* item.price *])
[* END *]

So we think you might be interested in our new product. Click below for more information:

[* link.track_url(json.data('browser_session').suggested_url, 'Primary CTA') *]

Happy hunting,

CycleHero

Using Variables

If you're referencing JSON data frequently, the resulting code can get complicated. You can simplify this by assigning data structures to variables. For example:

Copy
[* c = json.data('contact_info') *]
Dear [* c.first_name *] [* c.last_name *]

The browser session example above could be made more readable using this technique:

Copy
[* browser = json.data('browser_session') *]
Dear [* browser.title *][* browser.surname *]

We notice that you were browsing out website and viewed these:

[* FOREACH product IN browser.viewed_items *]
 - [* product.name *] (�[* product.price *])
[* END *]

So we think you might be interested in our new product. Click below
for more information:
[* link.track_url(browser.target_url, 'Primary CTA') *]

Happy hunting,

CycleHero

Caution: Do not use 'data', 'json' or 'link,' etc., as variable names; otherwise, you will overwrite the relevant functions.

Here's another example JSON and matching template using this technique:

Copy
{
"title":"Ms",
"first_name":"Jane",
"surname":"Doe",
"magazine_subscriptions":[
    {
      "title":"FootballMatch",
      "edition":72,
      "store":"WG Smiths"    },
    {
      "title":"O3 Academy Gigs",
      "edition":42, 
      "store":"O3 Academy Store"    },
    {
      "title":"Cocktail Master",
      "edition":11,
      "store":"Waterrocks"    }
  ]
}

 

Copy
[* subscription = json.data('magazine_subscriptions') *]

Dear [* subscription.title *][* subscription.surname *]

Did you know you can pick up your latest subscriptions?

[* FOREACH magazine IN subscription.magazine_subscriptions *]
  Get edition #[* magazine.edition *] of [* magazine.title *] at [* magazine.store *] now!
[* END *]

Happy reading, 

Subscription King