-1

I have been trying to create a function to use as an importable tool for parsing json. here is my code I have so far

#json parser
def jsonimporter(file_path , env_to_return):
    import json
    a = open(file_path,"r") #opens and sets the file to read

    data = a.read()         #sets variable to read function
    print(data)

    json_data = json.loads(data) #loads the json data and stores it in json_data variable


    print(json_data)
    json_data.get(env_to_return)

    return json_data

What my issue is, is that when I call the function in another file it does not display the parsed json it just displays the json as a a dict in this form;

{u'Environments': [{u'Dev': [u'111', u'222']}, {u'Qa': [u'333', u'444']}, {u'prod': [u'555', u'666']}]}

The print statements you see are just me trying to double check my answers. Also I have passed the call the correct parameters as it prints out the parameters if I ask it too. Thank you for your help!

dev = jsonimporter("test2.json","dev")
# dosomething with value
print(value)

This is how I called the function

Barmar
  • 741,623
  • 53
  • 500
  • 612
aroe
  • 499
  • 1
  • 6
  • 15
  • 2
    Parsed JSON *is* a dict. – Barmar Nov 19 '19 at 21:49
  • Shouldn't you return `json_data.get(env_to_return)`? – Barmar Nov 19 '19 at 21:50
  • And maybe you need to loop through the `Environments` list, and return the one where the dictionary key matches `env_to_return`. – Barmar Nov 19 '19 at 21:51
  • 1
    BTW, there's no need to call `a.read()` and `json.loads()`, you can just use `json.load(a)` – Barmar Nov 19 '19 at 21:52
  • 1
    And you should close the file after you're done with it. Best is to use `with` so this happens automatically. – Barmar Nov 19 '19 at 21:52
  • Why do you have a list of dictionaries that each have a different key? It would make more sense to have a single dictionary with each environment as a different key. – Barmar Nov 19 '19 at 21:58
  • Can you show how you're calling the function and the result you're trying to get? – Barmar Nov 19 '19 at 21:59
  • ```dev = jsonimporter("test2.json","dev") # dosomething with value print(value)``` This is how I am calling the function – aroe Nov 19 '19 at 22:10
  • Put it in the question itself. – Barmar Nov 19 '19 at 22:12
  • When you put code blocks in a question, the code has to start on the line *after* the triple backticks, not the same line. Does the little diagram below the question confuse you? See https://meta.stackoverflow.com/questions/391467/users-frequently-start-code-blocks-on-the-same-line-as-the-triple-backticks – Barmar Nov 19 '19 at 22:18
  • @Barmar is on top of this, good advice so far, what I will add is that the imports should go at the top of your file. Following proper style, especially this kind of rule, is important. Also, calling `my_dict.get(one_arg)` is pointless, just use `my_dict[one_arg]` instead. – AMC Nov 19 '19 at 23:15
  • @AlexanderCécile The latter depends on what you want to happen when the key isn't found. `get()` will return a default value, `my_dict[one_arg]` will throw an error. – Barmar Nov 20 '19 at 17:21
  • @Barmar For some reason I thought `.get()` without the default value would behave exactly like `[]`, forgetting that the uh, default default value, is None. – AMC Nov 20 '19 at 18:35
  • The `import json` should be at the top of the file, not in the function. – AMC Nov 20 '19 at 19:44

2 Answers2

0

You need to loop through the Environments property, searching for the dictionary with the key you want.

def jsonimporter(file_path, key, env_to_return):
    import json
    with open(file_path,"r") as a:
        json_data = json.load(a)

    for d in json_data[key]:
        if env_to_return in d:
            return d[env_to_return]

dev = jsonimporter("test2.json", "Environments", "dev")
print(dev)

Also see How to get string objects instead of Unicode from JSON? for how to get JSON with ordinary strings instead of Unicode strings in Python 2.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I like this loop for the answer however, could I used file_path instead of 'environments' because this is for a utility function and the 'Environments' is specific to this particular json – aroe Nov 19 '19 at 22:22
  • `file_path` is the filename, not the name of the key in the dictionary. – Barmar Nov 19 '19 at 22:23
  • 2
    Why is the parameter called `env_to_return` if this function isn't specifically about environments? – Barmar Nov 19 '19 at 22:24
  • 2
    You could make that key another parameter to the function. I've updated the answer to show how to do this. – Barmar Nov 19 '19 at 22:25
0

Here is my final answer I got.

def get_multiple_envs_using_jsonimporter(file_path , env_list):
    var1 = jsonimporter("test2.json","Dev")
    var2 = jsonimporter("test2.json","Qa")

    full_list=[]
    full_list.append(var1)
    full_list.append(var2)
    print(full_list)
    # for each env
    # var = jsonimporter(file_path, env)
    # add environment+accounts to list_to_return
    # dev = jsonimporter("test2.json","Dev")









def jsonimporter(file_path , env_to_return):
    a = open(file_path,"r") #opens and sets the file to read

    data = a.read()         #sets variable to read function
    # print(data)

    json_data = json.loads(data) #loads the json data and stores it in json_data variable


    for env in json_data["Environments"]:
        if env_to_return in env:
            return env[env_to_return]
        else:
             print('Not Found')

I just appended the variables to the list that called the first function from.

aroe
  • 499
  • 1
  • 6
  • 15