0
my_json = '{"hello":"\\x20\\x20\\x20\\x3Cdiv\\x3E\\x20\\x0A\\x20\\x20\\x20\\x20\\HELLO"}'

json.loads(my_json)

I get this.

JSONDecodeError: Invalid \escape: line 1 column 11 (char 10)

What conversion do i need to use so my json will load?

Keeno
  • 28
  • 1
  • 7

2 Answers2

2

just first convert python string into json and it will work for you.

my_json = json.dumps({
              "hello":"\\x20\\x20\\x20\\x3Cdiv\\x3E\\x20\\x0A\\x20\\x20\\x20\\x20\\HELLO"
                })
json.loads(my_json)
user202729
  • 3,358
  • 3
  • 25
  • 36
Sohaib
  • 566
  • 6
  • 15
  • `{ "hello":"\x20\x20\x20\x3Cdiv\x3E\x20\x0A\x20\x20\x20\x20\HELLO" }` is **not** the same thing as OP's data, which is `'{"hello":"\\x20\\x20\\x20\\x3Cdiv\\x3E\\x20\\x0A\\x20\\x20\\x20\\x20\\HELLO"}'`. _just first convert python string into json and it will work for you._ That makes little sense, and it certainly isn't what the code you shared is doing. – AMC Mar 20 '20 at 22:33
-1

The suggestion of @pguardiario definitely is cleaner, but if you're just after the resulting dict, give eval a try:

In [77]: my_json                                                                
Out[77]: '{"hello":"\\x20\\x20\\x20\\x3Cdiv\\x3E\\x20\\x0A\\x20\\x20\\x20\\x20\\HELLO"}'

In [78]: evaluated = eval(my_json)                                                          
Out[78]: {'hello': '   <div> \n    \\HELLO'}

In [79]: evaluated.items()                                                      
Out[79]: dict_items([('hello', '   <div> \n    \\HELLO')])
Klaus-Dieter Warzecha
  • 2,265
  • 2
  • 27
  • 33
  • 1
    Please don't suggest using `eval`, some dev will find this and stick a security hole in their app without thinking about it. https://stackoverflow.com/questions/1832940/why-is-using-eval-a-bad-practice – wjdp Jan 26 '21 at 01:09
  • 1
    Why is eval a security hole? – Louis Duran Jul 13 '22 at 20:48