1

sample_json_obj = '{"foo":{"IdentityDocuments":[{"bar":"{\"baz\":\"qux\"}"}]}}'

This is syntactically accurate for a JSON string. However when I try to load into a variable for further processing,

json.loads(sample_json_obj)

I get the following stacktrace

Traceback (most recent call last):
  File "test.py", line 5, in <module>
    json.loads(sample_json_obj)
  File "/usr/local/Cellar/python@2/2.7.17_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/local/Cellar/python@2/2.7.17_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/Cellar/python@2/2.7.17_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 380, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting , delimiter: line 1 column 40 (char 39)

I read through a few StackOverflow posts trying to fix this and one of the popular ones suggest mutating the string to the following format

sample_json_obj = r'{"foo":{"IdentityDocuments":[{"bar":"{\"baz\":\"qux\"}"}]}}'

which works as expected but my only problem is that since I am getting this JSON object directly from a query and I am not able to add the r in front of it i.e convert it to a raw string although I have tried several other stack posts including trying formatting, none worked.

Can someone please help me?

Would be great if someone could help me without modifying the first line i.e

sample_json_obj = '{"foo":{"IdentityDocuments":[{"bar":"{\"baz\":\"qux\"}"}]}}'

Swastik Udupa
  • 316
  • 3
  • 17
  • If you are getting the value of the string directly from a query, then why are you dealing with a string literal at all? – blhsing Jul 23 '21 at 05:34
  • let's say `sample_json_obj = get_query_result()` I am still having the same problem right? I dont understand your question. – Swastik Udupa Jul 23 '21 at 05:38
  • In your Sample Json: ` ​{"bar":"{\"baz\":\"qux\"}"} ` Do you want to keep it `bar:string` or make that dict too? – devReddit Jul 23 '21 at 05:51
  • The problem is that the string is effectively malformed as far as Python is concerned. The escape characters need to be doubled - i.e. '{"foo":{"IdentityDocuments":[{"bar":"{\\"baz\\":\\"qux\\"}"}]}}' –  Jul 23 '21 at 05:56
  • @devReddit I want to make everything a dict as in a json object so that I can access anything with key, value combinations. – Swastik Udupa Jul 23 '21 at 06:03
  • @AndyKnight I tried doing `sample_json_obj = (sample_json_obj.replace('\"', '\\"')) json.loads(sample_json_obj)` Seeing error, `ValueError: Expecting property name: line 1 column 2 (char 1)` – Swastik Udupa Jul 23 '21 at 06:03
  • Try this:- assign the string exactly as shown at the beginning of your post then print it. You'll see that the backslashes are not there. It's because the escaped double-quote is irrelevant. It would help if you could explain how you acquire these data –  Jul 23 '21 at 06:07

0 Answers0