-3

I'm trying to make a program that gets the "name" field out of the json file:

{
  "accounts" : {
    "fd1c3f81187b4a12b8e1226b0177a23basw" : {
      "accessToken" : "",
      "Profile" : {
        "id" : "ca227f281b6e40eebcb4fa9ef6c326fawedsa34",
        "name" : "random name"
      },
      
      "userProperites" : []
    }
  }
}

with the following code:

import json

json = json.loads('data.json')
accounts = json["accounts"]
    
for value in json["accounts"].values():
    print(value["name"])

but i am getting the "Expecting value: line 1 column 1 (char 0)" error, how can i make it work?

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
Makatina
  • 11
  • 1

1 Answers1

0

If you use .loads(), it's looking for the json as a string. So you need to ready in the json file

import json

# JSON file
with open('data.json', "r") as f:
    jsonStr = f.read()
    jsonData = json.loads(jsonStr)
    
accounts = jsonData["accounts"]
        
for value in jsonData["accounts"].values():
    print(value["name"])

Or

with open("data.json", "r") as f:
    jsonData = json.load(f)
chitown88
  • 27,527
  • 4
  • 30
  • 59