0

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"
        }
    }

2 Answers 2

1

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]
Sign up to request clarification or add additional context in comments.

5 Comments

Hi, as I said this is a part of a bigger JSON file. Your method is yielding results but, it is creating additional key-value pairs and the old ones still remain in the file. @Robin
If you don't want the old data. How about replaying the file content with new results?
I have updated the code. But now it seems like I hardcoded a lot. I don't know about the complete dataset. Let me know if this code not worked.
Hi, I have converted the keys into a list and later used del method to delete the unwanted items. Thanks for for the solution above. @robin
your previous solution worked fine for me. As I said I used it in combination with the delete method. Can you change your solution back to the previous one? I think it is more effective if we are working with dynamic changes.
0

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)

1 Comment

unfortunately, i cannot use this as keys with the name prefix "atrk" change dynamically.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.