URI module / json parsing problem with escaped doueble qoutes

402 views
Skip to first unread message

Lucas, Sascha

unread,
Dec 15, 2014, 7:10:13 AM12/15/14
to ansible...@googlegroups.com
Hi,

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

{ "names": [
"nameA",
"\"double quoted name\"",
"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:

File "/usr/lib64/python2.6/json/decoder.py", line 227, in JSONArray
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.

Michael DeHaan

unread,
Dec 15, 2014, 8:29:13 AM12/15/14
to ansible...@googlegroups.com
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.





--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/383FAD2C9609F74D8F075EFF2B86B57998F2CFD8%40SGIS111.gisa-halle.de.
For more options, visit https://groups.google.com/d/optout.

Lucas, Sascha

unread,
Dec 16, 2014, 5:49:42 AM12/16/14
to ansible...@googlegroups.com
Hi Michael,

From: Michael DeHaan
Date: Mon, 15. Dec 2014 14:29

> 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/ 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": [
"nameA",
"\"double quoted name\"",
"nameB"
] }
"""

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

Lucas, Sascha

unread,
Dec 17, 2014, 8:23:42 AM12/17/14
to ansible...@googlegroups.com
Hi,

From: myself
Date: Tue, 16. Dec 2014 11:48
> According to 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')
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:

# Make the request, or try to :)
try:
resp, content = h.request(url, method=method, body=body, headers=headers)
r['redirected'] = redirected
r.update(resp_redir)
r.update(resp)
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.
Reply all
Reply to author
Forward
0 new messages