0

I'm trying to go through a JSON by using python but I can't access the "mbid" node. I want to print only the first "mbid" node.

Here is my function :

def get_data():
    newJsonx = dict()
    for item in data["resultsPage"]["results"]["calendarEntry"]:
        mbid = item["event"]["performance"][0]["artist"]["identifier"][0]["mbid"]

With this function i get this error : IndexError: list index out of range

but when I'm doing

def get_data():
    newJsonx = dict()
    for item in data["resultsPage"]["results"]["calendarEntry"]:
        mbid = item["event"]["performance"][0]["artist"]["identifier"]

And print(mbid), I'm getting a correct answer :

"identifier": [
  {
   "mbid": "6655955b-1c1e-4bcb-84e4-81bcd9efab30"
  },
  {
   "mbid": "1b1b1b1b-1c1d"
  }
]

So means I don't have a problem with the data. Maybe I'm doing something wrong with the second array?

Here is an example of the JSON structure :

{
  "resultsPage": {
    "status": "ok",
    "results": {
      "calendarEntry": [
        {
          "reason": {

          },
          "event": {
            "performance": [
              {
                "id": 72641494,
                "displayName": "Arnalds",
                "artist": {
                  "id": 590465,
                  "identifier": [
                    {
                      "mbid": "6655955b-1c1e-4bcb-84e4-81bcd9efab30"
                    },
                    {                   
                      "mbid": "1b1b1b1b-1c1d"
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
}

Thanks for your time

3
  • 1
    Your first function works for me on your example. Commented Nov 11, 2019 at 9:46
  • Couldn't replicate, working fine for me, Your full data JSON must have some values missing or empty lists, which is making [0] give you the error Commented Nov 11, 2019 at 9:46
  • @OferSadan you're right, sometime i have empty lists Commented Nov 11, 2019 at 10:15

1 Answer 1

2
def get_data():
    newJsonx = dict()
    for item in data["resultsPage"]["results"]["calendarEntry"]:
        performance=item["event"]["performance"]
        if performace:
          identifier=performace[0]["artist"]["identifier"]
          if identifier:
            mbid=identifier[0]["mbid"]
Sign up to request clarification or add additional context in comments.

Comments

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.