I am writing a program that queries an API that responds with a JSON object. The JSON object is multilevel with several arrays and key value pairs. There is a fairly large number of items I would like to retrieve from each response, and typing out each path multiple times for each function is turning out to be time consuming and cluttering.
I would like to store each JSON path in a dictionary to iterate over. A quick example of what I am trying to accomplish:
api_response = api_request(query)
paths_to_data = {}
paths_to_data["author"] = "['Items'][0]['AttributeSets'][0]['Author']"
paths_to_data["actor"] = "['Items'][0]['AttributeSets'][0]['Actor']"
paths_to_data["format"] = "['Items'][0]['AttributeSets'][0]['Format']"
cleaned_response = {}
for a in paths_to_data.keys():
cleaned_response[a] = api_response.paths_to_data[a]
CodePudding user response:
Since you're hardcoding the paths it should be safe to use eval
.
cleaned_response[a] = eval(f"api_response{paths_to_data[a]}")
For something like this, I would usually use jq unless it has to be implemented in Python, in which case you could look for some JSONPath library. I'm not too familiar with any JSONPath libraries so I can't recommend any, but I've used similar things before for XML.
CodePudding user response:
Don't use strings. Use functions:
extractors = {}
extractors["author"] = lambda d: d['Items'][0]['AttributeSets'][0]['Author']
extractors["actor"] = lambda d: d['Items'][0]['AttributeSets'][0]['Actor']
extractors["format"] = lambda d: d['Items'][0]['AttributeSets'][0]['Format']
cleaned_data = {}
for item, extractor in paths_to_data:
cleaned_response[item] = extractor(api_response)