简体   繁体   English

如何使用 python 中的分组键将 OrderedDict 列表转换为嵌套 json

[英]How to convert a list of OrderedDict to nested json with grouped keys in python

I'm working on a project where I need to convert a set of data rows from database into list of OrderedDict for other purpose and use this list of OrderedDict to convert into a nested JSON format in python .我正在做一个项目,我需要将数据库中的一组数据行转换list of OrderedDict用于其他目的,并使用此list of OrderedDict转换为 python 中的nested JSON python I'm starting to learn python.我开始学习 python。 I was able convert the query response from database which is a list of lists to list of OrderedDict .我能够将来自数据库的查询响应( list of lists转换list of OrderedDict

I have the list of OrderedDict as below:list of OrderedDict如下:

    {
'OUTBOUND': [
OrderedDict([('Leg', 1), ('SessionID', 'W12231fwfegwcaa2'),('FeeCode', 'ATO'),('SeatGroup', '2'),
               ('Currency', 'MXN'),('Modality', 'VB'),('BookingClass', 'A'),('Price', 145.0),('Num_Pax', 1),('Channel', 'Web')]),
  OrderedDict([('Leg', 1),('SessionID', 'W12231fwfegwcaa2'),('FeeCode', 'ATO'),('SeatGroup', '4'),
               ('Currency', 'MXN'),('Modality', 'VB'),('BookingClass', 'A'),('Price', 111.0),('Num_Pax', 1),('Channel', 'Web')]),
  OrderedDict([('Leg', 1),('SessionID', 'W12231fwfegwcaa2'),('FeeCode', 'BDM'),('SeatGroup', 'null'),
               ('Currency', 'MXN'),('Modality', 'VB'),('BookingClass', 'A'),('Price', 111.0),('Num_Pax', 1),('Channel', 'Web')]),
  OrderedDict([('Leg', 2),('SessionID', 'W12231fwfegwcaa2'),('FeeCode', 'ATO'),('SeatGroup', '1'),
                ('Currency', 'MXN'),('Modality', 'VB'),('BookingClass', 'U'),('Price', 180.0),('Num_Pax', 1),('Channel', 'Web'))]),
  OrderedDict([('Leg', 2),('SessionID', 'W12231fwfegwcaa2'),('FeeCode', 'ATO'),('SeatGroup', '4'),
                ('Currency', 'MXN'),('Modality', 'VB'),('BookingClass', 'U'),('Price', 97.0),('Num_Pax', 1),('Channel', 'Web')]),
  OrderedDict([('Leg', 2),('SessionID', 'W12231fwfegwcaa2'),('FeeCode', 'BDM'),('SeatGroup', 'null'),
                ('Currency', 'MXN'),('Modality', 'VB'),('BookingClass', 'U'),('Price', 97.0),('Num_Pax', 1),('Channel', 'Web')])
            ]
}

And I needed the nested format like below:我需要如下嵌套格式:

{
"OUTBOUND": [
    {
      "Leg": 1,
      "SessionID": "W12231fwfegwcaa2",
      "Modality": "VB",
      "BookingClass": "A",
      "FeeCodes":[
                    {
                        "FeeCode": "ATO",
                        "Prices":
                        [
                            {
                                "SeatGroup": "2",
                                "Price": 145.0,
                                "Currency": "MXN"
                            },
                            {
                                "SeatGroup": "4",
                                "Price": 111.0,
                                "Currency": "MXN"
                            }
                        ]
                    },
                    {
                        "FeeCode": "VBABDM",                
                        "Prices":
                        [ 
                            {
                                "SeatGroup": "null",
                                "Price": 111.0,
                                "Currency": "MXN"                   
                            }
                        ]
                    }
                ],
      "Num_Pax": 1,
      "Channel": "Web"
    },
    {
      "Leg": 2,
      "SessionID": "W12231fwfegwcaa2",
      "Modality": "VB",
      "BookingClass": "U",
      "FeeCodes":[
                    {
                        "FeeCode": "ATO",
                        "Prices":
                        [
                            {
                                "SeatGroup": "1",
                                "Price": 180.0,
                                "Currency": "MXN"
                            },
                            {
                                "SeatGroup": "4",
                                "price": 97.0,
                                "Currency": "MXN"
                            }
                        ]
                    },
                    {
                        "FeeCode": "VBABDM",                
                        "Prices":
                        [ 
                            {
                                "SeatGroup": "null",
                                "price": 97.0,
                                "Currency": "MXN"                   
                            }
                        ]
                    }
                ],
      "Num_Pax": 1,
      "Channel": "Web"
    }
    ]
}

If I'm not wrong, I need to group by Leg , SessionID , Modality , BookingClass , NumPax and Channel and group the FeeCode , SeatGroup , Price and Currency into nested format as above but unable to move ahead with how to loop and group for nesting.如果我没记错的话,我需要按LegSessionIDModalityBookingClassNumPaxChannel分组,并将FeeCodeSeatGroupPriceCurrency分组为嵌套格式,但无法继续讨论如何循环和分组嵌套。

