python将多个json请求合并到一个文件并保存

时间:2016-08-09 12:30:06

标签: python arrays json merge python-requests

我知道这个请求有点重。不确定是否有人可以提供帮助,但如果有的话,我会很感激。

我试图在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并运行单独的请求以获取每个项目的更多详细信息。我知道如何运行请求,但是如何使用id来制作数组呢?

一旦我运行了这些请求,我就会收到5-6个发票详细信息PER项目。例如,这些都属于初始响应中的项目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,但是当我使用请求网址中的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
                                }],
                            }
            }]

1 个答案:

答案 0 :(得分:0)

Principe回答有关如何将项目和发票数据编译到单个字典中的信息:

# 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"]

希望它会对你有所帮助。