I have a string that looks like JSON but it's not. I want to convert it into a Python object (back and forth).
Here is the format:
v = "{ TestKey = true,
Calibration = 0,
Blacks = 0,
Brightness = 50,
BasicSetting = { 0,
0,
32,
22},
Whites = 0 }"
I can not use directly json.load(v) on such string. What's the best / easiest way to convert it into a Python object ? Writing a custom Python JSON encoder / decoder ? I will need to decode the Python back into the original string format.
So far, I'm replacing = by : but I face some issues to correctly put the ' in the original string and I don't think it's the best way to do. Any other suggestions ?
Thanks.
{0,0,32,22}appears to be intended as an array, but it starts with the same delimiter as a dictionary and the only way to distinguish it from a dictionary is to look ahead (indefinitely) and see that there's no= valuepart (this is in start contrast to JSON, where you can distinguish arrays and dicts by their starting delimiter). Where is this stupid data format from?import json; import re; v1 = re.sub(r'([a-zA-Z]+)', r'"\g<1>"', v.replace('=', ':').replace("\n", "")); json.loads(re.sub(r"{([^:]+)}", "[\g<1>]", v1));