Last active
March 2, 2020 21:44
-
-
Save aslamanver/a05e8de98479f617571a97fe3fe48c3c to your computer and use it in GitHub Desktop.
JSON List Filter in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"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