【问题标题】:python merge multiple json requests to a single file and save itpython将多个json请求合并到一个文件并保存
【发布时间】:2016-12-15 12:06:09
【问题描述】:

我知道这个要求有点重。不确定是否有人可以提供帮助,但如果可以,我将不胜感激。

我正在尝试在 python 中处理 XML 数据,这对我来说是新的。我正在尝试编写一个脚本来解析几个 json 请求并将它们组合成一个。我想每隔 x 时间将此文件输入一个专有系统,以使所有数据在一个位置保持最新。

            {
                "items": [{
                    "id": 333512,
                    "full_name": "Flooring",
                    "instock": true,
                    "dept": "none",
                    "stockid": 4708384,
                    "commonname": "StdFloor",
                    "reorder": true
                }, {
                    "id": 3336532,
                    "full_name": "Standard Tool",
                    "instock": true,
                    "dept": "none",
                    "stockid": 4708383,
                    "commonname": "StandardTool",
                    "reorder": true
                }]
            }

其中 200 多个将在初始请求中返回

首先,我需要从每个项目中获取 id 并运行一个单独的请求以获取每个项目的更多详细信息。我知道如何运行请求,但是如何仅使用 id 创建一个数组?

运行每个请求后,我会为每个项目返回 5-6 个发票详细信息。因此,例如,这些都属于初始响应中的项目 id 333512

                    {
                "invoices": [{
                    "invoice_id": 10015,
                    "cusbillable": true,
                    "inventoried": false,
                    "totals": 2.0,
                    "totalswh": 0.0,
                    "title": "EarlyOrder",
                    "invoicerate": 0.0,
                    "invoicedamt": 0.0,
                    "stockcost": 0.0,
                    "remainingbudg": null
                }, {
                    "invoice_id": 10016,
                    "title": "EarlyOrder",
                    "cusbillable": true,
                    "inventoried": false,
                    "totals": 2.0,
                    "totalswh": 0.0,
                    "invoicerate": 0.0,
                    "invoicedamt": 0.0,
                    "stockcost": 0.0,
                    "remainingbudg": null
                }]
            }

这些发票中没有物品 ID,所以当我使用请求 URL 中的 ID 取回它们时,我想将它们作为物品 ID 的子数组添加到原始物品列表中,就好像它们回来了一样与原始请求。因此,每个项目都会附上发票。我假设最好按顺序运行所有请求并创建一个数组,其中 id 作为每个成员的名称?

因此,例如,我希望得到这样的结果(但格式正确)。

            [{
                    "items": [{
                        "id": 333512,
                        "full_name": "Flooring",
                        "instock": true,
                        "dept": "none",
                        "stockid": 4708384,
                        "commonname": "StdFloor",
                        "reorder": true"
                            {
                            "invoices": [{
                                    "invoice_id": 10015,
                                    "cusbillable": true,
                                    "inventoried": false,
                                    "totals": 2.0,
                                    "totalswh": 0.0,
                                    "title": "EarlyOrder",
                                    "invoicerate": 0.0,
                                    "invoicedamt": 0.0,
                                    "stockcost": 0.0,
                                    "remainingbudg": null
                                },
                                {
                                    "invoice_id": 10016,
                                    "title": "EarlyOrder",
                                    "cusbillable": true,
                                    "inventoried": false,
                                    "totals": 2.0,
                                    "totalswh": 0.0,
                                    "invoicerate": 0.0,
                                    "invoicedamt": 0.0,
                                    "stockcost": 0.0,
                                    "remainingbudg": null
                                }],
                            }
                        }],
                        {
                        "id": 3336532,
                        "full_name": "Standard Tool",
                        "instock": true,
                        "dept": "none",
                        "stockid": 4708383,
                        "commonname": "StandardTool",
                        "reorder": true"
                            {
                            "invoices": [{
                                    "invoice_id": 10015,
                                    "cusbillable": true,
                                    "inventoried": false,
                                    "totals": 2.0,
                                    "totalswh": 0.0,
                                    "title": "EarlyOrder",
                                    "invoicerate": 0.0,
                                    "invoicedamt": 0.0,
                                    "stockcost": 0.0,
                                    "remainingbudg": null
                                },
                                {
                                    "invoice_id": 10016,
                                    "title": "EarlyOrder",
                                    "cusbillable": true,
                                    "inventoried": false,
                                    "totals": 2.0,
                                    "totalswh": 0.0,
                                    "invoicerate": 0.0,
                                    "invoicedamt": 0.0,
                                    "stockcost": 0.0,
                                    "remainingbudg": null
                                }],
                            }
            }]

【问题讨论】:

  • 那么到目前为止您尝试了哪些方法但没有成功?

标签: python arrays json merge python-requests


【解决方案1】:

关于如何将项目和发票数据编译到单个字典中的原则答案:

# The container for compiled items/invoices
items = {}

# Process an items request
for data in jsondata["items"]:
    items[data["id"]] = data
    items[data["id"]]["invoices"] = {}

# Process an invoices request
id = # Your way to get the associated id
items[id]["invoices"] = jsondata["invoices"]

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2020-09-28
    • 1970-01-01
    • 2010-09-11
    • 2018-04-09
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    • 2021-02-02
    • 2019-12-16
    相关资源
    最近更新 更多