0

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
2
  • 2
    So you want id = [d['id'] for d in runs] Commented Jul 7, 2017 at 5:11
  • what answer you expect, you want result in list or dict? Commented Jul 7, 2017 at 5:20

3 Answers 3

2

May be this can help you.

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

[12, 16]

Sign up to request clarification or add additional context in comments.

1 Comment

It's my pleasure.
0

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

Comments

0

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.

3 Comments

this works aswell but going with the. Tejas's answer. Thanks
Sorry. Forgot :). My code is so close to being done, but i have problem with my string that gets information. it should be like ('get_tests/1/ ') and i need to have a id[o] instead of the 1. i have tried ('get_tests/' + id[o] + '/ ') but it doens't work
i didn't get complete exactly what you want please mention properly or make changes in your question please.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.