I have a rather weird question:
So, I have a simple dictionary in python, looking like so:
data={'Acoustics': {'Product Type': 'Acoustic Pod', 'Width [cm]': '1000', 'Noise Reduction Coefficient': '29 dB', 'Standards, Certification, and Documentation': 'PN EN-ISO 717-1:1999 ; EN 13501 B, s1, d0', 'Material': 'MDF ; Glass ; Acoustic composite', 'Color': 'NCS ; RAL', 'Installation Method': 'Own assembly ; Installation by the manufacturer', 'Facing Material': 'MDF ; Certified Paint', 'Type': 'Adjustable Ventilation ; Motion Sensor ; LED 6000K 2W ; 230V ; RJ45 or USB Charger ; Integrated seat and shelf'}}
and I try to save this via django onto my pgSQL database (with jsonb column), and I somehow end up with (notice the double quote at the start and end):
"{'Acoustics': {'Product Type': 'Acoustic Pod', 'Width [cm]': '1000', 'Noise Reduction Coefficient': '29 dB', 'Standards, Certification, and Documentation': 'PN EN-ISO 717-1:1999 ; EN 13501 B, s1, d0', 'Material': 'MDF ; Glass ; Acoustic composite', 'Color': 'NCS ; RAL', 'Installation Method': 'Own assembly ; Installation by the manufacturer', 'Facing Material': 'MDF ; Certified Paint', 'Type': 'Adjustable Ventilation ; Motion Sensor ; LED 6000K 2W ; 230V ; RJ45 or USB Charger ; Integrated seat and shelf'}}"
To add to my DB, I use django forms like so:
form_data={"cvar": data}
form = myform(form_data)
if form.is_valid():
form.save()
So, now, I have two questions: [1] How do I avoid the above scenario? Why is it getting quoted
? I simply pass a form data to save and it somehow ends up as a string rather than json. [2] If I have such quoted json (which unfortunately I do now), how do I unquote and access this as a json (at the moment it is a damn string!).
Thank you.
It isn't easy to figure out without MCVE showing the relevant code .
It looks like you wrote to the database a dictionary converted to string and then converted to JSON, instead of converting the dictionary to json directly.
Like:
>>> import json
>>> a={'Acoustics': {'Product Type': 'Acoustic Pod', 'Width [cm]': '1000', 'Noise Reduction Coefficient': '29 dB', 'Standards, Certification, and Documentation': 'PN EN-ISO 717-1:1999 ; EN 13501 B, s1, d0', 'Material': 'MDF ; Glass ; Acoustic composite', 'Color': 'NCS ; RAL', 'Installation Method': 'Own assembly ; Installation by the manufacturer', 'Facing Material': 'MDF ; Certified Paint', 'Type': 'Adjustable Ventilation ; Motion Sensor ; LED 6000K 2W ; 230V ; RJ45 or USB Charger ; Integrated seat and shelf'}}
and then:
>>> print(json.dumps(str(a)))
"{'Acoustics': {'Product Type': 'Acoustic Pod', 'Width [cm]': '1000', 'Noise Reduction Coefficient': '29 dB', 'Standards, Certification, and Documentation': 'PN EN-ISO 717-1:1999 ; EN 13501 B, s1, d0', 'Material': 'MDF ; Glass ; Acoustic composite', 'Color': 'NCS ; RAL', 'Installation Method': 'Own assembly ; Installation by the manufacturer', 'Facing Material': 'MDF ; Certified Paint', 'Type': 'Adjustable Ventilation ; Motion Sensor ; LED 6000K 2W ; 230V ; RJ45 or USB Charger ; Integrated seat and shelf'}}"
instead of:
>>> print(json.dumps(a))
{"Acoustics": {"Product Type": "Acoustic Pod", "Width [cm]": "1000", "Noise Reduction Coefficient": "29 dB", "Standards, Certification, and Documentation": "PN EN-ISO 717-1:1999 ; EN 13501 B, s1, d0", "Material": "MDF ; Glass ; Acoustic composite", "Color": "NCS ; RAL", "Installation Method": "Own assembly ; Installation by the manufacturer", "Facing Material": "MDF ; Certified Paint", "Type": "Adjustable Ventilation ; Motion Sensor ; LED 6000K 2W ; 230V ; RJ45 or USB Charger ; Integrated seat and shelf"}}
If you already have the python dictionary representation as a string from some external data source, then you can use ast.literal_eval() to turn it to a proper dict first :
>>> the_dict=ast.literal_eval(the_data)
>>> the_json=json.dumps(the_dict)
Or, preferably, change the data source (for example a web form) to use JSON format instead of Python dict text representation for exchanging data.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.