json 提取中缺少字段

Missing fields in json extraction

提问人:Aiss 提问时间:6/9/2023 最后编辑:h0r53Aiss 更新时间:6/9/2023 访问量:50

问:

我遇到了 json 提取中缺少字段的问题。以下是我的 JSON 的样子:

{
    "orders": [
        {
            "id": "f7w0KvbCMln8snSrNuPm5H2t2GSZY",
            "location_id": "8V3SAM09X4AQF",
            "line_items": [
                {
                    "uid": "1",
                    "catalog_object_id": "EBUX2MILP4UJ76CEE5MBIQJA",
                    "catalog_version": 1681426511907,
                    "quantity": "1",
                    "name": "Minh's roll",
                    "variation_name": "Regular",
                    "base_price_money": {
                        "amount": 725,
                        "currency": "CAD"
                    },
                    "note": "",
                    "gross_sales_money": {
                        "amount": 725,
                        "currency": "CAD"
                    },
                    "total_tax_money": {
                        "amount": 36,
                        "currency": "CAD"
                    },
                    "total_discount_money": {
                        "amount": 0,
                        "currency": "CAD"
                    },
                    "total_money": {
                        "amount": 761,
                        "currency": "CAD"
                    },
                    "variation_total_price_money": {
                        "amount": 725,
                        "currency": "CAD"
                    },
                    "applied_taxes": [
                        {
                            "uid": "OLSBPOGb776jAh7GfxfTq",
                            "tax_uid": "9",
                            "applied_money": {
                                "amount": 36,
                                "currency": "CAD"
                            }
                        }
                    ],
                    "item_type": "ITEM",
                    "total_service_charge_money": {
                        "amount": 0,
                        "currency": "CAD"
                    }
                },
                {
                    "uid": "2",
                    "catalog_object_id": "FBKFLKQHT5DQXA7C2BOOA7QZ",
                    "catalog_version": 1681426511907,
                    "quantity": "1",
                    "name": "Royal Milk Tea",
                    "variation_name": "Regular",
                    "base_price_money": {
                        "amount": 550,
                        "currency": "CAD"
                    },
                    "modifiers": [
                        {
                            "uid": "3",
                            "base_price_money": {
                                "amount": 50,
                                "currency": "CAD"
                            },
                            "total_price_money": {
                                "amount": 50,
                                "currency": "CAD"
                            },
                            "name": "Tapioca Pearls",
                            "catalog_object_id": "D3JB47IY6ZMBSNNUCYRBVCP6",
                            "catalog_version": 1681426511907,
                            "quantity": "1"
                        },
                        {
                            "uid": "4",
                            "base_price_money": {
                                "amount": 0,
                                "currency": "CAD"
                            },
                            "total_price_money": {
                                "amount": 0,
                                "currency": "CAD"
                            },
                            "name": "1/2 Sugar",
                            "catalog_object_id": "L634LGNPEUWPGY3KA6CPZ6RH",
                            "catalog_version": 1681426511907,
                            "quantity": "1"
                        },
                        {
                            "uid": "5",
                            "base_price_money": {
                                "amount": 0,
                                "currency": "CAD"
                            },
                            "total_price_money": {
                                "amount": 0,
                                "currency": "CAD"
                            },
                            "name": "1/2 Ice",
                            "catalog_object_id": "GPISTPCC76ZPISXD7WDZQ6RJ",
                            "catalog_version": 1681426511907,
                            "quantity": "1"
                        }
                    ],
                    "note": "",
                    "gross_sales_money": {
                        "amount": 600,
                        "currency": "CAD"
                    },
                    "total_tax_money": {
                        "amount": 30,
                        "currency": "CAD"
                    },
                    "total_discount_money": {
                        "amount": 0,
                        "currency": "CAD"
                    },
                    "total_money": {
                        "amount": 630,
                        "currency": "CAD"
                    },
                    "variation_total_price_money": {
                        "amount": 550,
                        "currency": "CAD"
                    },
                    "applied_taxes": [
                        {
                            "uid": "vZUmVv8HhcYV9j1hvfDCJ",
                            "tax_uid": "9",
                            "applied_money": {
                                "amount": 30,
                                "currency": "CAD"
                            }
                        }
                    ],
                    "item_type": "ITEM",
                    "total_service_charge_money": {
                        "amount": 0,
                        "currency": "CAD"
                    }
                },
                {
                    "uid": "6",
                    "catalog_object_id": "PGMSZP2KCPT4BPJVRND2Y7TZ",
                    "catalog_version": 1681426511907,
                    "quantity": "2",
                    "name": "Pho Ga",
                    "variation_name": "Regular",
                    "base_price_money": {
                        "amount": 1650,
                        "currency": "CAD"
                    },
                    "modifiers": [
                        {
                            "uid": "7",
                            "base_price_money": {
                                "amount": 0,
                                "currency": "CAD"
                            },
                            "total_price_money": {
                                "amount": 0,
                                "currency": "CAD"
                            },
                            "name": "Rice Noodles",
                            "catalog_object_id": "THNRPUH447QAPTCPL5ZMJU5E",
                            "catalog_version": 1681426511907,
                            "quantity": "1"
                        }
                    ],
                    "note": "",
                    "gross_sales_money": {
                        "amount": 3300,
                        "currency": "CAD"
                    },
                    "total_tax_money": {
                        "amount": 165,
                        "currency": "CAD"
                    },
                    "total_discount_money": {
                        "amount": 0,
                        "currency": "CAD"
                    },
                    "total_money": {
                        "amount": 3465,
                        "currency": "CAD"
                    },
                    "variation_total_price_money": {
                        "amount": 3300,
                        "currency": "CAD"
                    },
                    "applied_taxes": [
                        {
                            "uid": "8jLMPWQ0ihC3C9x48dq6LD",
                            "tax_uid": "9",
                            "applied_money": {
                                "amount": 165,
                                "currency": "CAD"
                            }
                        }
                    ],
                    "item_type": "ITEM",
                    "total_service_charge_money": {
                        "amount": 0,
                        "currency": "CAD"
                    }
                }
            ],
            "taxes": [
                {
                    "uid": "9",
                    "name": "Delivery Tax",
                    "percentage": "4.995",
                    "type": "ADDITIVE",
                    "applied_money": {
                        "amount": 231,
                        "currency": "CAD"
                    },
                    "scope": "ORDER"
                }
            ],
            "fulfillments": [
                {
                    "uid": "8",
                    "type": "PICKUP",
                    "state": "COMPLETED",
                    "pickup_details": {
                        "pickup_at": "2023-04-13T23:35:13.000Z",
                        "note": "Delivery by Doordash\nNOTE: Lisa B",
                        "placed_at": "2023-04-13T23:21:10.883Z",
                        "accepted_at": "2023-04-14T03:30:25.315Z",
                        "ready_at": "2023-04-14T03:30:25.315Z",
                        "schedule_type": "ASAP",
                        "recipient": {
                            "display_name": "Lisa B",
                            "phone_number": "+12497022887",
                            "address": {
                                "address_line_1": "",
                                "administrative_district_level_1": "",
                                "first_name": "Lisa",
                                "last_name": "B"
                            }
                        },
                        "picked_up_at": "2023-04-14T03:30:25.315Z"
                    }
                }
            ],
            "created_at": "2023-04-13T23:21:10.037Z",
            "updated_at": "2023-04-14T03:30:25.316Z",
            "state": "COMPLETED",
            "version": 6,
            "reference_id": "a629b6f6",
            "total_tax_money": {
                "amount": 231,
                "currency": "CAD"
            },
            "total_discount_money": {
                "amount": 0,
                "currency": "CAD"
            },
            "total_tip_money": {
                "amount": 0,
                "currency": "CAD"
            },
            "total_money": {
                "amount": 4856,
                "currency": "CAD"
            },
            "closed_at": "2023-04-14T03:30:25.316Z",
            "tenders": [
                {
                    "id": "LdwMfHKagHGPC4TWfaonReYjL0GZY",
                    "location_id": "8V3SAM09X4AQF",
                    "transaction_id": "hi9NAJ3prjwsHf2v8feSkNiZhp8YY",
                    "created_at": "2023-04-13T23:21:10Z",
                    "amount_money": {
                        "amount": 4856,
                        "currency": "CAD"
                    },
                    "type": "OTHER",
                    "tip_money": {
                        "amount": 0,
                        "currency": "CAD"
                    },
                    "payment_id": "LdwMfHKagHGPC4TWfaonReYjL0GZY"
                }
            ],
            "total_service_charge_money": {
                "amount": 0,
                "currency": "CAD"
            },
            "net_amounts": {
                "total_money": {
                    "amount": 4856,
                    "currency": "CAD"
                },
                "tax_money": {
                    "amount": 231,
                    "currency": "CAD"
                },
                "discount_money": {
                    "amount": 0,
                    "currency": "CAD"
                },
                "tip_money": {
                    "amount": 0,
                    "currency": "CAD"
                },
                "service_charge_money": {
                    "amount": 0,
                    "currency": "CAD"
                }
            },
            "source": {
                "name": "DoorDash"
            },
            "net_amount_due_money": {
                "amount": 0,
                "currency": "CAD"
            }
        },
        {
            "id": "r9EV9dqgCtFTKxvxLLDOuV7m5DeZY",
            "location_id": "8V3SAM09X4AQF",
            "created_at": "2023-04-13T23:15:17.925Z",
            "updated_at": "2023-04-13T23:15:19.000Z",
            "state": "COMPLETED",
            "version": 4,
            "closed_at": "2023-04-13T23:15:18.273Z",
            "returns": [
                {
                    "uid": "7m4NHLUPMLkTmuLhtPkMRD",
                    "source_order_id": "PfyOwZLVSnNwKmhYwhNiSgVs7QeZY",
                    "return_line_items": [
                        {
                            "uid": "NeX5lw1mdclSazsaAFXzdB",
                            "quantity": "1",
                            "item_type": "CUSTOM_AMOUNT",
                            "base_price_money": {
                                "amount": 3678,
                                "currency": "CAD"
                            },
                            "variation_total_price_money": {
                                "amount": 3678,
                                "currency": "CAD"
                            },
                            "gross_return_money": {
                                "amount": 3678,
                                "currency": "CAD"
                            },
                            "total_tax_money": {
                                "amount": 0,
                                "currency": "CAD"
                            },
                            "total_discount_money": {
                                "amount": 0,
                                "currency": "CAD"
                            },
                            "total_money": {
                                "amount": 3678,
                                "currency": "CAD"
                            },
                            "total_service_charge_money": {
                                "amount": 0,
                                "currency": "CAD"
                            }
                        }
                    ]
                }
            ]
        }
    ]
}

