# URI module / json parsing problem with escaped doueble qoutes

**URL:** <https://forum.ansible.com/t/uri-module-json-parsing-problem-with-escaped-doueble-qoutes/17061>\
**Category:** Ansible Project\
**Created:** [December 15, 2014, 12:09pm UTC](https://forum.ansible.com/t/uri-module-json-parsing-problem-with-escaped-doueble-qoutes/17061 "2014-12-15T12:09:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sascha.lucas](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@sascha.lucas](https://forum.ansible.com/u/sascha.lucas)\
**Post date:** [December 15, 2014, 12:09pm UTC](https://forum.ansible.com/t/uri-module-json-parsing-problem-with-escaped-doueble-qoutes/17061/1 "2014-12-15T12:09:50Z")

</div>

Hi,

I'm talking to a REST/json webservice via the URI module. The webservice is correctly returning a list like this:

{ "names": [  
&nbsp;&nbsp;"nameA",  
&nbsp;&nbsp;"\"double quoted name\"",  
&nbsp;&nbsp;"nameB"  
] }

However the uri module does not retrun the json key, because json.loads(content) fails. If I comment out try/expect the last line of python trace is:

&nbsp;&nbsp;File "/usr/lib64/python2.6/json/decoder.py", line 227, in JSONArray  
&nbsp;&nbsp;&nbsp;&nbsp;raise ValueError(errmsg("Expecting , delimiter", s, end))  
ValueError: Expecting , delimiter: line 371 column 16 (char 10160)

In the URI module "content" key, the list looks like this (white space truncated):

\"names\": [\n \"nameA\",\n \"\"double quoted name\"\",\n \"nameB\"\n]

I asume, that the backslash preserving the double qoute in JSON must be escaped it self:

\"names\": [\n \"nameA\",\n \"\\"double quoted name\\"\",\n \"nameB\"\n]

Would be nice if someone can provide a quick fix for this problem.

Thanks, Sascha.

Aufsichtsratsvorsitzender: Herbert Vogel  
Geschäftsführung: Michael Krüger  
Sitz der Gesellschaft: Halle/Saale  
Registergericht: Amtsgericht Stendal | Handelsregister-Nr. HRB 208414  
UST-ID-Nr. DE 158253683

Diese E-Mail enthält vertrauliche und/oder rechtlich geschützte Informationen. Wenn Sie nicht der richtige Empfänger sind oder diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und vernichten Sie diese Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail oder des Inhalts dieser Mail sind nicht gestattet. Diese Kommunikation per E-Mail ist nicht gegen den Zugriff durch Dritte geschützt. Die GISA GmbH haftet ausdrücklich nicht für den Inhalt und die Vollständigkeit von E-Mails und den gegebenenfalls daraus entstehenden Schaden. Sollte trotz der bestehenden Viren-Schutzprogramme durch diese E-Mail ein Virus in Ihr System gelangen, so haftet die GISA GmbH - soweit gesetzlich zulässig - nicht für die hieraus entstehenden Schäden.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [December 15, 2014, 1:29pm UTC](https://forum.ansible.com/t/uri-module-json-parsing-problem-with-escaped-doueble-qoutes/17061/2 "2014-12-15T13:29:10Z")

</div>

Hmm, that’s curious.

Things like jsonlint pass, but it seems the Python JSON formatter does not like what you have.

Here’s a simple test program:

STRING = “”"

{ “names”: [

“nameA”,

“"double quoted name"”,

“nameB”

] }

“”"

import json

print json.loads(STRING)

I’m wondering if this might actually not be valid JSON, or just that there is a bug in the python json library.

---

<div class="post-metadata">

**Author:** ![sascha.lucas](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@sascha.lucas](https://forum.ansible.com/u/sascha.lucas)\
**Post date:** [December 16, 2014, 10:48am UTC](https://forum.ansible.com/t/uri-module-json-parsing-problem-with-escaped-doueble-qoutes/17061/3 "2014-12-16T10:48:20Z")

</div>

Hi Michael,

> Things like jsonlint pass, but it seems the Python JSON formatter does not like what you have.

> I'm wondering if this might actually not be valid JSON, or just that there is a bug in the python json library.

Thanks a lot. According to [http://json.org/](http://json.org/) in the picture "string" double quotes (quotation mark) must be escaped. So I assume it is valid JSON. If so, there must be something wrong with the python parser?

My temporary fix on the URI module is to remove the double occurrence of quotation marks to not confuse the parser. However, this only works because my quotation marks are at the beginning/end of the string (not in between):

-js = json.loads(content)  
+js = json.loads(content.replace('""','"'))

Or in your example:

STRING = """  
{ "names": [  
&nbsp;&nbsp;"nameA",  
&nbsp;&nbsp;"\"double quoted name\"",  
&nbsp;&nbsp;"nameB"  
] }  
"""

import json  
print json.loads(STRING.replace('""','"'))

Thanks, Sascha.

Aufsichtsratsvorsitzender: Herbert Vogel  
Geschäftsführung: Michael Krüger  
Sitz der Gesellschaft: Halle/Saale  
Registergericht: Amtsgericht Stendal | Handelsregister-Nr. HRB 208414  
UST-ID-Nr. DE 158253683

Diese E-Mail enthält vertrauliche und/oder rechtlich geschützte Informationen. Wenn Sie nicht der richtige Empfänger sind oder diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und vernichten Sie diese Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail oder des Inhalts dieser Mail sind nicht gestattet. Diese Kommunikation per E-Mail ist nicht gegen den Zugriff durch Dritte geschützt. Die GISA GmbH haftet ausdrücklich nicht für den Inhalt und die Vollständigkeit von E-Mails und den gegebenenfalls daraus entstehenden Schaden. Sollte trotz der bestehenden Viren-Schutzprogramme durch diese E-Mail ein Virus in Ihr System gelangen, so haftet die GISA GmbH - soweit gesetzlich zulässig - nicht für die hieraus entstehenden Schäden.

---

<div class="post-metadata">

**Author:** ![sascha.lucas](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@sascha.lucas](https://forum.ansible.com/u/sascha.lucas)\
**Post date:** [December 17, 2014, 1:23pm UTC](https://forum.ansible.com/t/uri-module-json-parsing-problem-with-escaped-doueble-qoutes/17061/4 "2014-12-17T13:23:15Z")

</div>

Hi,

> According to [http://json.org/](http://json.org/) in the picture "string"  
> double quotes (quotation mark) must be escaped. So I assume it is valid  
> JSON. If so, there must be something wrong with the python parser?

httplib2 returns the content right (backslash escaped). So I assume, it gets lost somewhere inside the URI module:

import httplib2  
import json  
http = httplib2.Http()  
response, content = http.request('[http://some.test.tld/test’,'GET](http://some.test.tld/test','GET)’)  
content  
'{ "key": "\\"works\\"" }\n'  
print content  
{ "key": "\"works\"" }

print json.loads(content)  
{u'key': u'"works"'}

I assume the correct way in URI module (function uri) is:

&nbsp;&nbsp;&nbsp;&nbsp;# Make the request, or try to 🙂  
&nbsp;&nbsp;&nbsp;&nbsp;try:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;resp, content = h.request(url, method=method, body=body, headers=headers)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r['redirected'] = redirected  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.update(resp\_redir)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.update(resp)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try:  
- return r, unicode(content.decode('unicode\_escape')), dest  
+ return r, unicode(content.encode('string\_escape').decode('unicode\_escape')), dest

At least, this works for me.

Thanks, Sascha.

Aufsichtsratsvorsitzender: Herbert Vogel  
Geschäftsführung: Michael Krüger  
Sitz der Gesellschaft: Halle/Saale  
Registergericht: Amtsgericht Stendal | Handelsregister-Nr. HRB 208414  
UST-ID-Nr. DE 158253683

Diese E-Mail enthält vertrauliche und/oder rechtlich geschützte Informationen. Wenn Sie nicht der richtige Empfänger sind oder diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und vernichten Sie diese Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail oder des Inhalts dieser Mail sind nicht gestattet. Diese Kommunikation per E-Mail ist nicht gegen den Zugriff durch Dritte geschützt. Die GISA GmbH haftet ausdrücklich nicht für den Inhalt und die Vollständigkeit von E-Mails und den gegebenenfalls daraus entstehenden Schaden. Sollte trotz der bestehenden Viren-Schutzprogramme durch diese E-Mail ein Virus in Ihr System gelangen, so haftet die GISA GmbH - soweit gesetzlich zulässig - nicht für die hieraus entstehenden Schäden.
