0

When I want to save my JSON it adds backslashes and double quotes to it. So the single quote around is getting replaced with double quotes and it adds the slashes

'{"created_at":"foo","created_by":"foo"}'

So when I open the saved file it looks like this

"{\"created_at\":\"foo\",\"created_by\":\"foo\"}"

and i need to have it like this

{"created_at":"foo","created_by":"foo"}

My code looks like this: For saving

with open('data3.json', 'w', encoding='utf-8') as f:
    json.dump(data3, f, ensure_ascii=False)
wihee
  • 55
  • 7
  • 2
    Possible duplicate: check out [this](https://stackoverflow.com/a/25242376/17079464) post. – Maximilian Gangloff Jul 18 '22 at 07:47
  • Does this answer your question? [Dump to JSON adds additional double quotes and escaping of quotes](https://stackoverflow.com/questions/25242262/dump-to-json-adds-additional-double-quotes-and-escaping-of-quotes) – Gamopo Jul 18 '22 at 07:49

1 Answers1

1

Saving with the following code:

import json 
test = {"created_at":"foo","created_by":"foo"}
with open('data3.json', 'w', encoding='utf-8') as f:
json.dump(test, f, ensure_ascii=False)

I get the following: {"created_at": "foo", "created_by": "foo"}

try editing your notepad++ setting maybe something about their display.

buddemat
  • 4,552
  • 14
  • 29
  • 49
  • maybe it's important to mention that the json looks like this befor saving it '{"created_at":"foo","created_by":"foo"}', so there is this single quote around it – wihee Jul 18 '22 at 08:07
  • you should maybe convert it to dict type or use json loads before writing to file. – Idan Toledano Jul 18 '22 at 10:44