La fonction de suggestion automatique permet d'affiner rapidement votre recherche en suggérant des correspondances possibles au fur et à mesure de la frappe.
I have a contact property that has a json string as value. When I fetch the value with the api, and paste it in my template, assigned to a variable, the variable neatly passes a fromjson filter and I can transform the json into a structure I can then use in the template.
when the property is collected in the template, it fails to be transformed to json: contact[property]|fromjson returns an empty result.
when I print the contact[property] and the variable with the json value from the api, both strings are exactly the same.
{% set blob = "[{\"test 1\":\"value 1\",\"test 2\":\"value 2\"}]" %}
{% set info = contact['easy_calculator_result']|fromjson %}
{# set info = blob|fromjson #}
<ul>
<li>{{ contact['easy_calculator_result']|pprint }}</li>
<li>{{ blob|pprint }}</li>
<li>{{ contact['easy_calculator_result']|fromjson}}</li>
<li>{{ blob|fromjson}}</li>
</ul>
So there you see both values are pprinted exactly the same. The one from the contact is not valid json. Even a trim before fromjson does not help. What other cleanup would be possible to make this parseable json?
Ah, I see it now. So if your contact property does indeed contain this value:
[{"test 1":"value 1","test 2":"value 2"}]
That is an array of data, so you would need to do info[0]['test 1'] for it to output the value.
Here is an output from a test module I created containing a contact with a multi-line text property containing your JSON value.
After using info[0]['test 1'], I get this.
I am not sure why your {{ info }} is not working in a similar way. Perhaps it needs to be in a multi-line text property rather than a single-line property.
info is indeed not in the ouput, it was a left over. In fact that assignment is what is being printed in the third list item, which results in no output.
The actual value set in the property is the json string that is also copy pasted in the assignment of 'blob', except for the quote escapes. So it is
[{"test 1":"value 1","test 2":"value 2"}]
to be exact.
for sake of clarity, I adjusted the template code:
template:
{% set blob = "[{\"test 1\":\"value 1\",\"test 2\":\"value 2\"}]" %}
{% set info = contact['easy_calculator_result']|fromjson %}
{# set info = blob|fromjson #}
<ul>
<li>{{ contact['easy__calculater_result_html']|pprint }}</li>
<li>{{ blob|pprint }}</li>
<li>{{ contact['easy__calculater_result_html']|safe|fromjson}}</li>
<li>{{ blob|fromjson}}</li>
<li>{{ info }}</li>
<li>{{ info['test 1'] }}</li>
</ul>
Hi, again, my bad, i copied it from an older open tab I had. Just to be sure, updated the template again with my original case, like the following (with the output after that, problem unfortunately remains)
{% set blob = "[{\"test 1\":\"value 1\",\"test 2\":\"value 2\"}]" %}
{% set info = contact['easy_calculator_result']|fromjson %}
{# set info = blob|fromjson #}
<ul>
<li>{{ contact['easy_calculator_result']|pprint }}</li>
<li>{{ blob|pprint }}</li>
<li>{{ contact['easy_calculator_result']|safe|fromjson}}</li>
<li>{{ blob|fromjson}}</li>
<li>{{ info }}</li>
<li>{{ info['test 1'] }}</li>
</ul>
Ah, I see it now. So if your contact property does indeed contain this value:
[{"test 1":"value 1","test 2":"value 2"}]
That is an array of data, so you would need to do info[0]['test 1'] for it to output the value.
Here is an output from a test module I created containing a contact with a multi-line text property containing your JSON value.
After using info[0]['test 1'], I get this.
I am not sure why your {{ info }} is not working in a similar way. Perhaps it needs to be in a multi-line text property rather than a single-line property.
I tried both single and multiline property field types, without any difference. Curious though that you get something for info[0]['test 1'], that still returns nothing.
Can you confirm you are using the fromjson filter on the contact property? It is there that it goes wrong in my case: as soon as I use that filter, the result is void.
I've let this rest a bit to see if fresh looks in a new year would help me 🙂
I see now the difference between your code and mine.
You use a text field on a module, and I can confirm this works. I use a text field on a contact. And it seems HS is doing some kind of 'protection' of that, or formatting, such that the fromjson filter does not work when it is applied to a contact property.
So that clarifies the reason why it works for you and not for me, but I would not mark this puzzle as solved. Someone from HS should look into this and document why this is different.
I would check that there isn't any whitespace before or after your property value as well. Perhaps ping it with an API request to see the true HubSpot database value.