Home > Blockchain >  How do I parse a json string literal in python without modifying it?
How do I parse a json string literal in python without modifying it?

Time:04-29

This, is unambiguously a valid json string:

{"key": "quoted \"value\" and 'value'"}

...because of the ' and " in the value, both of these are invalid:

json.parse("{"key": "quoted \"value\" and 'value'"}")
json.parse('{"key": "quoted \"value\" and 'value'"}')
> SyntaxError: invalid syntax

However, using triple quotes is also invalid:

json.loads("""{"key": "quoted \"value\" and 'value'"}""")
> JSONDecodeError: Expecting ',' delimiter: line 1 column 18 (char 17)

I get it, the reason for this is that \" is rendered as " by the multiline literal:

print("""{"key": "quoted \"value\" and 'value'"}""")
> {"key": "quoted "value" and 'value'"} <--- invalid json

So how do you do this?

In the trivial case I can manually fix the json, but in the complex case (hundreds of lines of json) this isn't plausible.

The issue I'm trying to solve is manually replaying a request via a jupyter notebook; you copy the request body in chrome, and then want to replay it by pasting the request into the cell of a jupyter notebook.

CodePudding user response:

You can use a raw triple-quoted string here, which treats backslashes as literal characters:

json.loads(r"""{"key": "quoted \"value\" and 'value'"}""")
> {'key': 'quoted "value" and \'value\''}
  • Related