我尝试了以下方法:

print("Started Reading JSON file")
with open("SQUARE_order.json", "r") as read_file:
print("Converting JSON encoded data into Python dictionary")`
data = json.load(read_file)
for order in data['orders']:
  for item in order['line_items']:
    for mod in modifiers:
      print((order['id'],item['name'],mod['name'], mod['catalog_object_id'], item['base_price_money']))

例如,我想提取: 但是使用上述逻辑,它不会给出结果。

请帮忙: “id”: “hi9NAJ3prjwsHf2v8feSkNiZhp8YY” 我想提取: “name”: “木薯珍珠”, “catalog_object_id”: “D3JB47IY6ZMBSNNUCYRBVCP6”。

python 数组 json 解析

评论

0赞 h0r53 6/9/2023
“我面临 json 提取中缺少字段的问题。” - 请提供您收到的错误。
0赞 h0r53 6/9/2023
我已经尽我所能正确地格式化了您的代码,但一开始就不清楚。如果格式无意中更改了您的代码,请检查并修改。
0赞 Aiss 6/9/2023
我没有这样的错误。只是我无法提取的那几个项目:例如:catalog_object_id“: ”D3JB47IY6ZMBSNNUCYRBVCP6“ 及其相对名称
0赞 h0r53 6/9/2023
当您说要提取时,您具体尝试提取哪些数据?您是否只是尝试索引到 JSON 中以检索该特定对象?或者您是否正在寻找一种更通用的方法来按名称/ID 标识对象并返回它?
0赞 Aiss 6/9/2023
通过名称/ID 标识对象并返回对象的通用方法

答:

1赞 h0r53 6/9/2023 #1

下面是一个 Python 脚本,它将尝试通过匹配 .modifierline_itemordercatalog_object_id

import json
print("Started Reading JSON file")
with open("SQUARE_order.json", "r") as read_file:
    print("Converting JSON encoded data into Python dictionary")
    data = json.load(read_file)

def find_modifier(catalog_object_id, order_object):
    if "orders" in order_object:
        for order in order_object["orders"]:
            if "line_items" in order:
                for item in order["line_items"]:
                    if "modifiers" in item:
                        for modifier in item["modifiers"]:
                            if modifier["catalog_object_id"] == catalog_object_id:
                                return modifier
    print(f"Modifier not found for catalog object ID: {catalog_object_id}")


modifier = find_modifier("D3JB47IY6ZMBSNNUCYRBVCP6", data)
if modifier:
    print(modifier)

结果:

Started Reading JSON file
Converting JSON encoded data into Python dictionary
{'uid': '3', 'base_price_money': {'amount': 50, 'currency': 'CAD'}, 'total_price_money': {'amount': 50, 'currency': 'CAD'}, 'name': 'Tapioca Pearls', 'catalog_object_id': 'D3JB47IY6ZMBSNNUCYRBVCP6', 'catalog_version': 1681426511907, 'quantity': '1'}

评论

0赞 Aiss 6/9/2023
多谢。你的答案是正确的
0赞 h0r53 6/9/2023
@Aiss我很高兴这对你有用。请考虑接受答案,以便我获得学分。谢谢!