Import JSON and check for condition

less than 1 minute read

Task

Given the following JSON file, print the name of every horror book

{
  "books":[
    {
      "name":"X",
      "genere":"horror",
      "publisher":"A"
    },
    {
      "name":"Y",
      "genere":"tech",
      "publisher":"B"
    },
    {
      "name":"Z",
      "genere":"horror",
      "publisher":"C"
    }
  ]
}

Solution

from json import load

with open("json_file") as f:
    books = load(f)
    for book in books:
        if book['genere'] == "horror":
            print(book['name'])

Categories:

Updated:

Comments