29

I am extracting a postgres table as json. The output file contains lines like:

{"data": {"test": 1, "hello": "I have \" !"}, "id": 4}

Now I need to load them in my python code using json.loads, but I get this error:

Traceback (most recent call last):
  File "test.py", line 33, in <module>
    print json.loads('''{"id": 4, "data": {"test": 1, "hello": "I have \" !"}}''')
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 365, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 381, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting , delimiter: line 1 column 50 (char 49)

I figured out the fix is to add another \ to \". So, if I pass

{"data": {"test": 1, "hello": "I have \\" !"}, "id": 4}

to json.loads, I get this:

{u'data': {u'test': 1, u'hello': u'I have " !'}, u'id': 4}

Is there a way to do this without adding the extra \? Like passing a parameter to json.loads or something?

1
  • 1
    hey folks i am having same problem any valid solutuion ? Commented Jul 7, 2017 at 14:08

5 Answers 5

30

You can specify so called “raw strings”:

>>> print r'{"data": {"test": 1, "hello": "I have \" !"}, "id": 4}'
{"data": {"test": 1, "hello": "I have \" !"}, "id": 4}

They don’t interpret the backslashes.

Usual strings change \" to ", so you can have " characters in strings that are themselves limited by double quotes:

>>> "foo\"bar"
'foo"bar'

So the transformation from \" to " is not done by json.loads, but by Python itself.

Sign up to request clarification or add additional context in comments.

2 Comments

I am reading the data file line by line. So I have the dictionaries in variables. I tried .replace("\\", r"\\") and .encode('string-escape'), neither works.
In JSON "\"" means '"', too, just like in Python. If there’s a \" in the input file, it actually is a ". If you for some reason want to have r'\"' anyway, you would need .replace('"', '\\"').
9

Try this:

json.loads(r'{"data": {"test": 1, "hello": "I have \" !"}, "id": 4}')

If you have that string inside a variable, then just:

json.loads(data.replace("\\", r"\\"))

4 Comments

I'm afraid it doesn't work, '{"data": {"test": 1, "hello": "I have \" !"}, "id": 4}' and '{"data": {"test": 1, "hello": "I have \" !"}, "id": 4}'.replace("\\", r"\\") are exactly the same.
@AliBZ It doesn't work because you're missing the r before the string. Copy and paste my first example, that will work.
I have my string inside a variable, thats why I used your second example.
@AliBZ Ok, so the unique way to do it is manually replace " with \\" using replace.
2

Try using triple quotes r""", no need to consider the \ thing.

json_string = r"""
{
    "jsonObj": []
}
"""
data = json.loads(json_string)

Comments

1

Try the ways source.replace('""', '') or sub it, cause "" in the source will make json.loads(source) can not distinguish them.

Comments

1

for my instance, i wrote:

STRING.replace("': '", '": "').replace("', '", '", "').replace("{'", '{"').replace("'}", '"}').replace("': \"", '": "').replace("', \"", '", "').replace("\", '", '", "').replace("'", '\\"')

and works like a charm.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.