Skip to content

Instantly share code, notes, and snippets.

@aslamanver
Last active March 2, 2020 21:44
Show Gist options
  • Save aslamanver/a05e8de98479f617571a97fe3fe48c3c to your computer and use it in GitHub Desktop.
Save aslamanver/a05e8de98479f617571a97fe3fe48c3c to your computer and use it in GitHub Desktop.
JSON List Filter in Python
import json
def json_file_filter_list(term, column, file) :
with open(file) as json_file:
data = json.load(json_file)
result = []
for p in data:
if term.lower() in p[column].lower() :
result.append(p)
return result
if __name__ == '__main__' :
# This method will read the value with the name mathing with term from names.json file and return the filtered list
filtered_list = json_file_filter_list(term = 'Sample 2', column = 'name', file='names.json')
print(filtered_list)
[
{
"name": "Aslam",
"age": "52"
},
{
"name": "Sample",
"age": "52"
},
{
"name": "Sample 2",
"age": "52"
},
{
"name": "Sample 4",
"age": "52"
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment