简体   繁体   中英

Flattening nested Json file into pandas dataframe

I have this json file

    {
    "OrderMaster": {
        "Order": {
            "item": [{
                "row_id": "1-2LDPVI0",
                "sequence_id": "3851101",
                "end_date": "",
                "name": "TV-Discount",
                "orderable": "Y",
                "period": "",
                "period_uom": "",
                "phone_number_flag": "N",
                "price_type": "Recurring",
                "product_category": "mobilepackage",
                "product_sub_category": "Discount",
                "product_type_code": "Product",
                "type": "PhoneOrder",
                "vendor_part_number": "",
                "created_date": "2018-02-16 09:09:24",
                "created_by": "id123",
                "last_updated_date": "2020-09-14 09:39:24",
                "last_updated_by": "id123",
                "ts_event_notification_time": "2020-09-14 09:40:69",
                "OrderItems": {
                    "item": [{
                        "original_list_price": "0",
                        "order_list_id": "1-4ABU",
                        "order_list_name": "SEK Pricelist",
                        "product_id": "1-2LDPUKX",
                        "start_date": "2018-02-17 00:00:00"
                    },
                    {
                        "original_list_price": "45",
                        "order_list_id": "1-4AFU",
                        "order_list_name": "SEK Pricelist",
                        "product_id": "1-2LGSDFUKX",
                        "start_date": "2018-02-18 00:04:20"
                    }]
                }
            },
            {
                "row_id": "1-2LDPVI0",
                "sequence_id": "3851101",
                "end_date": "",
                "name": "TV-Discount",
                "orderable": "Y",
                "period": "",
                "period_uom": "",
                "phone_number_flag": "N",
                "price_type": "Recurring",
                "product_category": "mobilepackage",
                "product_sub_category": "Discount",
                "product_type_code": "Product",
                "type": "PhoneOrder",
                "vendor_part_number": "",
                "created_date": "2018-02-16 09:19:24",
                "created_by": "id123",
                "last_updated_date": "2020-09-15 09:39:24",
                "last_updated_by": "id123",
                "ts_event_notification_time": "2020-09-14 09:40:28",
                "OrderItems": {
                    "item": [{
                        "original_list_price": "42",
                        "order_list_id": "1-4ABU",
                        "order_list_name": "SEK Pricelist",
                        "product_id": "1-2LDPUKX",
                        "start_date": "2018-02-19 00:00:00"
                    },
                    {
                        "original_list_price": "42",
                        "order_list_id": "1-4ASU",
                        "order_list_name": "SEK Pricelist",
                        "product_id": "1-2LDDAKX",
                        "start_date": "2018-02-12 00:00:00"
                    },
                    {
                        "original_list_price": "43",
                        "order_list_id": "1-4FDBU",
                        "order_list_name": "SEK Pricelist",
                        "product_id": "1-2LDFSDFKX",
                        "start_date": "2018-02-11 00:00:00"
                    }]
                }
            }]
        }
    }
}

This is what I'm trying to achieve: 在此处输入图像描述

So far I have managed to get to this point But I have problem with last nested column 'OrderItem' column. I managed to extract it but have hard time figuring out how to concatenate them together like in target result.

I managed to solve this by using json_normalise with correct set of parameters

with open(file_path) as f:
    data = json.load(f)

# Define feature list for dataframe
features = [
    "row_id",
    "sequence_id",
    "end_date",
    "name",
    "orderable",
    "period",
    "period_uom",
    "phone_number_flag",
    "price_type",
    "product_category",
    "product_sub_category",
    "product_type_code",
    "type",
    "vendor_part_number",
    "created_date",
    "created_by",
    "last_updated_date",
    "last_updated_by",
    "ts_event_notification_time"
]

# Create dataframe using json_normalize pandas function with necessary parameters
df = pd.json_normalize(data['OrderMaster']['Order']['item'],['OrderItems', 'item'], features)

Result is full rows of data for each item:结果

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