0

I am trying to write a class that will take a string variable in the constructor and will validate the file path and if valid will load the file and populate variables with the content and also the class to have getters for each var.

Here is the json file:

{
  "name": [
    "Caadi",
    "Iskadhig"
  ],
  "location": 20356,
  "job": "Engineer",
  "address": [
    {
      "city": "Swindon",
      "county": [
        "Avon"
      ]
      
    }
  ]
}

I have attempted so far the following code:

import json
import os.path

class Config:
    def __init__(self, file_name,name,location,job,address):
        self.file_name = file_name
        self.name = name
        self.location = location
        self.job = job
        self.address = address

        try:
            if os.path.exists(
                    '/CaseConfig.json'):  # validate the file path
                with open('/CaseConfig.json', 'r') as file:
                    json_file_data = file.read() # read the content of file
                    self.file_name.get_json_content_file = json.loads(json_file_data)  # the content of file
                
            else:
                print("File doesn't  exist please check the path")

        except Exception as e:
            print("File not accessible", e)

    def getName(self):
         return self.name

    def getLocation(self):
         return self.location

    def getJob(self):
        return self.job

    def getAddress(self):
        return self.address

obj = Config('file_name', 'name', 'location', 'job', 'address')

I am stuck and not sure why I am getting the following error:

File not accessible 'str' object has no attribute 'get_json_content_file'
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • 1
    This `self.file_name.get_json_content_file` will not work if all your code is just what you posted because `self.file_name` is just a string with value `'file_name'` and strings, as the error already indicates, doesn't have a `get_json_content_file` attribute. – accdias Feb 02 '21 at 10:59
  • Not a duplicate, but please see [What's the pythonic way to use getters and setters?](https://stackoverflow.com/questions/2627002/whats-the-pythonic-way-to-use-getters-and-setters) – Tomerikoo Feb 02 '21 at 11:03
  • Did you mean `self.get_json_content_file = ...`? It is not clear what you expect `self.file_name.get_json_content_file = ...` to do... – Tomerikoo Feb 02 '21 at 11:04
  • @Tomerikoo i am expecting to load/read what is in CaseConfig.json file – Caadi Waaye Feb 02 '21 at 11:27
  • Yes I can see that. But as explained above, you are trying to access a non-existent attribute of the `str` class. Did you mean to create a new instance of ***your*** class? – Tomerikoo Feb 02 '21 at 11:28
  • @Tomerikoo yes - i am looking for guidance the best approach i can take to fulfil my requirement. – Caadi Waaye Feb 02 '21 at 11:51

2 Answers2

0

Your JSON file has new lines, you must get rid of them. Try the following:

json_file_data = file.read().replace("\n","")
Zeh
  • 301
  • 2
  • 4
0

if you read a file file.read() it will at least in this casse be converted into a string. in order to properly read a JSON file you want to do something like this

with open("your file nane.json", "r") as file:
     data = json.load(file)

in your case data will be

{'name': ['Caadi', 'Iskadhig'], 'location': 20356, 'job': 'Engineer', 'address': [{'city': 'Swindon', 'county': ['Avon']}]}

you can than read the data out of this dictionary in the same way you would any other

Julian wandhoven
  • 268
  • 2
  • 11