我从API和JSON数据结构入手,并且很难正确地整理具有该结构的文件。
print(json.dumps(json_data["orders"][5], sort_keys=True, indent=4))
{
"admin_graphql_api_id": "gid://shopify/Order/1805223231558",
"app_id": 580111,
"billing_address": {
"address1": "fsalj",
"address2": "lajsd",
"city": "aklsdas",
"company": null,
"country": "Denmark",
"country_code": "DK",
"first_name": "",
"last_name": "fo",
"latitude": null,
"longitude": null,
"name": "fo",
"phone": null,
"province": null,
"province_code": null,
"zip": "1234"
},
"browser_ip": "152.115.81.234",
"buyer_accepts_marketing": false,
"cancel_reason": null,
"cancelled_at": null,
"cart_token": "919658f9255451c77cb3f1736f54150e",
"checkout_id": 11639577313350,
"checkout_token": "712110938f2bb22e2fcfcc5352668296",
"client_details": {
"accept_language": "en-US,en;q=0.9",
"browser_height": 766,
"browser_ip": "152.115.81.234",
"browser_width": 1440,
"session_hash": "345c24262f7a38e8467d28c4686c3ffb",
"user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36"
},
"closed_at": null,
"confirmed": true,
"contact_email": "jhf+lko@organicbasics.com",
"created_at": "2019-10-18T14:27:47+02:00",
"currency": "DKK",
"customer": {
"accepts_marketing": false,
"accepts_marketing_updated_at": "2019-10-18T13:51:46+02:00",
"admin_graphql_api_id": "gid://shopify/Customer/2579389612102",
"created_at": "2019-10-18T13:51:46+02:00",
"currency": "DKK",
"default_address": {
"address1": "fsalj",
"address2": "lajsd",
"city": "aklsdas",
"company": null,
"country": "Denmark",
"country_code": "DK",
"country_name": "Denmark",
"customer_id": 2579389612102,
"default": true,
"first_name": "",
"id": 2742281863238,
"last_name": "fo",
"name": "fo",
"phone": null,
"province": null,
"province_code": null,
"zip": "1234"
},
"email": "jhf+lko@organicbasics.com",
"first_name": "",
"id": 2579389612102,
"last_name": "fo",
"last_order_id": 1805223231558,
"last_order_name": "OBTART1013",
"marketing_opt_in_level": null,
"multipass_identifier": null,
"note": null,
"orders_count": 1,
"phone": null,
"state": "disabled",
"tags": "",
"tax_exempt": false,
"tax_exemptions": [],
"total_spent": "309.00",
"updated_at": "2019-10-18T14:27:47+02:00",
"verified_email": true
},
"customer_locale": "en",
"device_id": null,
"discount_applications": [],
"discount_codes": [],
"email": "jhf+lko@organicbasics.com",
"financial_status": "paid",
"fulfillment_status": null,
"fulfillments": [],
"gateway": "gift_card",
"id": 1805223231558,
"landing_site": "/password",
"landing_site_ref": null,
"line_items": [
{
"admin_graphql_api_id": "gid://shopify/LineItem/4020212432966",
"destination_location": {
"address1": "fsalj",
"address2": "lajsd",
"city": "aklsdas",
"country_code": "DK",
"id": 1600208109638,
"name": "fo",
"province_code": "",
"zip": "1234"
},
"discount_allocations": [],
"fulfillable_quantity": 1,
"fulfillment_service": "manual",
"fulfillment_status": null,
"gift_card": false,
"grams": 0,
"id": 4020212432966,
"name": "Boxers 2-pack - Navy / S",
"origin_location": {
"address1": "Baldersgade 45",
"address2": "st th",
"city": "Copenhagen",
"country_code": "DK",
"id": 1027147235428,
"name": "Tarteletter",
"province_code": "",
"zip": "2200"
},
"pre_tax_price": "239.20",
"pre_tax_price_set": {
"presentment_money": {
"amount": "239.20",
"currency_code": "DKK"
},
"shop_money": {
"amount": "239.20",
"currency_code": "DKK"
}
},
"price": "299.00",
"price_set": {
"presentment_money": {
"amount": "299.00",
"currency_code": "DKK"
},
"shop_money": {
"amount": "299.00",
"currency_code": "DKK"
}
},
"product_exists": true,
"product_id": 2631134511204,
"properties": [],
"quantity": 1,
"requires_shipping": true,
"sku": "M-BXR-C-NA-S",
"tax_lines": [
{
"price": "59.80",
"price_set": {
"presentment_money": {
"amount": "59.80",
"currency_code": "DKK"
},
"shop_money": {
"amount": "59.80",
"currency_code": "DKK"
}
},
"rate": 0.25,
"title": "Moms"
}
],
"taxable": true,
"title": "Boxers 2-pack",
"total_discount": "0.00",
"total_discount_set": {
"presentment_money": {
"amount": "0.00",
"currency_code": "DKK"
},
"shop_money": {
"amount": "0.00",
"currency_code": "DKK"
}
},
"variant_id": 23536501817444,
"variant_inventory_management": "shopify",
"variant_title": "Navy / S",
"vendor": "ORGANIC COTTON"
}
],
"location_id": null,
"name": "OBTART1013",
"note": null,
"note_attributes": [],
"number": 13,
"order_number": 1013,
"order_status_url": "https://tarteletter.organicbasics.com/1396572260/orders/bc731580366deaa9f5fded639f81caa5/authenticate?key=8be7f8a50658d61c1b1a04c643c63bfb",
"payment_gateway_names": [
"gift_card"
],
"phone": null,
"presentment_currency": "DKK",
"processed_at": "2019-10-18T14:27:46+02:00",
"processing_method": "gift_cards_only",
"reference": null,
"referring_site": "",
"refunds": [],
"shipping_address": {
"address1": "fsalj",
"address2": "lajsd",
"city": "aklsdas",
"company": null,
"country": "Denmark",
"country_code": "DK",
"first_name": "",
"last_name": "fo",
"latitude": null,
"longitude": null,
"name": "fo",
"phone": null,
"province": null,
"province_code": null,
"zip": "1234"
},
"shipping_lines": [
{
"carrier_identifier": null,
"code": "Standard Shipping",
"delivery_category": null,
"discount_allocations": [],
"discounted_price": "10.00",
"discounted_price_set": {
"presentment_money": {
"amount": "10.00",
"currency_code": "DKK"
},
"shop_money": {
"amount": "10.00",
"currency_code": "DKK"
}
},
"id": 1488804872262,
"phone": null,
"price": "10.00",
"price_set": {
"presentment_money": {
"amount": "10.00",
"currency_code": "DKK"
},
"shop_money": {
"amount": "10.00",
"currency_code": "DKK"
}
},
"requested_fulfillment_service_id": null,
"source": "shopify",
"tax_lines": [],
"title": "Standard Shipping"
}
],
"source_identifier": null,
"source_name": "web",
"source_url": null,
"subtotal_price": "299.00",
"subtotal_price_set": {
"presentment_money": {
"amount": "299.00",
"currency_code": "DKK"
},
"shop_money": {
"amount": "299.00",
"currency_code": "DKK"
}
},
"tags": "",
"tax_lines": [
{
"price": "59.80",
"price_set": {
"presentment_money": {
"amount": "59.80",
"currency_code": "DKK"
},
"shop_money": {
"amount": "59.80",
"currency_code": "DKK"
}
},
"rate": 0.25,
"title": "Moms"
}
],
"taxes_included": true,
"test": false,
"token": "bc731580366deaa9f5fded639f81caa5",
"total_discounts": "0.00",
"total_discounts_set": {
"presentment_money": {
"amount": "0.00",
"currency_code": "DKK"
},
"shop_money": {
"amount": "0.00",
"currency_code": "DKK"
}
},
"total_line_items_price": "299.00",
"total_line_items_price_set": {
"presentment_money": {
"amount": "299.00",
"currency_code": "DKK"
},
"shop_money": {
"amount": "299.00",
"currency_code": "DKK"
}
},
"total_price": "309.00",
"total_price_set": {
"presentment_money": {
"amount": "309.00",
"currency_code": "DKK"
},
"shop_money": {
"amount": "309.00",
"currency_code": "DKK"
}
},
"total_price_usd": "46.01",
"total_shipping_price_set": {
"presentment_money": {
"amount": "10.00",
"currency_code": "DKK"
},
"shop_money": {
"amount": "10.00",
"currency_code": "DKK"
}
},
"total_tax": "59.80",
"total_tax_set": {
"presentment_money": {
"amount": "59.80",
"currency_code": "DKK"
},
"shop_money": {
"amount": "59.80",
"currency_code": "DKK"
}
},
"total_tip_received": "0.0",
"total_weight": 0,
"updated_at": "2019-10-18T14:27:50+02:00",
"user_id": null
}
我知道函数json_normalize,但是问题是,当它到达“ line_items”时,json_normalize无法处理并将所有内容放置在DF中的同一列和同一行下。 据我了解,“ Line_item”是另一个字典/对象,我的最终目标是将json扁平化,为“ line_items”外部的所有key:value创建一列,并为“ line_items”内部的每个项目创建一个新行;并且“ line_items”之外的信息会重复。
我也尝试过下面的方法,定义“路径”。但是我遇到了这个错误:SyntaxError:位置参数紧跟关键字参数
df2 = json_normalize(json_data['orders'], sep = "_",
record_path = "line_items",
meta =
"fulfillable_quantity",
"fulfillment_service",
"fulfillment_status",
"grams",
"id",
"price",
"product_id",
"quantity",
"requires_shipping",
"sku",
"title",
"variant_id",
"variant_title",
"vendor",
"name",
"gift_card",
"properties",
"taxable",
"tax_lines",
"total_discount",
"total_discount_set",
"discount_allocations",
meta_prefix = "line_")
有关如何进行此转换的任何建议?