# How to generate JSON with escaped quotes

**URL:** <https://forum.ansible.com/t/how-to-generate-json-with-escaped-quotes/24365>\
**Category:** Ansible Project\
**Created:** [May 1, 2017, 5:39pm UTC](https://forum.ansible.com/t/how-to-generate-json-with-escaped-quotes/24365 "2017-05-01T17:39:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![oss.mlists](https://avatars.discourse-cdn.com/v4/letter/o/5daacb/32.png) [@oss.mlists](https://forum.ansible.com/u/oss.mlists)\
**Post date:** [May 1, 2017, 5:39pm UTC](https://forum.ansible.com/t/how-to-generate-json-with-escaped-quotes/24365/1 "2017-05-01T17:39:50Z")

</div>

Hello,  
I need to get JSON as string with quotes escaped by a single backslash. Original JSON is in file and I need to send it as string in another JSON body using URI module. I’m able to get only string with quotes escaped as \\" instead of ".

// file example.json  
{“a”:“b”,“c”:“d”}

// I need to get {"a":"b","c":"d"} to embed it in uri module

- uri  
url: http://…  
body: {“data”:“{{ lookup(‘file’, ‘files/example.json’) | replace(‘"’, ‘\"’) }}”}

It generates: {\"a\":\"b\",\"c\":\"d\"}  
It is not just escaping in debug, I save it to file using copy module and it has multiple backslashes.

Many thanks,  
Petr

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [May 1, 2017, 6:31pm UTC](https://forum.ansible.com/t/how-to-generate-json-with-escaped-quotes/24365/2 "2017-05-01T18:31:12Z")

</div>

I’d look at using the to\_json filter instead of replace.

body: {“data”:“{{ lookup(‘file’, ‘files/example.json’) | to\_json }}”}

---

<div class="post-metadata">

**Author:** ![oss.mlists](https://avatars.discourse-cdn.com/v4/letter/o/5daacb/32.png) [@oss.mlists](https://forum.ansible.com/u/oss.mlists)\
**Post date:** [May 3, 2017, 1:11pm UTC](https://forum.ansible.com/t/how-to-generate-json-with-escaped-quotes/24365/3 "2017-05-03T13:11:24Z")

</div>

Thank you so much,  
it did the trick… almost. I had to add single quotes enclosing the whole expression. Otherwise it defensively renders multiple escape backslashes.

Petr
