Search through more than a hundred articles on every aspect of User.com

Arkadiusz Wiśniewski
Written by Arkadiusz Wiśniewski

JSON Attributes Guide: How to Store and Use Them to Print Complex Data

Use JSON field format to store data in a simple and accessible way.


How to Use JSON Attributes

JSON attributes are available for all data models: user, event, company, deal, activity, and ticket. Even product events have their default JSON attribute “custom_data.” You can define them as an attribute type when defining a data model in the app settings.

It’s possible to put valid JSON as a global variable (text type) or any other string attribute, but you need to convert it to a JSON using the {% jsonify model.attribute_name as variable %} snippet to get a JSON on “variable.”

You can update JSON attribute values using: GUI (manual update), CSV import, REST API, and product feed. It’s not possible to update JSON attributes using automations.

JSON attributes have limited filtering options. You can filter only “if the attribute has any value or the value is unknown.” That’s why we use them to store data “for content rendering” not for “segmentation and filtering.”

How to Print the Data from JSON Attributes

As an example, we have a JSON object stored on a user’s attribute “cart_info”:

{
    "cart_id": 83883,
    "summary": {
        "total_value": 105,
        "discounted_value": 90,
        "active_promotions": [
            {
                "name": "Holiday Promotion",
                "discount_value": "-10% order value"
            },
            {
                "name": "Free delivery +100$",
                "discount_value": "free delivery for orders with total value > 100$"
            }
        ]
    },
    "products": [
        {
            "id": "bhd937",
            "name": "Tennis Ball",
            "price": 10,"qty": 3},
       {
            "id": "bhd937",
            "name": "Golf Ball",
            "price": 15,
            "qty": 5
        }
    ]
}

To print the key value, you need to refer to its name with a proper snippet:

{{ receiver.cart_info.cart_id }} = 83883

To refer to the nested value, you just need to add the next key:

{{ receiver.cart_info.summary.total_value }} = 105

As in the JSON format, we can use not only objects but also arrays. We can refer to a specific index of the array (print the name of the first active promotion, index = 0):

{{ receiver.cart_info.summary.active_promotions.0.name }} = Holiday Promotion

Or iterate through the array using the {% for %}{% endfor %} loop:

{% for promotion in receiver.cart_info.summary.active_promotions %}
{{ promotion.name }}: {{ promotion.discount_value }}{% endfor %}

Output:

Holiday Promotion: -10% order value

Free delivery +100$: free delivery for orders with total value > 100$

You can also refer to other data models using the JSON content. Let’s print the product data from the product feed (assuming that we have “name” and “category” in the feed, referring to the ids provided in the products array):

{% for item in receiver.cart_info.products %}{% products count=1 custom_id=item.id order_by='id' order=1 as product %}
{{ product.0.name }}
{{ product.0.category }}
{{ product.0.description }}
{% endfor %}

Output:

Tennis Ball PRO

tennis

The best tennis ball on the market

Golf Ball PRO

golf

The best golf ball on the market

Note: The names of the product are different than in the JSON. They're printed from a product feed.

How to Refer to the JSON Content Using Other Attribute Values

As an example, we have a JSON object stored on a product’s attribute “custom_prices”:

{
     "London": "10 000 £",
     "Warsaw": "20 000 zł",
     "Berlin": "30 000 €"
}

My customers have a predefined attribute “customer_city,” which can contain one of three values: London, Warsaw, Berlin.

In the abandoned cart email, I want to print the price according to the customer’s city, using the custom User.com tag {% get_attribute data_model attribute_name %}, referring to the JSON as our data model and:

{% product_events event_type='add to cart' for_last_days=1 count=3 unique=1 order=-1 as event_occurrences %}{% for event in event_occurrences %}
{{ event.name }}  your price: {% get_attribute event.custom_prices '{{receiver.customer_city}}' %}
{% endfor %}

Troubleshooting and FAQ

Not sure why the values of your JSON field are not rendered correctly?

  • You can check if your file is a valid JSON here: https://jsonlint.com

  • Your key name has a space inside? Even if your JSON object is valid, we can't process the value of this attribute.

  • Snippet tags for JSON keys are case-sensitive (uppercase letters are different from lowercase).

Categories:
Tags: