1

I'm getting below output from an API and I want to read all purchaseOrder data. Not really sure how to loop on this data. Also it comes with b' at the front.

  b'[
   {"purchaseOrder":
    [
     {
      "id":"d01f0f6d-398f-4220-8a9a-44f47beedf04",
      "installationNumber":null,
      "peerId":"308866ba-90cb-47a7-8c73-589c0f355eb7",
      "validFrom":"2019-06-07T12:51:15.000+0000",
      "validTo":"2019-06-07T13:51:15.000+0000",
      "originalQuantity":5,
      "quantity":5,
      "price":5,
      "periodInitial":"2019-06-07T13:00:00.000+0000",
      "periodFinal":"2019-06-07T14:00:00.000+0000"
     }
   ],
   "salesOrder":null,
   "agreement":null,
   "status":""
  }
]'

Have tried things like loaded_json = json.load(r.content) and it didn't work.

This is code I use to get the response:

r = requests.post(url=api_endpoint, data=json.dumps(json_post), headers=headers)
Bart
  • 37
  • 2
  • 1
    Possible duplicate of [What's the best way to parse a JSON response from the requests library?](https://stackoverflow.com/questions/16877422/whats-the-best-way-to-parse-a-json-response-from-the-requests-library) – Error - Syntactical Remorse Jun 07 '19 at 12:59
  • Thanks. Yes changed that. But then next step is to loop. How does that work? Thanks in advance. – Bart Jun 07 '19 at 13:15
  • 2
    Note that in the question linked above, the accepted answer is not the best answer. Look at the highest voted one. – eduffy Jun 07 '19 at 13:16

2 Answers2

2

To get the json of a response use data = response.json().

After that you can step through it like normal lists and dicts:

import json
data = r.json()
print(json.dumps(data , indent=2)) # If you want to see the data from the response
for dic in data :
    if 'purchaseOrder' in dic:
        for item in dic['purchaseOrder']:
            # item here is the `dict` for each purchaseOrder (PO).
            print(json.dumps(item, indent=2)) # This will print each item in PO.
  • Thanks this code works for me almost. Now I'm trying to reach key value. if 'purchaseOrder' in dic: for itemdata in dic['purchaseOrder']: for key in itemdata: print(itemdata[id]) – Bart Jun 07 '19 at 13:42
  • You would benefit into looking into a dictionary tutorial. `for key in itemdata.keys()`. – Error - Syntactical Remorse Jun 07 '19 at 14:19
  • data2 = r.json() print(json.dumps(data2, indent=2)) for dic in data2: if 'purchaseOrder' in dic: for itemdata in dic['purchaseOrder']: for key in itemdata: if key == 'id': print("Id:") print(itemdata['id']) print("Price:") print(itemdata['price']) – Bart Jun 07 '19 at 16:55
0

Thanks all for support. The next code works for me:

    data = r.json()
    print(json.dumps(data, indent=2))
    for dic in data:
        if 'purchaseOrder' in dic:
            for itemdata in dic['purchaseOrder']:
                for key in itemdata:
                    if key == 'id':
                        print("Id:")
                        print(itemdata['id'])
                        print("Price:")
                        print(itemdata['price']) 
Bart
  • 37
  • 2