CMS Development

Richard_yellow
Participant

How to convert String to Dict using HUBL

I am working on an email module within the Design Manager using Hubl. The final email will contain invoice details for a customer.

 

The actual invoice details will be provided by an external system. The external system will create a ticket and place a Dictionary string in a custom property within the ticket (ticket.statementDict):

{"billto": "Acme Associates",
"lines" : [
{"Description":"Item1 20 Sep - 30 Sep 2019","Price":"33.00"},
{"Description":"Item2 20 Sep - 30 Sep 2019","Price":"7.33"}]
}

 

My Problem is that the retrieved property is a "str" type. I need to convert the "str" to a "dict" in order to access the individual elements. So for all my efforts have failed. Can anyone tell me how to convert a string to Dictionary in Hubl

 

Thanks

 

 

0 Upvotes
3 Replies 3
tjoyce
Recognized Expert | Elite Partner
Recognized Expert | Elite Partner

How to convert String to Dict using HUBL

You could access the elements also if you convert it to json.

 

{% set str = '{"billto": "Acme Associates","lines" : [{"Description":"Item1 20 Sep - 30 Sep 2019","Price":"33.00"},{"Description":"Item2 20 Sep - 30 Sep 2019","Price":"7.33"}]}' %}

{% set json = str|fromjson %}

{{json['billto']}}

output: Acme Associates

If this answer helped, please, mark as solved 😄


tim@belch.io | forms.belch.io | Design your own Beautiful HubSpot Forms; No coding necessary.

 

Drop by and say Hi to me on slack.

Richard_yellow
Participant

How to convert String to Dict using HUBL

Thanks very much for your reply. 

Whilst your example works, the issue I have is the instead of:

{% set str = '{"billto": "Acme Associates","lines" : [{"Description":"Item1 20 Sep - 30 Sep 2019","Price":"33.00"},{"Description":"Item2 20 Sep - 30 Sep 2019","Price":"7.33"}]}' %}

 I have:

{% set str =  ticket.subscription_json %}

where ticket.subscription_json is a custom property containing the Dict value.

 

It seems that the value returned from the custom property is not recognised as a DICT.

 

Any thoughts?

0 Upvotes
tjoyce
Recognized Expert | Elite Partner
Recognized Expert | Elite Partner

How to convert String to Dict using HUBL

Can you tell us the output of 

{{ ticket.subscription_json|pprint }}
0 Upvotes