Reputation: 41
@app.get("/")
async def read_root():
# houses = json.load(open("data.json"))["houses"]["house"]
# return houses[0]["id"]
houses = json.load(open("data.json"))["houses"]["house"]
for house in houses:
return house["id"]
When I run the commented code, I receive "3070", which is working as intended, however when I try to loop it in the non-commented code, I also receive "3070" instead of a list of ids.
Can't figure this one out, thanks for the help :)
Upvotes: 0
Views: 396
Reputation: 361
The return
statement ends the loop and the function. Your loop is correct but it will stop at the first element because of return
. This is probably what you want:
houses = []
for house in json.load(open("data.json"))["houses"]["house"]:
houses.append(house["id"])
return houses
Upvotes: 2