user8231578
user8231578

Reputation:

parse data from Dictionary in python

So i have this dictionary "runs":

[{
'id': 12,
'suite_id': 2,
'name': 'name',
'description': "desc.",
'nice_id': 3,
'joku_id': None,
'onko': False,
'eikai': False,
'tehty': None,
'config': None,
'config_ids': [],
'passed_count': 1,
'blocked_count': 2,
'untested_count': 3,
'retest_count': 4,
'failed_count': 5,
'custom_status1_count': 0,
'custom_status2_count': 0,
'custom_status3_count': 0,
'custom_status4_count': 0,
'custom_status5_count': 0,
'custom_status6_count': 0,
'custom_status7_count': 0,
'projekti_id': 1,
'plan_id': None,
'created_on': 12343214,
'created_by': 11,
'url': 'google.com'
}, {
'id': 16,
'suite_id': 2,
'name': 'namae)',
'description': "desc1",
'nice_id': 5,
'joku_id': None,
'onko': False,
'eikai': False,
'tehty': None,
'config': None,
'config_ids': [],
'passed_count': 100,
'blocked_count': 1,
'untested_count': 3,
'retest_count': 2,
'failed_count': 5,
'custom_status1_count': 0,
'custom_status2_count': 0,
'custom_status3_count': 0,
'custom_status4_count': 0,
'custom_status5_count': 0,
'custom_status6_count': 0,
'custom_status7_count': 0,
'prokti_id': 7,
'plan_id': None,
'created_on': 4321341644,
'created_by': 11,
'url': 'google.com/2' }

there is "id" for about 50 times. that is just a part of it. i need to find all "id":s (Not joku_ids, ncie_ids etc. Only "id") and make a string/dict of them and same for name, and description

i have tried:

j = json.load(run)
ids = (j["id"])

j = json.load(run)
names = (j["name"])

j = json.load(run)
descriptions = (j["description"])

but it returns:

AttributeError: 'list' object has no attribute 'read'

I also need to send a request with specific id and in this case the specific id is marked by o. so id[o] the request code is below:

test = client.send_get('get_tests/1/ ')

so i need to have the id[o] instead of the 1. i have tried

test = client.send_get('get_tests/' + id[o] + '/ ')

but it returns:

TypeError: 'int' object is not subscriptable

Upvotes: 0

Views: 3867

Answers (3)

ammy
ammy

Reputation: 648

If you want your result in list of dictionary then:

result = [{x:y} for i in range(len(data)) for x,y in data[i].items() if x=='id' or x=='name' or x=='description']

output:

[{'name': 'name'}, {'id': 12}, {'description': 'desc.'}, {'name': 'namae)'}, {'id': 16}, {'description': 'desc1'}]

the data is your list of dictionary data. hope this answer helpful for you.

Upvotes: 0

Tejas Thakar
Tejas Thakar

Reputation: 583

May be this can help you.

id = []
for i in runs :
    id.append(i.get('id'))

[12, 16]

Upvotes: 2

K. Kirsz
K. Kirsz

Reputation: 1420

You are trying to pass a list to a json.load function. Please read the docs. Load() does not accep lists, it accepts

a .read()-supporting file-like object containing a JSON document

Upvotes: 0

Related Questions