# How to convert dict or list to string in ansible

**URL:** <https://forum.ansible.com/t/how-to-convert-dict-or-list-to-string-in-ansible/33887>\
**Category:** Ansible Project\
**Created:** [September 24, 2020, 1:46pm UTC](https://forum.ansible.com/t/how-to-convert-dict-or-list-to-string-in-ansible/33887 "2020-09-24T13:46:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Devops\_testing](https://avatars.discourse-cdn.com/v4/letter/d/5f8ce5/32.png) [@Devops\_testing](https://forum.ansible.com/u/Devops_testing)\
**Post date:** [September 24, 2020, 1:46pm UTC](https://forum.ansible.com/t/how-to-convert-dict-or-list-to-string-in-ansible/33887/1 "2020-09-24T13:46:15Z")

</div>

play:

- set\_fact:  
irules: “{{ rule | json\_query(‘[\*].definition’) }}”
- debug: msg: “{{ irules }}”

output:  
“msg”: [“when HTTP\_REQUEST {\n switch -glob [HTTP::uri] {\n "_HAC_" { pool char.hr.cal.ed.ABC.pool }\n \n } \n}” ]

I’m expecting output to be in below format.

**Expected output:**  
“when HTTP\_REQUEST {  
switch -glob [HTTP::uri] {  
“_HAC_” { pool char.hr.cal.ed.ABC.pool }  
}”

I have tried parsing “ **from\_json"** to the above play and ended up with the error:  
“({{ rule | json\_query(‘[\*].definition’) | from\_json }}): the JSON object must be str, bytes or ``bytearray, not ‘list’”}”

is there a way to convert list to string in ansibe? if not, any other suggestions would be appreciated to achieve “Expected output”`

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [September 24, 2020, 2:24pm UTC](https://forum.ansible.com/t/how-to-convert-dict-or-list-to-string-in-ansible/33887/2 "2020-09-24T14:24:36Z")

</div>

> play:  
> - set\_fact:  
> irules: "{{ rule | json\_query('[\*].definition') }}"  
> - debug: msg: "{{ irules }}"
> 
> output:  
> "msg": ["when HTTP\_REQUEST {\n switch -glob [HTTP::uri] {\n \"\*HAC\*\" { pool char.hr.cal.ed.ABC.pool }\n  
> \n } \n}" ]
> 
> I'm expecting output to be in below format.
> 
> \*Expected output:\*  
> "when HTTP\_REQUEST {  
> switch -glob [HTTP::uri] {  
> "\*HAC\*" { pool char.hr.cal.ed.ABC.pool }  
> }"
> 
> I have tried parsing "\*from\_json"\* to the above play and ended up with the error:  
> "({{ rule | json\_query('[\*].definition') | from\_json }}): the JSON object must be str, bytes or ``bytearray, not 'list'"}"
> 
> is there a way to convert list to string in ansibe? if not, any other suggestions would be appreciated to achieve  
> "Expected output"`

As a json\_query can have multiple results, the result of that filter is a list.

To get the first result you can use irules[0] or irules | first.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Devops\_testing](https://avatars.discourse-cdn.com/v4/letter/d/5f8ce5/32.png) [@Devops\_testing](https://forum.ansible.com/u/Devops_testing)\
**Post date:** [September 24, 2020, 4:14pm UTC](https://forum.ansible.com/t/how-to-convert-dict-or-list-to-string-in-ansible/33887/3 "2020-09-24T16:14:02Z")

</div>

Hi Racke,

Thank you for your response. I have used pause module and it got printed as expected. Is there a way to store that printed content in variable? I have to copy that content to a file and add few extra lines to that file and have to apply that configuration content to firewall. Please let me now if you need any info

play

- pause:  
seconds: 1  
prompt: “{{ irules[0] }}”

output:  
Pausing for 1 seconds  
(ctrl+C then ‘C’ = continue early, ctrl+C then ‘A’ = abort)  
[pause]  
when HTTP\_REQUEST {  
switch -glob [HTTP::uri] {  
“_HAC_” { pool char.hr.cal.ed.HAC.pool }

}  
}:
