Stripping auto-generated quotation marks using json.dumps( ) (Python) -
Stripping auto-generated quotation marks using json.dumps( ) (Python) -
i'm using json.dumps() method via passing in ordereddict. (see below syntax). it's doing correctly, there 1 specific field "labels":
consistently surrounds input " "
(quotation marks) , need not to.
desiredjson = ordereddict([('type', ""), ('labels', '' ), ('bgcolor', ''), ('bordercolor', '')]) (category_type, updatedlabels, bgcolors, bordercolors) in zip(type_, labels_, bgcolor_, bordercolor_): print category_type+updatedlabels desiredjson["type"] = category_type desiredjson["labels"] = '["%s", "%s"]' % (category_type, updatedlabels) desiredjson["bgcolor"] = bgcolors desiredjson["bordercolor"] = bordercolors json.dumps(desiredjson, sort_keys = false, indent = 4, separators=(',' , ': '))
here's looks like: (just sample block, outputs lot)
{ "type": "overall", "labels": "[\"overall\", \"over\"]", "bgcolor": "#ff7f8d", "bordercolor": "darken" }
i need follow format:
{ "type": "overall", "labels": ["overall", "over"], // note difference "bgcolor": "#ff7f8d", "bordercolor": "darken" }
** inserting list dic **
{ "type": "overall", "labels": [ "overall", "over" ], "bgcolor": "#ff7f8d", "bordercolor": "darken" }
this because created element string:
desiredjson["labels"] = '["%s", "%s"]' % (category_type, updatedlabels)
if want array in json, shoud set python list:
desiredjson["labels"] = [category_type, updated_labels]
python json
Comments
Post a Comment