0

I have a nested item list containing complicated nested dictionaries as shown:

y = [
{
    "page": 1,
    "pages": 304,
    "per_page": "1",
    "total": 304
},
[
    {
        "adminregion": {
            "id": "",
            "value": ""
        },
        "capitalCity": "Oranjestad",
        "id": "ABW",
        "incomeLevel": {
            "id": "HIC",
            "value": "High income"
        },
        "iso2Code": "AW",
        "latitude": "12.5167",
        "lendingType": {
            "id": "LNX",
            "value": "Not classified"
        },
        "longitude": "-70.0167",
        "name": "Aruba",
        "region": {
            "id": "LCN",
            "value": "Latin America & Caribbean "
        }
    }
]]

and I am trying to extract the "id" values and store them somewhere in a database. What I've tried so far is (not successful):

print([i['id'] for i in y[1]])

and:

 for i in y[1]:
    for j in i:
        print(j)

but no luck Thanks a million..

  • 2
    With my code in the linked question you can do `for k, v in find_key(y, 'id'): print(k, v)` to get the paths to all `'id'` keys, with their values. With the given data the result is `[1, 0, 'adminregion', 'id'], [1, 0, 'id'] ABW, [1, 0, 'incomeLevel', 'id'] HIC, [1, 0, 'lendingType', 'id'] LNX, [1, 0, 'region', 'id'] LCN` – PM 2Ring May 30 '18 at 18:53
  • did you try this? https://stackoverflow.com/a/14059645/2706419 – tourist May 30 '18 at 18:57
  • Yes that worked out thanks PM 2Ring and naveen marri – Dia Abdulkarim May 30 '18 at 19:29

0 Answers0