json.loads(jsonstring) in Python fails if string has a “\r” i.e. carriage return character

I am getting a JSON string which has a "\r" character somewhere e.g. "{"data":"foo \r\n bar"}" when I try to parse it throws ValueError.

>>> j="""{"data":"foo \r\n bar"}"""
>>> import json
>>> f=json.loads(j)

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    f=json.loads(j)
  File "C:\Python27\lib\json\__init__.py", line 326, in loads
    return _default_decoder.decode(s)
  File "C:\Python27\lib\json\decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python27\lib\json\decoder.py", line 382, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Invalid control character at: line 1 column 13 (char 13)
>>> j[13]
'\r'

"\r" is a perfectly legal character in a Python string.

How can I parse this JSON string, such that

>>> dct = somehow_parse_json(j)
>>> dct['data']
'foo \r\n bar'

I could easily just find and pop carriage return characters, but I would prefer if they can be saved.

Answers


You should escape slashes in JSON:

j="""{"data":"foo \\r\\n bar"}"""

If you are not escaping them, your JSON is invalid (being valid Python string).


Need Your Help

Why doesn't HttpClient work but HttpUrlConnenction do when posting data to form

android cookies httpclient httpurlconnection urlconnection

I'm building an android app which should perform a GET on my site to get two cookies and then perform a post to the same site with these cookies.

using the colorbox plugin with jQuery JS works locally but not from server

jquery jquery-plugins colorbox

I know I've over looked something, but as that statement suggests I can't figure out what it is. I have downloaded the latest jquery and colorbox libraries. I've added this to a working a web pa...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.