“Missing fields in json extraction” 可翻译为 “JSON提取中缺少的字段”。

huangapple go评论179阅读模式
英文:

Missing fields in json extraction

问题

I apologize for the confusion. It seems you want to extract specific information from a JSON structure. Here is the extracted information you requested:

From the provided JSON structure, you want to extract the following:

  1. For the "id": "hi9NAJ3prjwsHf2v8feSkNiZhp8YY":
    • "name": "Tapioca Pearls"
    • "catalog_object_id": "D3JB47IY6ZMBSNNUCYRBVCP6"

Here is a Python code snippet that extracts this information:

import json

# Load the JSON data from the file
with open("SQUARE_order.json", "r") as read_file:
    data = json.load(read_file)

# Iterate through the orders and line_items to find the desired information
for order in data['orders']:
    for item in order['line_items']:
        if 'modifiers' in item:
            for modifier in item['modifiers']:
                if modifier['name'] == 'Tapioca Pearls':
                    extracted_info = {
                        'id': order['id'],
                        'name': modifier['name'],
                        'catalog_object_id': modifier['catalog_object_id']
                    }
                    print(extracted_info)

This code will search for the specified modifier with the name "Tapioca Pearls" and extract the desired information when found.

英文:

I am facing an issue with missing fields in json extraction. following is how my json looks like:

{
    "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"
                            }
                        }
                    ]
                }
            ]
        }
    ]
}

I tried the following:

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']))

For example I want to extract: but with the above logic it doesnt give the outcome.

Please help: "id": "hi9NAJ3prjwsHf2v8feSkNiZhp8YY" I want to extract: "name": "Tapioca Pearls", "catalog_object_id": "D3JB47IY6ZMBSNNUCYRBVCP6".

答案1

得分: 1

这是一个Python脚本,它尝试通过匹配catalog_object_id来返回orderline_itemmodifier

结果:

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'}
英文:

Here is a Python script that will attempt to return the modifier of a line_item in an order by matching the catalog_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)

Result:

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'}

huangapple
  • 本文由 发表于 2023年6月9日 00:14:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76433860.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定