简体   繁体   中英

Convert CSV to Nested JSON complex structure using Pandas

Converted into a nested JSON file using Pandas

This is the sample csv for one row

name      type  aitm      alitm     aaitm           adsc1   
specs     glass 70072187  ESA65Z45  ESA 65Z45       CUT TIP FG 1808-40  

I'm trying to achieve the below structure of Nested JSON for every row

import pandas as pd
import json

df = pd.DataFrame([['specs','glass','70072187','ESA65Z45','ESA 65Z45','CUT TIP FG 1808-40'],
                   ['specs','glass','666','ESA6665','ESB 666','CUT TIP FG 66-40']],
                       columns = ['name', 'type','aitm','alitm','aaitm','adsc1' ])


data = {'entities':[]}
for key,grp in df.groupby('name'):
    for idx, row in grp.iterrows():
        temp_dict_alpha = {'name':key, 'type':row['type'], 'data':{'attributes':{}}}

        attr_row = row[~row.index.isin(['name','type'])]
        for idx2, row2 in attr_row.iteritems():
            dict_temp = {}
            dict_temp[idx2] = {'values':[]}
            dict_temp[idx2]['values'].append({'value':row2,'source':'internal','locale':'en_US'})

            temp_dict_alpha['data']['attributes'].update(dict_temp)

        data['entities'].append(temp_dict_alpha)


print(json.dumps(data, indent= 4))   

Output:

print(json.dumps(data, indent= 4))
{
    "entities": [
        {
            "name": "specs",
            "type": "glass",
            "data": {
                "attributes": {
                    "aitm": {
                        "values": [
                            {
                                "value": "70072187",
                                "source": "internal",
                                "locale": "en_US"
                            }
                        ]
                    },
                    "alitm": {
                        "values": [
                            {
                                "value": "ESA65Z45",
                                "source": "internal",
                                "locale": "en_US"
                            }
                        ]
                    },
                    "aaitm": {
                        "values": [
                            {
                                "value": "ESA 65Z45",
                                "source": "internal",
                                "locale": "en_US"
                            }
                        ]
                    },
                    "adsc1": {
                        "values": [
                            {
                                "value": "CUT TIP FG 1808-40",
                                "source": "internal",
                                "locale": "en_US"
                            }
                        ]
                    }
                }
            }
        },
        {
            "name": "specs",
            "type": "glass",
            "data": {
                "attributes": {
                    "aitm": {
                        "values": [
                            {
                                "value": "666",
                                "source": "internal",
                                "locale": "en_US"
                            }
                        ]
                    },
                    "alitm": {
                        "values": [
                            {
                                "value": "ESA6665",
                                "source": "internal",
                                "locale": "en_US"
                            }
                        ]
                    },
                    "aaitm": {
                        "values": [
                            {
                                "value": "ESB 666",
                                "source": "internal",
                                "locale": "en_US"
                            }
                        ]
                    },
                    "adsc1": {
                        "values": [
                            {
                                "value": "CUT TIP FG 66-40",
                                "source": "internal",
                                "locale": "en_US"
                            }
                        ]
                    }
                }
            }
        }
    ]
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM