I have a dict as follows in python
test_dict = {'A': 'apple', 'B': 12345000, 'c': '2020-07-13 03:04:21.752566'}
And I need to format the json output as follows inclusing back slashes and comments, Back slashes and comments are not required for int values
{
"zz":"zz",
"xx":"x",
"rawData":"{\"A\":\"apple\",\"B\":12345000,\"c\":\"2020-07-13 03:04:21.752566\"}"
}
I have implemented following function
def construct_raw(abc):
start = "\"{"
end = "}"
delimiter = ","
dict_len = len(abc)
cnt = 0
for k, v in abc.items():
cnt = cnt + 1
if type(v) is str:
n = "\\\"" + v + "\\\""
else:
n = str(v)
start = start + "\\\"" + k + "\\\"" + ":" + n
if cnt != dict_len:
start = start + delimiter
start = start + end + "\""
return start
This produced following output for rawData
"{\\"A\\":\\"apple\\",\\"B\\":12345000,\\"c\\":\\"2020-07-13 03:04:21.752566\\"}"
But this will results double back slash in the string How to fix this issue?