Home > database >  What do I need to write if i want to get following json input?
What do I need to write if i want to get following json input?

Time:01-01

I have a string here:

string = '{{"Temporary Directory Path" : "{0}", "Download Folder" : "{1}"}}'.format(temp, download) The temp and download varaibles are just file paths

I want to put it in my json file. But if I write it with python in my json file, I get the following text in my json file:

"{\"Temporary Directory Path\" : \"C:/Temporary/\", \"Download Folder\" : \"C:/Users/myname/Downloads"}"

How can I get the following output, without removing the double {{ ? If I remove them, I get a KeyError.

CodePudding user response:

As you already have JSON content (a string following JSON specs), you need either to

  • write it directly

    with open("data.json", "w") as f:
        f.write(string)
    
  • load as Python structure, then JSON dump it

    json.dump(json.loads(string), open("data.json", "w"))
    
  • Related