Home > Software engineering >  Issue accessing data returned from an API with Python
Issue accessing data returned from an API with Python

Time:02-02

I am a little past the noo-by stage with python but new to getting data from API's. I am having an issue extracting a piece of data from what is returned from an API.

I am using the "requests" module in python.

My code

import requests import json

url = "https://<my url.com/<locations>"
     resp = requests.get(url) r_dict = resp.json()

After this the value of the "r_dict" variable is

"[{'content': {'parameters': {'node_external_ip_list': '10.xx.yy.zz'}}}]".

The type is "<class 'list'>" and the length is 1.

I convert this to a dictionary with

my_dict = {}

for i in n_dict:
    my_dict.update(i)**

The value of my_dict is "{'content': {'parameters': {'node_external_ip_list': '10.xx.yy.zz'}}}"

and is of type "<class 'dict'>" with a length of 1.

I am trying to extract the IP address from this dictionary

I have tried

my_dict2 = {}

    for i in my_dict:
        my_dict2.update(i)**

but I receive the error

Traceback (most recent call last): File "<stdin>", line 2, in <module> ValueError: dictionary update sequence element #0 has length 1; 2 is required

If I use a print(i) instead of the update I see

"content': {'parameters': {'node_external_ip_list': '10.xx.yy.zz'}}"

I have also tried

result = json.dumps(my_dict, indent = 4)

and when I print "result" I see

{
"content": {
"parameters": { "node_external_ip_list": "10.xx.yy.zz"
}
} }

I have then tried

print(result\['content'\]\['parameters'\]\['emp_external_ip_list'\])

and

print(result[0]['content']['parameters']['emp_external_ip_list'])

and received

Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: string indices must be integers

Is there anyway to extract the IP address either from the list r_dict or from the dictionary my_dict or from the json variable "result"?

CodePudding user response:

import requests

url = "https://<my url.com/<locations>"
resp = requests.get(url)
result = resp.json()

Note: I changed r_dict to result just to avoid the confusion since the result of the request is a list and not a dict.

Based on what you mentioned, the variable result, in the above code, will contain a list.

In Python, to access items in lists, you use the square brackets for slicing and the index to obtain the item available at that specific index. In this case, the list contains one and only item, so you access it with result[0].

This item happens to be the following dictionary:

{
  "content": {
    "parameters": {
      "node_external_ip_list": "10.xx.yy.zz"
    }
  }
}

To access elements in a dictionary, you can use the square brackets and the key to obtain its value. In this case, to obtain the value of content, we can use:

content = result[0]["content"]

Again, the variable content will contain a dictionary and so will result[0]["content"]["parameters"].

So, you can access the IP address as follows, without doing any conversion:

ip_address = result[0]["content"]["parameters"]["node_external_ip_list"]
  • Related