Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I am trying to print information from a json file with python

I have this .JSON file that tells what food there will be during the week in our school.(Sorry it’s in Swedish)

Here is the JSON file

{
  "feedbackAllowed": "True",
  "weeks": [
    {
      "days": [
        {
          "date": 1647820800,
          "items": [
            "Korv Stroganoff serveras med ris",
            "Vegetarisk Stroganoff med sojakorv serveras ris"
          ]
        },
        {
          "date": 1647907200,
          "items": [
            "Pasta serveras med laxsås",
            "Vegetarisk pastasås"
          ]
        },
        {
          "date": 1647993600,
          "items": [
            "Morotslasagne med keso och soltorkad tomat",
            "Kökets klimatsmarta rätt ( vegetarisk lasagne)"
          ]
        },
        {
          "date": 1648080000,
          "items": [
            "Het kycklingsoppa serveras med mjukt bröd och ost samt frukt",
            "Vegetarisk nudelsoppa serveras med mjukt bröd och ost samt frukt"
          ]
        },
        {
          "date": 1648166400,
          "items": [
            "Quorngryta med chili serveras med ris",
            "Kökets klimatsmarta rätt"
          ]
        }
      ],
      "number": 12,
      "year": 2022
    }
  ],
  "school": {
    "URLName": "XXX",
    "id": 000,
    "district": {
      "province": {
        "URLName": "XXX",
        "id": 000,
        "name": "XXX"
      },
      "URLName": "XXX",
      "id": 000,
      "name": "000"
    },
    "name": "000"
  },
  "id": 000,
  "bulletins": [
    {
      "text": "XXX"
    }
  ]
}

What I am looking for is a way to only print out the "items"(all of them) from the JSON file. I have watched many tutorials and i keep getting errors like:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

TypeError: '_io.TextIOWrapper' object is not callable

etc.

i tried using this

import json
with open('filename.json', 'r') as input:
    obj = json.load(input)
    #make it a string the item is the first one
    print(str(obj['items']))

but i got this error:

KeyError: 'items'

>Solution :

You have to loop over all the weeks and days.

for week in obj['weeks']:
    for day in week['days']:
        print(day['items'])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading