I am trying to convert a json file to txt:
def json_to_plaintext(json_file):
json_tmp = json.loads(json_file.read())
attr = str(json_tmp["meta"]) # collect attribute
txt_file = open("json_attr.txt", "w+")
attr = str(attr) # make string of object
txt_file.write(attr)
txt_file.close()
json_file.close()
return txt_file
When I call json_to_plaintext("json_file") I get: AttributeError: 'str' object has no attribute 'read'. json_file is a correct json file, so that should not be an issue. As goes for my code, my understanding is that it should work since I do json.loads(json_file.read()), which often can be an issue otherwise.
Is the function perceiving the parameter "json_file" as a string instead of a file-object? If so, how should I pass my json-file as a parameter? If not, what may cause this error?