Return nested JSON item that has multiple instances

2024/10/5 15:13:00

So i am able to return almost all data, except i am not able to capture something like this:

 "expand": "schema""issues": [{"expand": "<>","id": "<>","self": "<>","key": "<>","fields": {"components": [{"self": "<>","id": "1","name": "<>","description": "<>"}]}},{"expand": "<>","id": "<>","self": "<>","key": "<>","fields": {"components": [{"self": "<>","id": "<>","name": "<>"}]}},

I want to return a list that contains both of the 'name's for 'components', i have tried using:

 list((item['fields']['components']['name']) for item in data['issues'])

but i get a type error saying TypeError: list indices must be integers or slices, not str when i try to Print() the above line of code

Also, if i could get some explanation of what this type error means, and what "list" is trying to do that means that it is not a "str" that would be appreciated

EDIT:

url = '<url>'
r = http.request('GET', url, headers=headers)
data = json.loads(r.data.decode('utf-8'))print([d['name'] for d in item['fields']['components']] for item in data['issues'])
Answer

As the commenter points out you're treating the list like a dictionary, instead this will select the name fields from the dictionaries in the list:

list((item['fields']['components'][i]['name'] for i, v in enumerate(item['fields']['components'])))

Or simply:

[d['name'] for d in item['fields']['components']]

You'd then need to apply the above to all the items in the iterable.

EDIT: Full solution to just print the name fields, assuming that "issues" is a key in some larger dictionary structure:

for list_item in data["issues"]: # issues is a list, so iterate through list itemsfor dct in list_item["fields"]["components"]: # each list_item is a dictionaryprint(dct["name"]) # name is a field in each dictionary
https://en.xdnf.cn/q/119963.html

Related Q&A

What request should I make to get the followers list from a specific profile page

I am trying to get the followers list from this profile, I tried making a GET request using python requests to the API using this request URL but it didnt seem to work, I got a METHOD_NOT_ALLOWED error…

PlayerDB API Post Requests bring 404

I made a little script to get the UUIDs of people who joined my minecraft server, then run them through the PlayerDB API through a post request to: https://playerdb.co/api/player/minecraft/* where the …

How to make changes using Configparser in .ini file persistent

How to modify the .ini file? My ini file looks like this. And i want the format section ini to be changed like this[Space to be replaced with a tab followed by $] Format="[%TimeStamp%] $(%Thre…

How to structure template libraries in a Django project? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.Clo…

Not able to click button with Selenium (Python)

Im trying to click a button using Selenium, but everytime I get the message that it cant find the element. This happens even when I put a time.sleep() in front of it. time.sleep(5)#Click on downloaddow…

how to aproximate shapes height and width for image detection using opencv and python

i was following a tutorial about shapes detection using opencv ,numpy and python ,and it was this function i know the reason from it but i do not know how to modify it so i can use it as i want the to…

Using str.replace in a for loop

I am working on an assignment that is asking me to change the below code so that line 4 uses str.isalnum and lines 5-7 become uses only one line using str.replace.s = p55w-r@d result = for c in s:if(c…

extract all vertical slices from numpy array

I want to extract a complete slice from a 3D numpy array using ndeumerate or something similar. arr = np.random.rand(4, 3, 3)I want to extract all possible arr[:, x, y] where x, y range from 0 to 2

Output an OrderedDict to CSV

I read a CSV file and use the usaddress library to parse an address field. How do I write the resulting OrderedDicts to another CSV file?import usaddress import csvwith open(output.csv) as csvfile:re…

min() arg is an empty sequence

I created a function that consumes a list of values and produces the average. However it only works when I use integer values. I get the following error when I make the values into floats:min() arg is …