Here I want to find the matching json object from the json file(sample below). The provided json object which needs to be matched inside the .json
file will also have same format.
After the json object gets matched to one of them, then i want to return the previous object of that matched json object.
What is the best and fastest way to perform this with python ?
my_json_file.json
{
"all_data": [
{
"key":"value",
"key2": {
"key": "val"
}
},
{
"key":"value",
"key2": {
"key": "val"
}
},
{
"key":"value",
"key2": {
"key": "val"
}
},
]
}
get_value.py
with open('my_json.json') as file:
file_data = json.load(file)
if some_json in file_data:
# return previous json
2
Answers
You could use
enumerate
to iterate overfile_data['all_data']
:Not sure what are you looking for but your code looks right to me