Home > Software engineering >  parsing the uri key from json file is failing
parsing the uri key from json file is failing

Time:05-24

Hi everyone I am trying to parse the uri key from a json file - it is properly loading the JSON file, but when I try to parse uri its failing with:

    raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not TextIOWrapper


with open(RROOT) as data_file:
    data = json.loads(data_file)
    for key, value in data.items():
        if key["uri"] in data:
            print(value)

What am I doing wrong here? Thank you

CodePudding user response:

Firstly, use json.load(takes file object) instead of json.loads(takes json string).

Secondly, key["uri"] will error out. If you're trying to just get the value for the key "uri" then do:

with open(RROOT) as data_file:
    data = json.load(data_file)
    for value in data['uri']:
        print(value)
  • Related