简体   繁体   中英

Pandas dataframe to_json with nesting

I'm new to python and Pandas. Want to create nested JSON with below sample data. I saw posts where we can use groupby and to_Json to create nested json. The Issue I'm facing is that the nested json gets created as arrays. I'm looking for both, in one case the endpoint I'm going to call expects arrays and in other cases it expects single record. I'm having trouble getting this to work. Moreover, I have multiple such JSONs based on different dataframes and I would like to create generic method to create such json.

json_str = """[
{"StoreId" :111 ,
"StoreName" :"NeighbourhoodStore",
"City":"New Jersey",
"StreetAddress" :"23, Main Street",
"Zip" :12345,
"Monday"    :"9am - 6pm",
"Tuesday"   :"9am - 6pm",
"Wednesday" :"9am - 6pm",
"Thursday"  :"9am - 6pm",
"Friday"    :"9am - 6pm",
"Saturday"  :"10am - 5pm"},
{"StoreId" :112 ,
"StoreName" :"MainStreetStore",
"City":"New York",
"StreetAddress" :"1, Times Square",
"Zip" :12345,
"Monday"    :"9am - 9pm",
"Tuesday"   :"9am - 9pm",
"Wednesday" :"9am - 9pm",
"Thursday"  :"9am - 9pm",
"Friday"    :"9am - 9pm",
"Saturday"  :"10am - 5pm"}
]"""

data_list = json.loads(json_str)

Here is the expected output. Notice the OperationDays is array (I've not included all days there to keep it brief) whereas location is not array.

[
    {
        "StoreId": 111,
        "StoreName": "NeighbourhoodStore",
        "Location": {
            "City": "New York",
            "StreetAddress": "23, Main Street",
            "Zip": 12345
        },
        "OperationDays": [
            {
                "dayOfWeekName": "Monday",
                "hours": "9am-6pm",
            }
        ]
    },
    {
        "StoreId": 112,
        "StoreName": "MainStreet",
        "Location": {
            "City": "New York",
            "StreetAddress": "11 Times Square",
            "Zip": 11001
        },
        "OperationDays": [
            {
                "dayOfWeekName": "Tuesday",
                "hours": "9am-6pm",
            }
        ]
    }
]

I don't really see how we could take advantage of pandas in this case. You can however create a new dictionary list from data_list and export it to json:

new_list = []
days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
location_keys = {"City", "StreetAddress", "Zip"}

for dic in data_list:
    new_dic = {}
    for k,v in dic.items():
        if k in days:
            new_dic.setdefault("OperationDays", []).append({"dayOfWeekName": k, "hours": v})
        elif k in location_keys:
            new_dic.setdefault("Location", {})[k] = v
        else:
            new_dic[k] = v
    new_list.append(new_dic)

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

Output:

[
    {
        "StoreId": 111,
        "StoreName": "NeighbourhoodStore",
        "Location": {
            "City": "New Jersey",
            "StreetAddress": "23, Main Street",
            "Zip": 12345
        },
        "OperationDays": [
            {
                "dayOfWeekName": "Monday",
                "hours": "9am - 6pm"
            },
            {
                "dayOfWeekName": "Tuesday",
                "hours": "9am - 6pm"
            },
            {
                "dayOfWeekName": "Wednesday",
                "hours": "9am - 6pm"
            },
            {
                "dayOfWeekName": "Thursday",
                "hours": "9am - 6pm"
            },
            {
                "dayOfWeekName": "Friday",
                "hours": "9am - 6pm"
            },
            {
                "dayOfWeekName": "Saturday",
                "hours": "10am - 5pm"
            }
        ]
    },
    {
        "StoreId": 112,
        "StoreName": "MainStreetStore",
        "Location": {
            "City": "New York",
            "StreetAddress": "1, Times Square",
            "Zip": 12345
        },
        "OperationDays": [
            {
                "dayOfWeekName": "Monday",
                "hours": "9am - 9pm"
            },
            {
                "dayOfWeekName": "Tuesday",
                "hours": "9am - 9pm"
            },
            {
                "dayOfWeekName": "Wednesday",
                "hours": "9am - 9pm"
            },
            {
                "dayOfWeekName": "Thursday",
                "hours": "9am - 9pm"
            },
            {
                "dayOfWeekName": "Friday",
                "hours": "9am - 9pm"
            },
            {
                "dayOfWeekName": "Saturday",
                "hours": "10am - 5pm"
            }
        ]
    }
]

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