Search by User/Page
Lesson 6 of 10 in Coddy's Social media Search Project - Python JSON Fundamentals course.
Now you're asked to implement search features one-by-one. As we've mentioned before, there are 2 kinds of entities/account types - user and page.
You need to add a new filter so that the search can be narrowed down to a particular entity.
For eg., a user might want to search for all the users in the system, then they select user in the entity dropdown filter and then click on Search.
You'll be receiving a JSON string containing entity records to your function.
JSON objects will follow the below format. type may be user or page
For eg., a user record will look like:
{
"name": "Robert McNally",
"city": "Hawaii",
"type": "user"
}a page record will look like:
{
"name": "Friendly Neighborhood Coder",
"city": "Trivandrum",
"type": "page"
}A bunch of these objects will be packaged in a JSON array called result.
Please check the sample input in the challenge for input format.
Challenge
EasyWrite a function searchby_entity that gets,
all_entities- string-array - List of all entities from the JSON file in JSON string formatfilter_entity- string - Selected filter entity. Value could user or page or group
And outputs a python list, which is is the all_entities list filtered by the filter_entity.
Please note that all_entities is a JSON string. String is a JSON object which you'll need to convert to a dictionary object.
Example:
Input - all_entities =
{"result": [{
"name": "Robert McNally",
"city": "Hawaii",
"type": "user"
}, {
"name": "Friendly Neighborhood Coder",
"city": "Trivandrum",
"type": "page"
}, {
"name": "Coddy Official",
"city": "Haifa",
"type": "page"
}]}filter_entity = page
Expected Output - Below list
[{
"name": "Friendly Neighborhood Coder",
"city": "Trivandrum",
"type": "page"
}, {
"name": "Coddy Official",
"city": "Haifa",
"type": "page"
}]Explanation - The dictionary object with type="user" was removed from the all_entities list.
Try it yourself
def searchby_entity(all_entities, filter_entity):
# Write code here