CMS Development

PDeT
Participant

cleaning up string with json

SOLVE

hi,

 

I found a new puzzle today.

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>

 

This renders:

<ul>
  <li>(String: [{"test 1":"value 1","test 2":"value 2"}])</li>
  <li>(String: [{"test 1":"value 1","test 2":"value 2"}])</li>
  <li></li>
  <li>[{test 1=value 1, test 2=value 2}]</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?

 

thanks for any pointers or feedback.

 

Pieter

0 Upvotes
1 Accepted solution
tominal
Solution
Guide | Partner
Guide | Partner

cleaning up string with json

SOLVE

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.

 

tominal_1-1670448644158.png

After using info[0]['test 1'], I get this.

 

tominal_2-1670448713335.png

 

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.

 

Hope that helps!


Thomas Johnson
Community Champion


Kahu Software LLC
A Texan HubSpot consulting firm
https://kahusoftware.com

View solution in original post

0 Upvotes
11 Replies 11
Jaycee_Lewis
Community Manager
Community Manager

cleaning up string with json

SOLVE

Hi, @PDeT 👋 Thanks for your question. Hey, @tominal @JBeatty @taran42, do you have any tips for @PDeT and their puzzle 😊

 

Best,

Jaycee

linkedin

Jaycee Lewis

Developer Community Manager

Community | HubSpot

0 Upvotes
tominal
Guide | Partner
Guide | Partner

cleaning up string with json

SOLVE

Hey @PDeT,

 

Have you tried printing out the values of your "info" variable? You're setting it but I didn't see an output there.

{{ info }}

 

Also, do you get anything if you try this? Hopefully that will get you something if the JSON is the same in the contact property.

{{ info['test 1'] }} 

 

Another also, what is the exact value of your contact property so we can also try to reproduce this?


Thomas Johnson
Community Champion


Kahu Software LLC
A Texan HubSpot consulting firm
https://kahusoftware.com
0 Upvotes
PDeT
Participant

cleaning up string with json

SOLVE

Hi, thx for reaching out !

 

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>

 

<ul>
  <li style="mso-line-height-rule:exactly">(String: [{"test 1":"value 1","test 2":"value 2"}])</li>
  <li style="mso-line-height-rule:exactly">(String: [{"test 1":"value 1","test 2":"value 2"}])</li>
  <li style="mso-line-height-rule:exactly"></li>
  <li style="mso-line-height-rule:exactly">[{test 1=value 1, test 2=value 2}]</li>
  <li style="mso-line-height-rule:exactly"></li>
  <li style="mso-line-height-rule:exactly"></li>
</ul>

 

 

 

0 Upvotes
tominal
Guide | Partner
Guide | Partner

cleaning up string with json

SOLVE

I just noticed this:

tominal_0-1670444297924.png

 

In your code you are setting the info variable to your contact property with one underscore. Does changing that affect anything?


Thomas Johnson
Community Champion


Kahu Software LLC
A Texan HubSpot consulting firm
https://kahusoftware.com
0 Upvotes
PDeT
Participant

cleaning up string with json

SOLVE

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>

 

<ul>
  <li style="mso-line-height-rule:exactly">(String: [{"test 1":"value 1","test 2":"value 2"}])</li>
  <li style="mso-line-height-rule:exactly">(String: [{"test 1":"value 1","test 2":"value 2"}])</li>
  <li style="mso-line-height-rule:exactly"></li>
  <li style="mso-line-height-rule:exactly">[{test 1=value 1, test 2=value 2}]</li>
  <li style="mso-line-height-rule:exactly"></li>
  <li style="mso-line-height-rule:exactly"></li>
</ul>

 

 

Much appreciated that you are taking the time to look into this!

 

Kind regards

 

Pieter

 

0 Upvotes
tominal
Solution
Guide | Partner
Guide | Partner

cleaning up string with json

SOLVE

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.

 

tominal_1-1670448644158.png

After using info[0]['test 1'], I get this.

 

tominal_2-1670448713335.png

 

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.

 

Hope that helps!


Thomas Johnson
Community Champion


Kahu Software LLC
A Texan HubSpot consulting firm
https://kahusoftware.com
0 Upvotes
PDeT
Participant

cleaning up string with json

SOLVE

Hi @tominal ,

 

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.

 

Pieter

0 Upvotes
tominal
Guide | Partner
Guide | Partner

cleaning up string with json

SOLVE

Hmm I'm not too sure why it won't work. HubL should be the same everywhere. Here is my code that I used in a module.

 

tominal_0-1670462499999.png

 

Hope that helps!


Thomas Johnson
Community Champion


Kahu Software LLC
A Texan HubSpot consulting firm
https://kahusoftware.com
PDeT
Participant

cleaning up string with json

SOLVE

Hi Kahu,

 

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.

 

Kind regards & happy new year  🙂

 

Pieter

 

PDeT
Participant

cleaning up string with json

SOLVE

@tominal, could you tell how you filled the property as well? Because maybe the problem lies there... as I can't get your example to work.

 

thx

0 Upvotes
tominal
Guide | Partner
Guide | Partner

cleaning up string with json

SOLVE

I filled it with exactly the following:

 

[{"test 1":"value 1","test 2":"value 2"}]

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.


Thomas Johnson
Community Champion


Kahu Software LLC
A Texan HubSpot consulting firm
https://kahusoftware.com
0 Upvotes