2

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?

1
  • are the related ids sorted? in any case you could add a break statement after you found it Commented Jun 9, 2022 at 14:03

3 Answers 3

1

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)['id']
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.

Sign up to request clarification or add additional context in comments.

Comments

1

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'

Comments

0

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.

1 Comment

Unfortunately this is not possible, as input is not created by a program I wrote/am responsible for

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.