I'm receiving orders as JSON where multiple order-items are added as a list like so:
SAMPLE_ORDER
{
"importDate": "2020-03-18T10:03:19.194336",
"status": "shipped",
"orderNumber": 123456,
"orderItem": [
{
"sku": 998877,
"itemName": "Doomsday device",
"netCost": 8.7,
"quantity": 1
},
{
"sku": 665544,
"itemName": "Fing longerer",
"netCost": 99.9,
"quantity": 1
}
],
"addressData": {
"recipientCompany": "Planet Express, Inc",
"recipientName": "Farnsworth",
"recipientFirstName": "Hubert",
"recipientStreet": "72nd Street",
"recipientHouseNumber": null,
"recipientAnnex": null,
"recipientZip": "NNY 10023",
"recipientCity": "New New York",
"recipientCountry": "USA",
"recipientEmail": "hubert@farnsworth.com",
"recipientPhone": "+123 445-566-7789",
"recipientMobilePhone": "+123 444 555 666 777"
}
}
I need to "convert" those JSON-orders into CSV where I create a new row for each ordered item. Above sample would result in a CSV with two rows. This works fine when adding all data the "manual" way - meaning adding address-data like "recipientCompany": order_payload["addressData"]["recipientCompany"]
.
What I would like to do is adding the whole dictionary order_payload["addressData"]
w/o adding each field manually.
I've tried using extend
but I only add the keys to the dict and I don't know how to ust list/dict-comprehension "inside" my orders.append()
I've tried to add the key-value-pairs by using something like
(key: value for (key, value) in order_payload["externalReferences"])
but that didn't work either.
I'm sure there is a (very) easy way but I didn't find an answer that helped me in this regard
# order_payload is just a 'json.loads' of the above order
def main(order_payload):
orders = []
for order_item in order_payload["orderItem"]:
orders.append({
"orderDate": order_payload["importDate"],
"sku": order_item["sku"],
"itemName": order_item["itemName"]
# TODO dynamically add contents of order_payload["addressData"]
})
Thanks in advance