-1

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'
J. Murray
  • 1,460
  • 11
  • 19
zkalman
  • 17
  • 6
  • Are you certain that your **json_file** isn't a null object at this point? There's nothing saying that it was passed to that function (properly) initialized. – J. Murray Oct 09 '19 at 21:42
  • That's not a JSON file, it's a Python script. – Barmar Oct 09 '19 at 21:43
  • You pass a string to your function and then call `.read()` on that string - you need to `open()` a file before reading from it. Also, that's not a proper JSON file, there is no attribute "person", and your `return` is dedented. – jbndlr Oct 09 '19 at 21:45
  • If you search in your browser for "Python read JSON file", you'll find references that can explain this much better than we can manage here. – Prune Oct 09 '19 at 21:46

1 Answers1

1

json_file is a filename, not a file. You need to open the file in order to read it.

You can also use json.load() instead of json.loads(). It will read from the file itself.

def json_to_plaintext(filename, attribute):
    with open(filename) as json_file:
        json_tmp = json.load(json_file)
    attr = json_tmp[attribute]  # collect attribute
    with open("json_attribute.txt", "w+") as txt_file:
        attr = str(attr)  # make string of object
        txt_file.write(attr)

However, the file you show is not a proper JSON file. The JSON file should look like this:

{ "person": {
    "name": "Name",
    "adress": "Somewhere",
    "phone_no": "0700000000",
    "email_id": null
    }
}

What you showed is a Python script that defines a variable named person. If you want to read and execute another script, you can use import.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • OK. That makes me somewhat more clear of the problem. I tried making testcode: sample_json = { "name": "Name", "address": "Somewhere", "phone_no": "0700000000" } attribute = sample_json["name"] def test(): plain_text.json_to_plaintext(sample_json, attribute). Of course this does not work, I get "'dict' object has no attribute 'read'"... – zkalman Oct 10 '19 at 10:23
  • The argument to `json_to_plaintext()` is the name of the file that contains the JSON, not a dictionary. – Barmar Oct 10 '19 at 15:55
  • Put the JSON I showed in a file, e.g. `sample.json`. Then use `json_to_plaintext("sample.json", "person")` – Barmar Oct 10 '19 at 15:57