Home > Back-end >  Python retrieve specified nested JSON value
Python retrieve specified nested JSON value

Time:06-10

I have a .json file with many entries looking like this:

{
    "name": "abc",
    "time": "20220607T190731.442",
    "id": "123",
    "relatedIds": [
        {
            "id": "456",
            "source": "sourceA"
        },
        {
            "id": "789",
            "source": "sourceB"
        }
    ],
}

I am saving each entry in a python object, however, I only need the related ID from source A. Problem is, the related ID from source A is not always first place in that nested list.

So data['relatedIds'][0]['id'] is not reliable to yield the right Id.

Currently I am solving the issue like this:

import json

with open("filepath", 'r') as file:
    data = json.load(file)

for value in data['relatedIds']:
    if(value['source'] == 'sourceA'):
        id_from_a = value['id']

entry = Entry(data['name'], data['time'], data['id'], id_from_a)

I don't think this approach is the optimal solution though, especially if relatedIds list gets longer and more entries appended to the JSON file.

Is there a more sophisticated way of singling out this 'id' value from a specified source without looping through all entries in that nested list?

CodePudding user response:

Can you get whatever generates your .json file to produce the relatedIds as an object rather than a list?

{
    "name": "abc",
    "time": "20220607T190731.442",
    "id": "123",
    "relatedIds": {
        "sourceA": "456",
        "sourceB": "789"
    }
}

If not, I'd say you're stuck looping through the list until you find what you're looking for.

CodePudding user response:

For a cleaner solution, you could try using python's filter() function with a simple lambda:

import json

with open("filepath", 'r') as file:
    data = json.load(file)

filtered_data = filter(lambda a : a["source"] == "sourceA", data["relatedIds"])

id_from_a = next(filtered_data)
entry = Entry(data['name'], data['time'], data['id'], id_from_a)

Correct me if I misunderstand how your json file looks, but it seems to work for me.

CodePudding user response:

One step at a time, in order to get to all entries:

>>> data["relatedIds"]
[{'id': '789', 'source': 'sourceB'}, {'id': '456', 'source': 'sourceA'}]

Next, in order to get only those entries with source=sourceA:

>>> [e for e in data["relatedIds"] if e["source"] == "sourceA"]
[{'id': '456', 'source': 'sourceA'}]

Now, since you don't want the whole entry, but just the ID, we can go a little further:

>>> [e["id"] for e in data["relatedIds"] if e["source"] == "sourceA"]
['456']

From there, just grab the first ID:

>>> [e["id"] for e in data["relatedIds"] if e["source"] == "sourceA"][0]
'456'
  • Related