0

I'm trying to parse a JSON string that contains double quotes in it:

import json
x = '''{"key":"Value \"123\" "}'''

When I try to load this JSON using the following statement

y = json.loads(x)

It raises the following exception:

Expecting ',' delimiter: line 1 column 15 (char 14)

As per my understanding, it is due to the double quotes around 123 in JSON. Also, I tried replacing the \" (backslash quote) with some other stuff as well but all in vain

x.replace('\"',"'")

as it also replaced the double quotes that are present around key and value as well

'''{"key": "Value \"123\" "}'''  ---Replacing-->    '''{'key':'Value '123' '}''')

I can not change anything in the input string. That's coming from an API. Any help would be highly appreciated, I'm stuck with this for quite a time now. Thanks in advance...

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • that s a bit related only to that load fn, but if you have any method for parsing from a string, try to first parse i, with what in JS would be JSON.parse of a string cause that would temove the Adddional extra quotes – quirimmo Nov 28 '21 at 06:22
  • You are referring to some JS lib? – Alica Bellchi Nov 28 '21 at 06:34
  • 2
    Are you certain that is actually how the real data looks? A literal string (written in the source code) is interpreted with escape sequences - an existing string (from a file, socket, API) is taken verbatim. Python would not represent a string as shown because the escape sequence is redundant. – MisterMiyagi Nov 28 '21 at 06:56
  • Yes the format of data is as it is. The server is basically extracting the data from PST files and sending the data through API. – Alica Bellchi Nov 28 '21 at 07:08
  • Perhaps I should have asked more clearly: does the actual data correspond to the *literal* (source code) interpretation or the *verbatim* (text file) interpretation? Are there backslashes when you look at the `repr` of your data? – MisterMiyagi Nov 28 '21 at 07:14
  • This it the output for repr(x) -> '{"key":"Value "123" "}'... I'm Sorry I'm still not getting your concern. In the response from server, I get the exact same data string '{"key":"Value \"123\" "}' and that's how data is present in the PST files as well. Please let me know if I'm still not getting your point. – Alica Bellchi Nov 28 '21 at 07:20
  • 1
    The point is that for some reason you show `\"` in the data when that is not how Python would show it. So it is not clear whether these are there or got lost because of some error in the code or in preparing the question. For example, you have again stated that the `\" ` are actually there in your data, but at the same time show data without it as well. – MisterMiyagi Nov 28 '21 at 07:27
  • Yes, I got your point. The string that I showed here is basically printed out using a scala code. Here's the big picture. I'm running a python snippet in scala code. The string I showed in input is basically printed by scala code, and the python snippet is using the same string for parsing. – Alica Bellchi Nov 28 '21 at 07:39

1 Answers1

1

\" within a string is simply a double quotation mark. You need to add another backslash:

x = '''{"key":"Value \\"123\\" "}'''
DYZ
  • 55,249
  • 10
  • 64
  • 93
  • I can not change anything in the string directly. Have to do it using some function or something like replace, or re.sub – Alica Bellchi Nov 28 '21 at 06:26
  • What is the source of the string? If it would be your code you could obviously change it – Klaus D. Nov 28 '21 at 07:09
  • The source is basically a server and I'm just consuming the API to get this response as string: '{"key":"Value \"123\" "}' FYI the server is basically parsin the PST files so there are many irregular patterns in it. – Alica Bellchi Nov 28 '21 at 07:20