1

I am trying to parse a nested JSON and trying to collect data into a list under some condition.

Input JSON as below:

[
    {
        "name": "Thomas",
        "place": "USA",  
        "items": [
            {"item_name":"This is a book shelf", "level":1},
            {"item_name":"Introduction", "level":1},
            {"item_name":"parts", "level":2},   
            {"item_name":"market place", "level":3},
            {"item_name":"books", "level":1},
            {"item_name":"pens", "level":1},
            {"item_name":"pencils", "level":1}
        ],
        "descriptions": [
            {"item_name": "Books"}  
        ]
    },
    {
        "name": "Samy",
        "place": "UK",  
        "items": [
            {"item_name":"This is a cat house", "level":1},
            {"item_name":"Introduction", "level":1},
            {"item_name":"dog house", "level":3},   
            {"item_name":"cat house", "level":1},
            {"item_name":"cat food", "level":2},
            {"item_name":"cat name", "level":1},
            {"item_name":"Samy", "level":2}
        ],
        "descriptions": [
            {"item_name": "cat"}    
        ]
    }   

]

I am reading json as below:

with open('test.json', 'r', encoding='utf8') as fp:
    data = json.load(fp)
for i in data:
   if i['name'] == "Thomas":
      #collect "item_name", values in a list (my_list) if "level":1
      #my_list = []

Expected output:

my_list = ["This is a book shelf", "Introduction", "books", "pens", "pencils"] 

Since it's a nested complex JSON, I am not able to collect the data into a list as I mentioned above. Please let me know no how to collect the data from the nested JSON.

2 Answers 2

2

Try:

import json

with open("test.json", "r", encoding="utf8") as fp:
    data = json.load(fp)

my_list = [
    i["item_name"]
    for d in data
    for i in d["items"]
    if d["name"] == "Thomas" and i["level"] == 1
]
print(my_list)

This prints:

['This is a book shelf', 'Introduction', 'books', 'pens', 'pencils']

Or without list comprehension:

my_list = []
for d in data:
    if d["name"] != "Thomas":
        continue
    for i in d["items"]:
        if i["level"] == 1:
            my_list.append(i["item_name"])

print(my_list)
Sign up to request clarification or add additional context in comments.

Comments

2

Once we have the data we iterate over the outermost list of objects. We check if the object has the name equals to "Thomas" if true then we apply filter method with a lambda function on items list with a condition of level == 1

This gives us a list of item objects who have level = 1

In order to extract the item_name we use a comprehension so the final value in the final_list will be as you have expected.

["This is a book shelf", "Introduction", "books", "pens", "pencils"]

import json
def get_final_list():
    with open('test.json', 'r', encoding='utf8') as fp:
        data = json.load(fp)

    final_list = []

    for obj in data:
        if obj.get("name") == "Thomas":
            x = list(filter(lambda item: item['level'] == 1, obj.get("items")))
            final_list = final_list + x

    final_list = [i.get("item_name") for i in final_list]

    return final_list

2 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
@lemon thanks for the valuable feedback. I have updated the answer.

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.