It would be great if I could get some help.如果我能得到一些帮助,那就太好了。 Thanks谢谢

I was able to write a python code to get the format as I needed using simple looping with a couple of changes in the output like the fields SessionID, Num_Pax and Channel is taken outside then the OUTBOUND field and fields within are generated.我能够编写一个 python 代码来获得所需的格式,使用简单的循环并在 output 中进行一些更改,例如字段 SessionID、Num_Pax 和 Channel 被取出然后生成 OUTBOUND 字段和其中的字段。

Instead of OrderedDict, I used a list of lists as input which I convert into Pandas DataFrame and work with the DataFrame to get the nested format.我没有使用 OrderedDict,而是使用列表列表作为输入,将其转换为 Pandas DataFrame 并使用 DataFrame 来获得嵌套格式。

Below is the code I used:下面是我使用的代码:

outbound_df = pd.DataFrame(response_outbound,columns=All_columns)
Common_columns = ['Leg', 'Modality', 'BookingClass']

### Taking SessionID, AirlineCode,Num_Pax and Channel outside OUTBOUND part as they are common for all the leg level data
response_data['SessionID'] = outbound_df['SessionID'].unique()[0]   
response_data['Num_Pax'] = int(outbound_df['Num_Pax'].unique()[0])
response_data['Channel'] = outbound_df['Channel'].unique()[0]

temp_data = []
Legs = outbound_df['Leg'].unique()

for i in Legs:
    subdata = outbound_df[outbound_df['Leg']==i]
    
    ### Initializing leg_data dict
    leg_data = collections.OrderedDict()
        
    ### Populating common fields of the leg (Leg, Modality,BookingClass)
    for j in Common_columns: 
        if(j=='Leg'):
            leg_data[j] = int(subdata[j].unique()[0])
        else:
            leg_data[j] = subdata[j].unique()[0]
    
    leg_data['FeeCodes'] = []
    FeeCodes = subdata['FeeCode'].unique()
    
    for fc in FeeCodes:
        subdata_fees = subdata[subdata['FeeCode']==fc]
        
        Prices = {'FeeCode':fc, "Prices":[]}
        
        for _,rows in subdata_fees.iterrows():
            data = {}
            data['SeatGroup'] = rows['SeatGroup']
            data['Price'] = float(rows['Price'])
            data['Currency'] = rows['Currency']
            
            Prices["Prices"].append(data)
        
        leg_data["FeeCodes"].append(Prices)
    temp_data.append(leg_data)

response_data["OUTBOUND"] = temp_data

I can just do json.dumps on response_data to get json format which will be sent to the next steps.我可以在response_data上执行json.dumps以获得 json 格式,该格式将发送到下一步。

Below is the output format I get:下面是我得到的 output 格式:

{
   "SessionID":"W12231fwfegwcaa2",
   "Num_Pax":1,
   "Channel":"Web",
   "OUTBOUND":[
      {
         "Leg":1,
         "Modality":"VB",
         "BookingClass":"A",
         "FeeCodes":[
            {
               "FeeCode":"ATO",
               "Prices":[
                  {
                     "SeatGroup":"2",
                     "Price":145.0,
                     "Currency":"MXN"
                  },
                  {
                     "SeatGroup":"4",
                     "Price":111.0,
                     "Currency":"MXN"
                  }
               ]
            },
            {
               "FeeCode":"VBABDM",
               "Prices":[
                  {
                     "SeatGroup":"null",
                     "Price":111.0,
                     "Currency":"MXN"
                  }
               ]
            }
         ]
      },
      {
         "Leg":2,
         "Modality":"VB",
         "BookingClass":"U",
         "FeeCodes":[
            {
               "FeeCode":"ATO",
               "Prices":[
                  {
                     "SeatGroup":"1",
                     "Price":180.0,
                     "Currency":"MXN"
                  },
                  {
                     "SeatGroup":"4",
                     "price":97.0,
                     "Currency":"MXN"
                  }
               ]
            },
            {
               "FeeCode":"VBABDM",
               "Prices":[
                  {
                     "SeatGroup":"null",
                     "price":97.0,
                     "Currency":"MXN"
                  }
               ]
            }
         ]
      }
   ]
}

Please let me know if we can shorten the code in terms of lengthy iterations or any other changes.请让我知道我们是否可以在冗长的迭代或任何其他更改方面缩短代码。 Thanks.谢谢。 PS: Sorry for my editing mistakes PS:抱歉我的编辑错误

Assuming that you stored the dictionary to some variable foo , you can do:假设您将字典存储到某个变量foo ,您可以执行以下操作:

import json

json.dumps(foo)

And be careful, you added extra bracket in the 4th element OUTBOUND list请注意,您在第 4 个元素OUTBOUND列表中添加了额外的括号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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