Home > database >  Get values from Data-Driven key in dict
Get values from Data-Driven key in dict

Time:10-29

I have a dict like below with hundreds of "assets". I would like to get the key="href" and print the url but because all "assets" are in a list and the first key of "assets" is changing I havent found a way to get there. Thanks for helping!

d = {"features": [

    {
        "assets": {
            "swissbuildings3d_2_2018-07_1064-24_2056_5728.dxf.zip": {
                "checksum:multihash":
                    "1220A94A04BD19E190139FAD49E6174DE82987221F7330DDEB7F6943AEAC3D7C4C78",
                "created": "2021-02-10T17:51:31.618859Z",
                "href":
                    "https://data.geo.admin.ch/ch.swisstopo.swissbuildings3d_2/swissbuildings3d_2_2018-07_1064-24/swissbuildings3d_2_2018-07_1064-24_2056_5728.dxf.zip",
                "proj:epsg": 2056,
                "type": "application/x.dxf zip",
                "updated": "2021-02-10T17:51:31.618877Z"
            }
        }
    },

    {
        "assets": {
            "swissbuildings3d_2_2018-07_1064-42_2056_5728.dxf.zip": {
                "checksum:multihash":
                    "1220EA3AFCCDE8648CB60CDF17AF679458233DE2E6052CBBB91F058CBCA651191F6D",
                "created": "2021-02-10T17:51:33.722985Z",
                "href":
                    "https://data.geo.admin.ch/ch.swisstopo.swissbuildings3d_2/swissbuildings3d_2_2018-07_1064-42/swissbuildings3d_2_2018-07_1064-42_2056_5728.dxf.zip",
                "proj:epsg": 2056,
                "type": "application/x.dxf zip",
                "updated": "2021-02-10T17:51:33.723005Z"}
        }
    }
]}

CodePudding user response:

If the dictionary in your example is assigned to a variable called d, this works:

result = [(next(iter(x['assets'])), x['assets'][next(iter(x['assets']))]['href']) for x in d['features']]

print(result)

Output:

[('swissbuildings3d_2_2018-07_1064-24_2056_5728.dxf.zip', 'https://data.geo.admin.ch/ch.swisstopo.swissbuildings3d_2/swissbuildings3d_2_2018-07_1064-24/swissbuildings3d_2_2018-07_1064-24_2056_5728.dxf.zip'), ('swissbuildings3d_2_2018-07_1064-42_2056_5728.dxf.zip', 'https://data.geo.admin.ch/ch.swisstopo.swissbuildings3d_2/swissbuildings3d_2_2018-07_1064-42/swissbuildings3d_2_2018-07_1064-42_2056_5728.dxf.zip')]

If what you shared wasn't in fact a dictionary, but a .json file, this is how to get it all the same:

import json

with open('mydata.json') as f:
    d = load.loads(f.read())

CodePudding user response:

Try this one.

output_list = []
for data_dict in d['features']:
    for key, value_dict in data_dict['assets'].items():
        output_list.append(value_dict['href'])

print(output_list)

Output:

['https://data.geo.admin.ch/ch.swisstopo.swissbuildings3d_2/swissbuildings3d_2_2018-07_1064-24/swissbuildings3d_2_2018-07_1064-24_2056_5728.dxf.zip', 'https://data.geo.admin.ch/ch.swisstopo.swissbuildings3d_2/swissbuildings3d_2_2018-07_1064-42/swissbuildings3d_2_2018-07_1064-42_2056_5728.dxf.zip']

  • Related