I'm testing a function that is supposed to convert a json to plain text.
I've checked similar threads, but the most relevant I found was problems in their actual function. I am not at all comfortable with json, or Python for that matter, but my guess is that the problem lies in how I use the function rather than the actual function.
The json-file I've created and tried converting looks as follows: person = {}
person ['Name'] = {
'name': 'Name',
'adress': 'Somewhere',
'phone_no': '0700000000',
'email_id': None
}
This is the function I am testing:
def json_to_plaintext(json_file, attribute):
json_tmp = json.loads(json_file.read())
attr = json_tmp[attribute] # collect attribute
txt_file = open("json_attribute.txt", "w+")
attr = str(attr) # make string of object
txt_file.write(attr)
txt_file.close()
return txt_file
To test this I run
plain_text.json_to_plaintext(r'C:\Desktop\Tests\test2', 'person')
"test2" is the json-file I created, and 'person' is what I believe is an attribute.
When I run this I get the error:
json_tmp = json.loads(json_file.read())
AttributeError: 'str' object has no attribute 'read'