1

I am trying to get values from a JSON file and store it in a python Dictionary.

I want to assign the values for what ever is available from the keys I give and for those which are not available I want to store as "None"

I tried the following,

try:
    dataDict['syn']= results['synonyms']
    dataDict['reactivity']= results['crossReactivity']
    dataDict['Temp']= results['storageTemp']
    dataDict['catNum']= results['catalogNum']
    dataDict['shipping']= results['shippingInstructions']
except KeyError as e:
    dataDict[e.args[0]] = "None"
sa = f"""
        synonyms: {dataDict['syn']}
        crossReactivity: {dataDict['reactivity']}
        storageTemp: {dataDict['Temp']}
        catalogNum: {dataDict['catNum']}
        shippingInstructions: {dataDict['shipping']}
        """
print (sa) 

I get the following output,

synonyms: None
crossReactivity:
storageTemp:
catalogNum:
shippingInstructions:

If 'synonyms' is not available on the JSON file 'None' is assigned to dataDict['syn'] but where as the rest of the statements are getting ignored.

How to execute the rest of the assignments without having to write each and everything in separate try and except?

Guru
  • 27
  • 6
  • first I see a `(`in your codebefore the results - is that needed? then I would write it in a loop. like `if data availible` put it in the dict `else` put none. – grumpyp Dec 24 '20 at 09:13
  • 2
    you should use `dict.get(key, default_value)` – furas Dec 24 '20 at 09:16
  • Does this answer your question? [Return None if Dictionary key is not available](https://stackoverflow.com/questions/6130768/return-none-if-dictionary-key-is-not-available) – Tomerikoo Dec 24 '20 at 09:18
  • The ( was a copy pasting error, I have corrected it. I have to get around 54 values like this. so is there any other way of writing the code or automating instead of writing if-else statements or try-except for all the 54 cases? Thanks. @grumpyp – Guru Dec 24 '20 at 09:19
  • @Guru Here and in the link above you a combined number of `13` answers to that. Didn't any of them help you? – Tomerikoo Dec 24 '20 at 09:24
  • @Tomerikoo it partially answered. For fetching catalog number if I have to write results['response']['docs'][0]['catalogNum'] since its a dict of dicts how do I use it with .get()?. Sorry if its silly question. Thanks. – Guru Dec 24 '20 at 09:43
  • https://stackoverflow.com/a/65385965/10367459 answered my previous comment. – Guru Dec 24 '20 at 10:11

1 Answers1

5

You can use .get(), which takes a 'default' value:

dataDict = {}
results = {"synonyms": "foo", "storageTemp": 15}

dataDict['syn'] = results.get('synonyms', None)
dataDict['reactivity'] = results.get('crossReactivity', None)
dataDict['Temp'] = results.get('storageTemp', None)
dataDict['catNum'] = results.get('catalogNum', None)
dataDict['shipping'] = results.get('shippingInstructions', None)

print(dataDict)

Out:

{'shipping': None, 'catNum': None, 'reactivity': None, 'syn': 'foo', 'Temp': 15}
Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47
  • I am sorry if its a noob question since its a JSON file the data is in dict of dicts and dict of list of dicts all mixed. So how do I write the for this query "results['response']['docs'][0]['catalogNum']" using .get()? – Guru Dec 24 '20 at 09:33
  • (https://stackoverflow.com/a/65385965/10367459) answered my previous comment. – Guru Dec 24 '20 at 10:13