Filip N.
Filip N.

Reputation: 171

How to handle double quotes in JSON value?

I have string that I receive as a HTTP response. Unfortunately, the string is quite in a raw format and I am unable to convert it to the JSON object.

Example string:

json_string = '{"client_id":8888,"time":null,"questions":{"id":10000,"answered":"true","answer":"The "project" was good, I enjoyed it. Do you plan to repeat it?"},"other":"When is the "project" released?"}'

The problem with the string is that it contains double quotes in some of the values (user answers). The keys of the values that can contain double quotes are not always the same (here "answer" and "other" may be different in other responses). The user answer can contain any characters (commas, brackets, double quotes, ...).

I tried to use different loaders (json, yaml) and I even tried to parse the string by myself with regexp, but I have always failed.

Is there any way how to convert this string to JSON object?

Upvotes: 3

Views: 26022

Answers (1)

Mark Reed
Mark Reed

Reputation: 95252

To be clear, the correct solution here would be to fix whatever is sending that HTTP response; the proper way to include double quotation marks in a JSON string is to backslash them:

{ "key": "value with \"double quotes\" inside" }

Any JSON parser should accept the above as a valid object.

You can't reliably parse something with unescaped quotes because you can't tell which ones are the actual string delimiters and which ones are enclosed data. If you can assume that the quotes are balanced (so there's always an even number of quotation marks inside any string) then you could construct a parser to transform it into the right syntax, but it's not going to be a simple regex-based fix.

That said, with your sample data, it looks like the embedded quotes are surrounded by spaces, and the actual delimiter quotes aren't. So you could try running it through a replace of " (space-quote) with \" (space-backslash-quote) and " (quote-space) with \" (backslash-quote-space) and see if that works.

Upvotes: 7

Related Questions