2

I'm trying to navigation through a json file but cannot parse properly the 'headliner' node.

Here is my JSON file :

{  
   "resultsPage":{  
      "results":{  
         "calendarEntry":[  
            {
              "event":{  
                 "id":38862824,
                 "artistName":"Raphael",
               },
               "performance":[  
                  {  
                     "id":73632729,
                     "headlinerName":"Top-Secret",
                  }
               }
            ],
            "venue":{  
               "id":4285819,
               "displayName":"Sacré"
            }
         }
      }
   }

Here is what I my trying to do :

for item in data ["resultsPage"]["results"]["calendarEntry"]:
    artistname = item["event"]["artistName"]
    headliner = item["performance"]["headlinerName"]

I don't understand why it's working for the 'artistName' but it's not working for 'headlinerName'. Thanks for your help and your explanation.

3
  • Because "performance" is an array, not an object. You'd want to use something like: item["performance"][0]["headliner"] for the headliner within the first object. Or else roll out another loop there and use it instead of [0]. Commented Aug 7, 2019 at 20:38
  • notice, the json is malformed Commented Aug 7, 2019 at 20:38
  • thanks for the tips ! it works ! Commented Aug 7, 2019 at 21:00

2 Answers 2

1

Notice your performance key:

"performance":[  
                  {  
                     "id":73632729,
                     "headlinerName":"Top-Secret",
                  }
               }
            ],

The json you posted is malformed. Assuming the structure is like:

"performance":[  
                      {  
                         "id":73632729,
                         "headlinerName":"Top-Secret",
                      }

              ],

You can do:

for i in item:
    i["headlinerName"]

or as @UltraInstinct suggested:

item["performance"][0]["headlinerName"]
Sign up to request clarification or add additional context in comments.

Comments

0

A few problems here. First, your JSON is incorrectly formatted. Your square brackets don't match up. Maybe you meant something like this? I am going to assume "calendarEntry" is a list here and everything else is an object. Usually lists are made plural, i.e. "calendarEntries".

{  
   "resultsPage": {
      "results": {  
         "calendarEntries": [  
            {
              "event": {  
                 "id": 38862824,
                 "artistName": "Raphael"
               },
               "performance": {
                  "id": 73632729,
                  "headlinerName": "Top-Secret"
               },
               "venue": {  
                  "id": 4285819,
                  "displayName": "Sacré"
               }
            }
         ]
      }
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.