0

I am making a request to a URL using python requests and getting the following response

{
    "content": {
        "root": {
            "details":"http://localhost:8080/****/root",
            "content": {
                "A": {
                "details":"http://localhost:8080/***"
                },
                "B":{
                "details":"http://localhost:8080/***"
                },
                "C":{
                "details":"http://localhost:8080/****"
                }
            }
        }
    }
}

I want to get the values A, B, C in a list. How can I do that? Any inputs would be of great help.

1
  • 1
    What have you tried? You have a nested dictionary, it should not be hard to extract items from it. Commented Mar 5, 2020 at 4:21

4 Answers 4

1

Python allows dict to be nested in another dict. So it should be straight forward.

You can iterate over the dict obtained from 'root' -> 'content' keys.

values = []
contents = response['root']['content']
for key, value in contents.items():
     print(key, value['details'])
     values.append(value['details']

Method two : Iterate over dict using keys only :

values = []
contents = response['root']['content']
for key in contents :
    value = contents[key]
    print(key, value['details'])
    values.append(value['details'])
print(values)
Sign up to request clarification or add additional context in comments.

Comments

1

you can simply read them as dict and append to a list:

#assuming res is the response json
Vals=[]
a = res["root"]["content"]["A"]
Vals.append(a)

Similarly do for B and C

Comments

1

Get the values and store it in a list:

response = {
        "content":{
            "root":{
                    "details":"http://localhost:8080/****/root",
                    "content":{
                    "A":{
                        "details":"http://localhost:8080/***"
                    },
                    "B":{
                        "details":"http://localhost:8080/***"
                    },
                    "C":{
                        "details":"http://localhost:8080/***"
                    }
                }
            }
        }
    }

contents = response["content"]['root']['content']
contents_list = []

for content in contents.values():
    contents_list.append(content["details"])

print(contents_list)

OUTPUT:

['http://localhost:8080/***', 'http://localhost:8080/***', 'http://localhost:8080/***']

But if you want to get both key and value of A, B and C. Try this:

for content in contents.values():
    for key, value in content.items():
        contents_list.append(f"{key}: {value}")

print(contents_list)

OUTPUT:

['details: http://localhost:8080/***', 'details: http://localhost:8080/***', 'details: http://localhost:8080/***']

Comments

0

You should try your self first.

  1. First understand to level in nested dictionary. Note: Give input is invalid, missing ".
  2. Use get method of dictionary to fetch key.
  3. you should know have to iterate over dictionary by for loop.
  4. You should know items method of dictionary.
  5. If you want to get value of specific keys like A and/or B and/or C ... then please add if loop before appending value to output list.

Hope this will help you.

Code:

output_list = []
for item in input_data.get("content", {}).get("root", {}).get("content", {}).items():
    #output_list.append(item[1])  # Value of Key
    output_list.append(item[1]["details"]) # Value of details from Key


print("output_list:", output_list)

List Comprehension

output_list = [item[1]["details"]
    for item in input_data.get("content", {}).get("root", {}).get("content", {}).items()
]

Comments

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.