JSON parsing in python using JSONPath

0 votes

In the JSON below, I want to access the email-id and 'gamesplayed' field for each user.

 "UserTable" : {
     "abcd@gmailcom" : {
        "gameHistory" : {
           "G1" : [ {
             "category" : "1",
             "questiontext" : "What is the cube of 2 ?"
           }, {
             "category" : "2",
             "questiontext" : "What is the cube of 4 ?"
           } ]
         },
        "gamesplayed" : 2
      },
     "xyz@gmailcom" : {
        "gameHistory" : {
           "G1" : [ {
             "category" : "1",
             "questiontext" : "What is the cube of 2 ?"
           }, {
             "category" : "2",
             "questiontext" : "What is the cube of 4 ?"
           } ]
         },
        "gamesplayed" : 2
      }
}

Following is the code that I using to try and access the users email-id:

for user in jp.match("$.UserTable[*].[0]", game_data):
    print("User ID's {}".format(user_id))

This is the error I'm getting:

  File "C:\ProgramData\Anaconda3\lib\site-packages\jsonpath_rw\jsonpath.py", line 444, in find
    return [DatumInContext(datum.value[self.index], path=self, context=datum)]

KeyError: 0

And when I run the following line to and access the 'gamesplayed' field for each user, the IDE Crashes.

print (parser.ExtentedJsonPathParser().parse("$.*.gamesplayed").find(gd_info))
Jan 4, 2023 in Python by erzan
• 630 points

edited 4 days ago 6 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP