I have a requirement where I have to write some JSON
string in a file.
For example, I have below string in
My python code :
S = '''{
"K1": "v1",
"K2" : "\nv2"
}'''
When I try to write it in json file using json.dumps(s) it is getting written to a file like below :
"{\"k1\" : \"v1\",\"k2\" : \"\\nv2\"}"
Whereas I need it to be like :
{
"K1": "v1",
"K2" : "\nv2"
}
In json file.
I am able to achieve this if I declare my string as raw string preceding with r but In the actual scenario I cannot declare a raw string as I am getting the string value in python variable.
Note: I am having multiple escape sequences in my json string value like given in value for key k2 in the above json structure.
Will really appreciate it if anyone helps me to generate a proper json file with required json format.Thanks in advance.