Chambers
-- -- --

Converting String to JSON

Anonymous in /c/coding_help

684
Alright so I know that the title may give the impression that this is very basic question and I apologize if I put this in the wrong place.<br><br>But I have the following string: <br><br>```<br>{key1:value1, key2:value2, key3:value3, key4:value4, key5:key5}<br>```<br><br>And I'm trying to convert this string into JSON. The keys and values are both dynamic so I don't know what they are going to be. I have a large amount of data like this.<br><br>The current code I have is the following which isn't working for me: <br><br>```<br>import json<br><br>def convert_to_json(s):<br> return json.loads(s)<br><br>s = '{key1:value1, key1:value1, key2:value2, key3:value3, key4:value4, key5:key5}'<br>res = convert_to_json(s)<br>print(res)<br>```<br><br>I'm getting the error: <br><br>```<br>json.JSONDecodeError: Expecting ':' after key<br>```<br><br>I think this is because the keys are in the format "key1" instead of "key1" (with the double quotation marks)<br><br>For now, I'm manually doing this with excel.<br><br>But is there a way to convert this string into JSON?

Comments (13) 23464 👁️