Bart
Bart

Reputation: 37

Read nested json data with python

I'm trying to read all id's from below JSon data for all purchase orders. In below example there are just 2 id's (523fbc6c-359a-49ea-81ea-065e20e4b1db + 77a5e074-5b23-4f85-989d-1b8152982c6e). Any ideas how to achieve this?

Have tried some blogs and code snippets, but result yet :) Next example can be used:

json_data = {
        "purchaseOrder": [
            {
                "id": "523fbc6c-359a-49ea-81ea-065e20e4b1db",
                "installationNumber": "test",
                "peerId": "ac0fd195-cb24-4ced-a5fe-664a25651855",
                "validFrom": "2019-05-28T14:57:21.000+0000",
                "validTo": "2019-05-28T15:57:21.000+0000",
                "originalQuantity": 20,
                "quantity": 0,
                "price": 5,
                "periodInitial": "2019-05-28T15:00:00.000+0000",
                "periodFinal": "2019-05-28T16:00:00.000+0000"
            },
            {
                "id": "77a5e074-5b23-4f85-989d-1b8152982c6e",
                "installationNumber": "test",
                "peerId": "308866ba-90cb-47a7-8c73-589c0f355eb7",
                "validFrom": "2019-05-28T14:57:21.000+0000",
                "validTo": "2019-05-28T15:57:21.000+0000",
                "originalQuantity": 20,
                "quantity": 15,
                "price": 5,
                "periodInitial": "2019-05-28T15:00:00.000+0000",
                "periodFinal": "2019-05-28T16:00:00.000+0000"
            }
        ],
        "salesOrder": [
            {
                "id": "7113f1ee-6980-4447-bf71-75c93a4c5bad",
                "installationNumber": "test",
                "peerId": "308866ba-90cb-47a7-8c73-589c0f355eb7",
                "validFrom": "2019-05-28T14:57:21.000+0000",
                "validTo": "2019-05-28T15:57:21.000+0000",
                "originalQuantity": 20,
                "quantity": 0,
                "price": 5,
                "periodInitial": "2019-05-28T15:00:00.000+0000",
                "periodFinal": "2019-05-28T16:00:00.000+0000"
            }
        ],
        "agreement": [
            {
                "id": "e0f0ea4d4ecb357f443df720c457d8f20bcdc0b9d28b8eaa24a1b6bd80bd3ac50",
                "installationNumber": "test",
                "quantity": 15,
                "price": 5,
                "periodInitial": "2019-05-28T15:00:00.000+0000",
                "periodFinal": "2019-05-28T16:00:00.000+0000",
                "type": "A",
                "status": "",
                "agrPurchOrder": "523fbc6c-359a-49ea-81ea-065e20e4b1db",
                "agrSalesOrder": "7113f1ee-6980-4447-bf71-75c93a4c5bad",
                "creationDate": "2019-06-06T09:42:46.710+0000"
            },
            {
                "id": "e0f0ea4d4ecb357f443df720c457d8f20bcdc0b9d28b8eaa24a1b6bd80bd3ac51",
                "installationNumber": "test",
                "quantity": 5,
                "price": 5,
                "periodInitial": "2019-05-28T15:00:00.000+0000",
                "periodFinal": "2019-05-28T16:00:00.000+0000",
                "type": "A",
                "status": "",
                "agrPurchOrder": "77a5e074-5b23-4f85-989d-1b8152982c6e",
                "agrSalesOrder": "7113f1ee-6980-4447-bf71-75c93a4c5bad",
                "creationDate": "2019-06-06T09:42:46.711+0000"
            }
        ],
        "status": ""
    }

Upvotes: 0

Views: 49

Answers (4)

Shane Parker
Shane Parker

Reputation: 1

Your json_data variable is not a json construct. It is a python dictionary. If you want to process it as json data, you first need to load it with the json module. You can change your variable to be a string of json data like below:

import json

json_data = '''
{
        "purchaseOrder": [
            {
                "id": "523fbc6c-359a-49ea-81ea-065e20e4b1db",
                "installationNumber": "test",
                "peerId": "ac0fd195-cb24-4ced-
...
}
'''

loaded_json = json.loads(json_data)
print(loaded_json['purchaseOrder']

You can then iterate through the loaded_json object and traverse it as a json object.

Upvotes: 0

Lahel Light
Lahel Light

Reputation: 66

You can import the JSON data and loop through the "purchaseOrder" array and append the IDs of each element in an array.

Upvotes: 0

jose_bacoy
jose_bacoy

Reputation: 12684

Use comprehensions and it will collect all id in purchase order.

print([item['id'] for item in json_data['purchaseOrder']])

Result: ['523fbc6c-359a-49ea-81ea-065e20e4b1db', '77a5e074-5b23-4f85-989d-1b8152982c6e']

Upvotes: 1

vurmux
vurmux

Reputation: 10020

Use list comprehensions:

result = [x['id'] for x in json_data['purchaseOrder']]

['523fbc6c-359a-49ea-81ea-065e20e4b1db',
 '77a5e074-5b23-4f85-989d-1b8152982c6e']

Upvotes: 1

Related Questions