0

I have JSON in response and I'm trying to get all "Id" and "Pages" values and put them to an array (or list) for next steps

[
{
    "Page": 1,
    "Content": [
        {"Id": 100000000000001,"Title": "title1", ...},
        {"Id": 100000000000002,"Title": "title2", ...},
        {"Id": 100000000000003,"Title": "title3", ...}
    ]
},
{
    "Page": 2,
    "Content": [
        {"Id": 100000000000004,"Title": "title4", ...},
        {"Id": 100000000000005,"Title": "title5", ...},
        {"Id": 100000000000006,"Title": "title6", ...}
    ]
},
{
    "Page": 3,
    "Content": [
        {"Id": 100000000000007,"Title": "title7", ...},
        {"Id": 100000000000008,"Title": "title8", ...},
        {"Id": 100000000000009,"Title": "title9", ...}
    ]
}

]

Got "Page" values by using pages = [ e['Page'] for e in data ] from here

Can't get "Id" values. Tried

for el in data: print (el['Content']['Id'])

But got error TypeError: list indices must be integers or slices, not str

Can you help me?

Update1: Sorry for my slightly incorrectly asked question: as output from this JSON I want to return array ["id1","id2",...,"id9"], not print

0

3 Answers 3

1

using list comprehension you can do this easily

a = [
{
    "Page": 1,
    "Content": [
        {"Id": 100000000000001,"Title": "title1",  },
        {"Id": 100000000000002,"Title": "title2",  },
        {"Id": 100000000000003,"Title": "title3",  }
    ]
},
{
    "Page": 2,
    "Content": [
        {"Id": 100000000000004,"Title": "title4",  },
        {"Id": 100000000000005,"Title": "title5",  },
        {"Id": 100000000000006,"Title": "title6",  }
    ]
},
{
    "Page": 3,
    "Content": [
        {"Id": 100000000000007,"Title": "title7",  },
        {"Id": 100000000000008,"Title": "title8",  },
        {"Id": 100000000000009,"Title": "title9",  }
    ]
}

]

res = [[i['Page'],[ j['Id'] for j in i['Content']]] for i in a]
print(res)

output

[[1, [100000000000001, 100000000000002, 100000000000003]],
 [2, [100000000000004, 100000000000005, 100000000000006]],
 [3, [100000000000007, 100000000000008, 100000000000009]]]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer. Sorry for my slightly incorrectly asked question: As output from my JSON I want to return array ["id1","id2",...,"id9"], but not print these "ID"s
1

This is only an addition to the answers already given, just in case u come across more nested paths : jmespath makes it easy to traverse through nested lists and dicts :

The path to pages is data -> list -> page ... so u access the data, next a list, then the page key

Same applies to id : data ->list ->content -> list ->id

In jmespath, a key is accessed via a dot, while lists are accessed via brackets([]):

import jmespath
expression = jmespath.compile('[].{page:Page,id:Content[].Id}')
expression.search(data)

[{'page': 1, 'id': [100000000000001, 100000000000002, 100000000000003]},
 {'page': 2, 'id': [100000000000004, 100000000000005, 100000000000006]},
 {'page': 3, 'id': [100000000000007, 100000000000008, 100000000000009]}]

jmespath allows u to compile a search path, similar to the functionality in python's re module. it's more of a tool to keep handy when you come across really nested paths.

Comments

0

Your 'Content' field is a list, with potentially multiple elements themselves containing an 'Id' field. If you want to print them all, here is one way to do it

for element in data:
    ids = [c['id'] for c in element['Content']]
    print(ids)

2 Comments

Thanks for your answer. Two more questions, if you don't mind: 1. How can I put "id" to array (don't want to print them right away)? Because now it prints array with arrays inside 2. How to convert array with arrays [[],[],[]] to one array with all values? As general output I want to get array ["id1", "id2", "id3",...,"id9"]
flattened = [element for subarray in array for element in subarray]

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.