0

I am trying to get some values stored in a JSON file through python 3.7. The file contain values but it is returning null.

Here is the JSON file:

{
   "data_columns":[
      "yearsexperience",
      "milesfrommetropolis",
      "ceo",
      "cfo",
      "cto",
      "janitor",
      "junior",
      "manager",
      "senior",
      "vice president",
      "bachelors",
      "doctoral",
      "high school",
      "masters",
      "others",
      "biology",
      "business",
      "chemistry",
      "computer science",
      "engineering",
      "literature",
      "math",
      "none",
      "physics",
      "automobile",
      "education",
      "finance",
      "health",
      "oil",
      "service",
      "web"
   ]
}

And here is how I am trying to fetch the values:

import json, pickle

__jobType = None
__data_columns = None
__model = None

def get_jobtpye():
    return __jobType

def load_saved_artifacts():
    print('Loading saved artifacts!')
    global __jobType
    global __data_columns

    with open("./artifacts/columns.json", 'r') as f:
        __data_columns = json.load(f)['data_columns']
        __jobType = __data_columns[2:10]

if __name__ == '__main__':
    load_saved_artifacts()
    get_jobtpye()

When I am checking if the __jobType variable contains any thing, then i am finding that it does. It just won't return anything. I have been stuck in this problem for a week now. Please help.

roganjosh
  • 12,594
  • 4
  • 29
  • 46
Aunkit Chaki
  • 43
  • 2
  • 9
  • What do you mean by "it just won't return anything"? What exactly are you expecting to happen when you run the code, and how is that different from what actually happens? – Karl Knechtel Apr 04 '20 at 08:42
  • 2
    What's with the global declarations and name mangling? This is not the way to approach coding in python – roganjosh Apr 04 '20 at 08:43
  • @roganjosh, sorry I get that! – Aashish Chaubey Apr 04 '20 at 08:56
  • What I meant was that it was supposed to return a list of values from "ceo" to "vice-president" (if you check the indexes of the json file) but instead it shows nothing. The program runs smooth, with any errors, warnings and output. Just the string "loading artifacts" gets printed that's it. – Aunkit Chaki Apr 04 '20 at 09:13

3 Answers3

1

Your code works well, you just need to print the result of assign it to another variable:

print(get_jobtpye())
# or
job_type = get_jobtpye()

BUT -

Why do you need those global variables? this is absolutely unnecessary and it's a very bad practice.

You can just return values from your functions:

import json, pickle

def load_saved_artifacts():
    print('Loading saved artifacts!')
    with open("./artifacts/columns.json", 'r') as f:
        data_columns = json.load(f)['data_columns']
        return data_columns[2:10]

if __name__ == '__main__':
    job_type = load_saved_artifacts()
Community
  • 1
  • 1
Gabio
  • 9,126
  • 3
  • 12
  • 32
  • Hi. I have tried the print(get_jobtype()) thing. It shows "None" in the output. – Aunkit Chaki Apr 04 '20 at 09:09
  • @AunkitChaki: Did you call `load_saved_artifacts()` beforehand? – martineau Apr 04 '20 at 09:19
  • Yes. I did. It in the code. I simply can't find anything why my code isn't running. – Aunkit Chaki Apr 04 '20 at 09:40
  • 1
    Are you sure that this is not an issue in another part of the code? Cause the snippet you shared is running well on my machine and I can't reproduce your issue. Did you try my suggested approach, without the global vars. It's much cleaner code. – Gabio Apr 04 '20 at 09:52
1

You need to catch the return of get_jobtpye() in some variable... that will have the values.

if __name__ == '__main__':
    load_saved_artifacts()
    answer = get_jobtpye()

Also, I believe, you don't actually need get_jobtpye() function. Your variable __jobType is already having the answer

sam
  • 2,263
  • 22
  • 34
-2

Have you tried using json.loads() ? It will take the file contents as a string and can be sliced. You can refer to this answer.

kshitij86
  • 129
  • 1
  • 8
  • They already use `json.load()` on the file object, which is the correct approach. There's no need to try read the file as a string and then use `json.loads()` – roganjosh Apr 04 '20 at 08:46