Reputation: 1194
I'm trying to take in a json object as input and find a count of items that matches a certain criteria. The json object structure is in a nested python dictionary for example:
businesses= ["{\"hours\":
{
\"tuesday\": [[\"11:30\", \"23:00\"]],
\"thursday\": [[\"11:30\", \"23:00\"]],
\"wednesday\": [[\"11:30\", \"23:00\"]],
\"friday\": [[\"11:30\", \"23:00\"]],
\"sunday\": [[\"9:00\", \"23:00\"]],
\"monday\": [[\"11:30\", \"23:00\"]],
\"saturday\": [[\"11:30\", \"23:00\"]]
},
\"name\": \"XYZ\"
}"]
There will be multiple items in that structure. What I am having issues with is writing the for loop to go into the levels, and search for businesses which are open on Sundays before 10 am.
So something like this:
def count_businesses(object):
for i in object:
for j in i:
....
But when I get to that level, it seems to output each and every letter of the dictionary. Also, I'm not sure how to write the function to find the days and the time it is open, which for me would have to be find Sundays before 10 am, and return a count. The time is in an array inside the dictionary in this object as shown.
Any help would be appreciated!
Upvotes: 0
Views: 2482
Reputation: 11
Add this as a null check condition for Sunday.
if("sunday" in business_obj["hours"])
Upvotes: 1
Reputation: 73
it seems that there is some confusion between what a python dictionary is. The data you have in the array in businesses
is actually a string in JavaScript Object Notation (JSON) and python sees it as a string. For you to use it as a python dictionary, you will need to convert it using python's json
library. The conversion would look something like,
import json
python_obj = json.loads(json_str)
The object you provided is an Array of a JSON string such as
businesses = ["{\"hours\":"
"{"
"\"tuesday\": [[\"11:30\", \"23:00\"]],"
"\"thursday\": [[\"11:30\", \"23:00\"]],"
"\"wednesday\": [[\"11:30\", \"23:00\"]],"
"\"friday\": [[\"11:30\", \"23:00\"]],"
"\"sunday\": [[\"9:00\", \"23:00\"]],"
"\"monday\": [[\"11:30\", \"23:00\"]],"
"\"saturday\": [[\"11:30\", \"23:00\"]]"
"},"
"\"name\": \"XYZ\""
"}"]
An array of a python dictionary would look like the following
businesses = [
{
"hours":{
"tuesday":[["11:30","23:00"]],
"thursday":[["11:30","23:00"]],
"wednesday":[["11:30","23:00"]],
"friday":[["11:30", "23:00"]],
"sunday":[["9:00", "23:00"]],
"monday":[["11:30", "23:00"]],
"saturday":[["11:30", "23:00"]]
},
"name":"XYZ"
}
]
And so, the reason you are seeing it output each letter is because you are iterating through a string, not a python dictionary. When python iterates through a string, it looks through each character. Just like the following.
string_data = "123456789"
# will iterate through each character
for i in string_data:
print(i) # will print 9 times each time outputting a character in order
As for the function, you will need to make sure that when you do your time comparisons, you are using python time objects instead of strings since that will accurately compare time. I'm not entirely sure why the times are in listed in a nested array such as [["11:30","23:00"]]
and so you will likely need to modify the following function if the data is formatted differently for other businesses.
This is a function that describes what you need.
import json, datetime
businesses = ["{\"hours\":"
"{"
"\"tuesday\": [[\"11:30\", \"23:00\"]],"
"\"thursday\": [[\"11:30\", \"23:00\"]],"
"\"wednesday\": [[\"11:30\", \"23:00\"]],"
"\"friday\": [[\"11:30\", \"23:00\"]],"
"\"sunday\": [[\"9:00\", \"23:00\"]],"
"\"monday\": [[\"11:30\", \"23:00\"]],"
"\"saturday\": [[\"11:30\", \"23:00\"]]"
"},"
"\"name\": \"XYZ\""
"}"]
def count_businesses(business_list):
"""
:param business_list: An array of business in JSON to query from
:return: Int of the count of businesses that are open on Sunday before 10 am
"""
# initialize the array that will contain the businesses that meet the search criteria
businesses_found = []
# python time object of 10:00am that will be used to check against
opening_business_time = datetime.time(hour=10)
# iterate through each busineses to check if it meets the search criteria
for business in business_list:
# since each business is in JSON, we convert it into a Python object
business_obj = json.loads(business)
# Look into the 'hours' key, then the 'sunday' key and get the first item in the array. ( i.e ["11:30","23:00"])
sunday_hours = business_obj["hours"]["sunday"][0]
# read in the sunday opening hours as a string from the first value of the array. {i.e "11:30")
sunday_opening_hours_str = sunday_hours[0]
# convert the sunday opening hours into a time object so it can be compared.
# '%H:%M' looks for the format HH:MM in a string.
# for more reference. https://docs.python.org/3.6/library/datetime.html#strftime-and-strptime-behavior
sunday_opening_hours_time = datetime.datetime.strptime(sunday_opening_hours_str, '%H:%M').time()
# if sunday opening hours is before 10 am
if sunday_opening_hours_time < opening_business_time:
# add the business object to the list
businesses_found.append(business_obj)
# returns the count of the businesses that met the search criteria
return len(businesses_found)
total = count_businesses(businesses)
print(total)
Upvotes: 1