Reputation: 113
I am trying to change the JSON format using python. The received message has some key-value pairs and needs to change certain key names before forwarding the message. for normal key-value pairs, I have used "data. pop" method, data["newkey"]=data.pop("oldkey") . But it got complicated with nested key-values. This is just a part of big file that needs to be convrted. How to convert this
{
"atrk1": "form_varient",
"atrv1": "red_top",
"atrt1": "string",
"atrk2": "ref",
"atrv2": "XPOWJRICW993LKJD",
"atrt2": "string"
}
into this?
"attributes": {
"form_varient": {
"value": "red_top",
"type": "string"
},
"ref": {
"value": "XPOWJRICW993LKJD",
"type": "string"
}
}
Upvotes: 0
Views: 104
Reputation: 159
If the keys gonna be in the same format you can do something like this.
d = {
"ev": "contact_form_submitted",
"et": "form_submit",
"id": "cl_app_id_001",
"uid": "cl_app_id_001-uid-001",
"mid": "cl_app_id_001-uid-001",
"t": "Vegefoods - Free Bootstrap 4 Template by Colorlib",
"p": "http://shielded-eyrie-45679.herokuapp.com/contact-us",
"l": "en-US",
"sc": "1920 x 1080",
"atrk1": "form_varient",
"atrv1": "red_top",
"atrt1": "string",
"atrk2": "ref",
"atrv2": "XPOWJRICW993LKJD",
"atrt2": "string",
"uatrk1": "name",
"uatrv1": "iron man",
"uatrt1": "string",
"uatrk2": "email",
"uatrv2": "[email protected]",
"uatrt2": "string",
"uatrk3": "age",
"uatrv3": "32",
"uatrt3": "integer"
}
d["attributes"] = {}
d["traits"] = {}
keys_to_remove = []
for k in d.keys():
if k.startswith("atrk"):
value_key = k.replace("atrk","atrv")
type_key = k.replace("atrk","atrt")
d["attributes"][d[k]] = {"value":d[value_key],"type":d[type_key]}
keys_to_remove += [value_key,k,type_key]
if k.startswith("uatrk"):
keys_to_remove.append(k)
value_key = k.replace("uatrk","uatrv")
type_key = k.replace("uatrk","uatrt")
d["traits"][d[k]] = {"value":d[value_key],"type":d[type_key]}
keys_to_remove += [value_key,k,type_key]
for k in keys_to_remove:
if k in d:
del d[k]
Upvotes: 1
Reputation: 89
Use the following code it will successfully convert it.
json1={
"atrk1": "form_varient",
"atrv1": "red_top",
"atrt1": "string",
"atrk2": "ref",
"atrv2": "XPOWJRICW993LKJD",
"atrt2": "string"
}
json2={}
keys=[]
values=[]
types=[]
for i in json1:
if i[:4]=='atrk':
keys.append(json1[i])
values.append([])
types.append([])
elif i[:4]=='atrv':
values[int(i[-1:])-1].append(json1[i])
elif i[:4]=='atrt':
types[int(i[-1:])-1].append(json1[i])
for i in range(len(keys)):
json2[keys[i]]={
'value':values[i],'type':types[i]
}
json3={}
json3['attributes']=json2
print(json3)
Upvotes: 0