Search code examples
pythonlistnodechildren

Get the child node data from Json File


I got a list from JSON file, but how can I get the username and result from each result using Python?

Json File

{
  "Users" : {
    "abcde" : {
      "email" : "[email protected]",
      "gender" : "Male",
      "password" : "123",
      "result" : "Back Road Explorer",
      "username" : "abcde"
    },
    "halo" : {
      "email" : "[email protected]",
      "password" : "halo",
      "result" : "Outdoor Adventure",
      "username" : "halo"
    },
    "01" : {
      "email" : "[email protected]",
      "gender" : "Male",
      "password" : "dajcaq",
      "result" : "Culinary Connoisseur",
      "username" : "01"
    }
}

The result should be:

username = abcde, halo, 01
result = Back Road Explorer, Outdoor Adventure, Culinary Connoisseur

Solution

  • json = {...}
    
    result_username = []
    result = []
    
    for username in json['Users']:
        result_username += [username]
        result += [json['Users'][username]['result']]
    
    print ('username : ',', '.join(result_username))
    print ('result : ',', '.join(result))
    

    Result

    username :  abcde, halo, 01
    result :  Back Road Explorer, Outdoor Adventure, Culinary Connoisseur