0

I have these horrible nested JSON dictionaries:

  "data": {
            "assetID": "VMSA0000000000310652",
            "lastModified": "2017-06-02T19:36:36.535-04:00",
            "locale": {
              "MetadataAlbum": {
                "Artists": {
                  "Artist": {
                    "ArtistName": "Various Artists",
                    "ArtistRole": "MainArtist"
                  }
                },
                "Publishable": "true",
                "genres": {
                  "genre": {
                    "extraInfos": null,
                  }
                },
                "lastModified": "2017-06-02T19:32:46.296-04:00",
                "locale": {
                  "country": "UK",
                  "language": "en",

And want to be able to match the value of the language with the method below. I am passing in language ('en') and the data is the nested dictionary above.

def get_localized_metadataalbum(language, data):
    for locale in data['locale']:
        if data['locale'].get('MetadataAlbum') is not None:
            if data['locale'].get('MetadataAlbum').get('locale') is not None:
                if data['locale'].get('MetadataAlbum').get('locale').get('language') is not None:
                    if data['locale'].get('MetadataAlbum').get('locale').get('language') == language:
                        return data['locale']

return None

The method works with a list of dictionaries, but not with dictionaries inside dictionaries... Can anyone point me to a place where I can learn how to parse through nested dictionaries? I'm a little lost here and all examples I've found show how to parse to a list of dictionaries.

I've been getting: TypeError: string indices must be integers

Tejas Thakar
  • 585
  • 5
  • 19
  • Please, close correctly your dict structure. – julian salas Jul 04 '17 at 04:23
  • Can you clarify if you sometimes pass dicts and sometimes lists? You would then have to check the type of the data (e.g. type(mydata) == dict). For looping in dicts check this https://stackoverflow.com/questions/3294889/iterating-over-dictionaries-using-for-loops. – Anton vBR Jul 04 '17 at 04:28

2 Answers2

0

I fixed your json. I put into string and closed the brackets correctly. This works as expected:

import json

json_string = """
  {"data": {
            "assetID": "VMSA0000000000310652",
            "lastModified": "2017-06-02T19:36:36.535-04:00",
            "locale": {
              "MetadataAlbum": {
                "Artists": {
                  "Artist": {
                    "ArtistName": "Various Artists",
                    "ArtistRole": "MainArtist"
                  }
                },
                "Publishable": "true",
                "genres": {
                  "genre": {
                    "extraInfos": null
                  }
                },
                "lastModified": "2017-06-02T19:32:46.296-04:00",
                "locale": {
                  "country": "UK",
                  "language": "en"
                }
              }
           }
         }
   }
   """

json_data = json.loads(json_string)

print(json_data)


def get_localized_metadataalbum(language, data):
    for locale in data['locale']:
        if data['locale'].get('MetadataAlbum') is not None:
            if data['locale'].get('MetadataAlbum').get('locale') is not None:
                if data['locale'].get('MetadataAlbum').get('locale').get('language') is not None:
                    if data['locale'].get('MetadataAlbum').get('locale').get('language') == language:
                        return data['locale']

    return None

print('RESULT:')
print(get_localized_metadataalbum("en", json_data['data']))

I ran it on python 2.7.12.

Nurjan
  • 5,889
  • 5
  • 34
  • 54
0

you probably want try: except like

try:
    assert x in data.keys() for x in ["x","y"]
    ...
    return data["x"]["y"]
except:
    return None
Jack Wu
  • 447
  • 4
  • 14