-1

I have a json file with something like "key": "value1\value2". I can not make python read this file properly:

  1. If there is only one backslash, it refuses to parse it.
  2. If there are two (like "key":"value1\\value2") the output also has two backslashes

I am using standard json module with loads/dumps functions

Could not find any information anywhere, other similar questions are irrelevant

ymdred16
  • 98
  • 3
  • 15
  • 2
    Use `print` to look at the value of the fields you read. Otherwise, Python will show you the "representation" which is designed to look like what you would have to type to recreate the value. – rici Jan 02 '23 at 18:48

1 Answers1

0

You say you have a JSON file "with something like ...". Something like? Really? Why couldn't you say what it exactly contains?

Regardless, none of the possible choices you display are legal. A JSON-encoded Python dictionary within a file would look like:

{"key": "value1\\value2"}

Note the opening and closing braces, '{' and '}'. A single backslash within a double-quoted string is for special escape sequences, for exmaple "\n", to represent a newline character. So the way you represent an actual backslash character is with two consecutive backslashes, for example "value\value2".

Booboo
  • 38,656
  • 3
  • 37
  • 60