Hello,
I’m encountering an issue with the ‘fromjson’ filter when attempting to work with nested objects in programmable emails.
Specifically, I’m using the ‘fromjson’ filter to parse a string property from a contact. The goal is to convert it into a JSON object and then iterate over an array within this object. However, I’ve run into a specific challenge when it comes to nested objects in the resulting JSON.
Below is an example code snippet that illustrates the issue:
<p>With user data (not working)</p>{% set data = contact.json_test|fromjson %}{{ data }}{% for item in data %}<p>name: {{ loop.index }} {{ item.name }}</p>{% endfor %}<p>With static data (works)</p>{% set test = '[{"name":"test","object":{"name":"another test"}}]'|fromjson %}{{ test }}{% for item in test %}<p>name: {{ loop.index }} {{ item.name }}</p>{% endfor %}
The value of the contact’s property I’m testing
property in contact
The result
result
I have tried addressing this problem by:
- using the ‘escapejson’ filter
- escaping the nested curly braces with ‘{{’ and ‘\{’
But so far, I haven’t had success.
Could you please provide assistance in resolving this issue?
I appreciate any guidance or suggestions you can offer.
Hi @JuliaVallina,
Thanks for reaching out to the Community!
Let’s seek guidance from a few experts:
Hello @albertsg, @Jnix284, @GRajput - Could you please share any insights or suggestions for @JuliaVallina?
The time and expertise you contribute are truly valued!
Thanks,
Diana
Programmable emails have a couple of known bugs, and this is one of them!
HTML that is included in a contact property will be escaped and rendered as a string instead of HTML. This is due to escaping being enabled by default within the new renderer. To render HTML in contact properties use the safe filter.
{{ contact.property_with_html | safe }}
Give that a try and let me know if that doesn’t do the trick!
Thanks for your answer.
I’m not sure what I am suppose to do with the `safe` filter.
There is no HTML in my object, just nested objects.
I tried to chain it here and nothing happens:
{% set data = contact.json_test|fromjson|safe %} // not working{% set data = contact.json_test|safe|fromjson %} // not working either
Hi Julia - having the same issue. Did you ever figure this one out?
Reading through the Slack, it seems like some people have been able to get this to work, but I haven’t been able to.
Example of someone getting it working: Slack
Hi Ben,
No, I couldn’t get it to work. Luckily, I had control over the JSON object in I wanted to manipulate, and I could transform it into a single-level object.
So, instead of creating a JSON like this in my contact property:
{% set test = '[{"name":"test","object":{"name":"another test"}}]'|fromjson %}
I went for:
{% set test = '[{"name":"test","object_name":"another test", "object_other": [1,2,3]}]'|fromjson %}
Oh, ok, nice. Glad you were able to get it to work. I eventually got it working, as well. I was trying to use a custom-coded DND template, but had to use a regular DND template instead.