Hello Everyone,
We are sperimenting a strange behaviour: we have a custom property on Hubspost (multitext) where a JSON string is saved.
In an email template, via Hubl we would like to retrieve that value, make it fromJson and then use the properties of the json in the template.
Below you can see an example of code, in the first piece of code we try to read the value of the custom field “ds_domains”, but the fromJson filter returns a NULL.
In the second piece of code we statically reported the value of the ds_domains property and in that case it works correctly.
What are we doing wrong?
# Reading json string value from a custom property called ds_domains
{% set ds_json = contact.ds_domains %}
{% set obj = ds_json|fromjson %}
Json STR: {{ ds_json|pprint }} --> {"product pills": ["test123.com", "test123.it"]} "OK"
Json OBJ: {{ obj | pprint }} --> NULL (ERROR)
# Test with static value
{% set ds_json_2 = '{"product pills": ["test123.com", "test123.it"]}' %} (Static value of contact.ds_domains)
{% set obj2 = ds_json_2|fromjson %}
Json STR2: {{ ds_json_2|pprint }}
Json OBJ2: {{ obj2['product pills'] }} --> WORK
The preview result is:
Hey, @tommaso1288
Did you get this worked out?
If not, have you tried using the HubL filters trim and striptags to check the formatting for the JSON string you’re storing in the ds_domains property?
Also, have you double-checked your escaping of slashes? Making sure to use double backslashes (\\) in places where you would normally use one (\)
Best,
Jaycee
Hi Jaycee,
thank you for your kind reply, yes I tried both trim and the various filters for json or slash escaping.
Finally, reading the hubl documentation I found this:
“you can apply HubL filters to personalization tokens, such as contact and company tokens, on HubSpot CMS and blog pages, but not in emails.”
In fact, I tried using the split filter instead of fromjson on a test string (a_b_c) read from a custom property and noticed that the split is done not on the value of the custom property but on its name.
Example:
{% set ds_domains = contact.ds_domains %}
{% set names = ds_domains | split("_") %}
{{ names }}
names → [contact.ds, domains] and not [a, b, c]
Probably the fromJson filter does the same thing, it applies it to the name of the custom property and not to its value and therefore (not being a valid json) it returns a null.