2016-08-09 7 views
0

この要求は少し重いです。誰かが助けることができるかどうかは分かりませんが、もしそうなら、私はそれを感謝します。python複数のjson要求を1つのファイルにマージして保存する

私は新しいPythonでXMLデータを扱うことを試みています。私は、いくつかのjson要求を解析し、それらを1つに結合するスクリプトを作成しようとしています。すべてのデータを1つの場所に最新の状態に保つために、このファイルをプロプライエタリなシステムに送ります。これらの

  { 
       "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 +は、最初に、私はそれぞれ1からIDを取得し、それぞれの項目の詳細を取得するために別の要求を実行する必要があります戻って最初の要求

でてきます。私は要求を実行する方法を知っていますが、私はちょうどIDの配列を作るのですか?

これらのリクエストを一度実行すると、5〜6冊目のインボイスの詳細が表示されます。したがって、たとえば、これらはすべて私がリクエストURLにIDを使用してそれらを取り戻すときに、私はしたいものの、これらの請求書は、それらの項目のIDを持っていない

    { 
       "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 333512に属しオリジナルのアイテムリストにアイテム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 
           }], 
          } 
      }] 
+0

これまでに何を試しても動作していませんか? – Anzel

答えて

0

単一の辞書にアイテムや請求書データをコンパイルする方法についてプリンシペの答え:

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

は、それはあなたを助け願っています。

関連する問題