Access value in request.cookies object array

SOLVE
DevM
Participant

I'm using the following code to retreive and display the cookie info from my site:

{% set user_info = [request.cookies.officerInfo] %}
{{user_info}}

 

{{user_info}} successfully displays the information in the cookie like this:

{"fname":"Admin","lname":"Testing","crm_id":"010101"}

 

I have a field in a 3rd party form where I need to put the crm id "010101".

I've tried various things like {{user_info.crm_id}} or {{user_info[2]}} without success.

Can someone help point me in the right direction? Thanks!

0 Upvotes
1 Accepted solution

Accepted Solutions
miljkovicmisa
Solution
Top Contributor | Gold Partner

Hello @DevM , since {{user_info}} returns a json string, you can convert that to a hubl object with the fromjson so you can access its keys like you tried to. So the snippet would look something like this:

 

 

{% set user_info_json = user_info %}
{% set user_info_obj = user_info_json|fromjson %}<br>
{{user_info_obj.crm_id}}

 

 

You can learn more about the fromjson filter right here in this link.

 

If my answer was helpful please mark it as a solution.

View solution in original post

5 Replies 5
Teun
Guide | Diamond Partner

Hi @DevM ,

 

Why are you wrapping the request.cookies in an array? ([request.cookies.officerInfo])
Does it not work if you use {% set user_info = request.cookies.officerInfo %} ?

 

Could you use the |pprint filter to check the output of user_info? {{user_info|pprint}}

Did my answer solve your issue? Help the community by marking it as the solution.
DevM
Participant

I tried a variety of things and had left the brackets by accident on my question - thank you for catching that.

Using pprint helped me see what the output was but I'm still trying to narrow my selection down.

0 Upvotes
miljkovicmisa
Solution
Top Contributor | Gold Partner

Hello @DevM , since {{user_info}} returns a json string, you can convert that to a hubl object with the fromjson so you can access its keys like you tried to. So the snippet would look something like this:

 

 

{% set user_info_json = user_info %}
{% set user_info_obj = user_info_json|fromjson %}<br>
{{user_info_obj.crm_id}}

 

 

You can learn more about the fromjson filter right here in this link.

 

If my answer was helpful please mark it as a solution.

View solution in original post

Teun
Guide | Diamond Partner

Yep, this is most likely the solution. Thanks @miljkovicmisa !

Did my answer solve your issue? Help the community by marking it as the solution.
DevM
Participant

This is what I needed! Thank you so much!