0

I have a little problem. I don't know much about json and I need help. I have main.py file and the .json file. I, for example, want to output a certain line from .json file in print() in main.py. For Example, the json file has the line "name":"Alex" and the second line "name":"John". I need to make sure that it finds the line "name":" Alex" in the json file and outputs the name Alex in main.py. I hope I have made my question clear

So this is a piece of json file. It's Schedule of university

"group": "КМБО-02-19",
      "days": [
        {
          "day": "ПН",
          "pars": [
            {
              "name": "Введение в ПД",
              "type": "зачет",
              "number": 3,
              "place": "Б-209",
              "whiteWeek": 17
            }
          ]
        },
        {
          "day": "ВТ",
          "pars": [
            {
              "name": "Программирование в ЗР",
              "number": 2,
              "place": "Б-209",
              "type": "зачет",
              "whiteWeek": 17
            },
            {
              "name": "Физкультура и спорт",
              "type": "зачет",
              "number": 5,
              "whiteWeek": 17
            }
          ]
        }
Adrian B
  • 1,490
  • 1
  • 19
  • 31
  • Why the name Alex? What is the overall structure of the JSON file? – kynnemall Apr 22 '20 at 11:28
  • 1
    A JSON file is not about single lines. It defines a whole datastructure. Read the file with the `json` module and then access the values you want. How to do that depends on the type of the structure. It might be a single list, but it could be something like a list in a dictionary in a dictionary in a list. – Matthias Apr 22 '20 at 11:28
  • [json is a documented format](https://www.json.org/json-en.html) and [there's a json package in the stdlib](https://docs.python.org/3/library/json.html) to unserialize json strings to Python objects. Once you have unserialzed your json to Python, it's just plain python dicts and lists stuff. – bruno desthuilliers Apr 22 '20 at 11:44
  • Your json snippet is not valid json. – bruno desthuilliers Apr 22 '20 at 11:45

3 Answers3

0

An example tailored exactly to OPs question edit:

import json

# read the json
# with open('data.txt') as f:
    # json_data = json.load(f)

json_data = {
    "group": "КМБО-02-19",
    "days": [
    {
      "day": "ПН",
      "pars": [
        {
          "name": "Введение в ПД",
          "type": "зачет",
          "number": 3,
          "place": "Б-209",
          "whiteWeek": 17
        }
      ]
    },
    {
      "day": "ВТ",
      "pars": [
        {
          "name": "Программирование в ЗР",
          "number": 2,
          "place": "Б-209",
          "type": "зачет",
          "whiteWeek": 17
        },
        {
          "name": "Физкультура и спорт",
          "type": "зачет",
          "number": 5,
          "whiteWeek": 17
        }
      ]
    }]}

# loop over each day and pars inside it
for day in json_data['days']:
    for par in day['pars']:

        # check if Alex and print
        if par['name'] == 'Alex':
            print(par['name'])
RaidasGrisk
  • 444
  • 2
  • 12
0

I think that this was already answered here: multiple Json objects in one file extract by python.

Where u can see how to store multiple objects in a file:

[
{"ID":"12345","Timestamp":"20140101", "Usefulness":"Yes",
  "Code":[{"event1":"A","result":"1"},…]},
{"ID":"1A35B","Timestamp":"20140102", "Usefulness":"No",
  "Code":[{"event1":"B","result":"1"},…]},
{"ID":"AA356","Timestamp":"20140103", "Usefulness":"No",
  "Code":[{"event1":"B","result":"0"},…]},
...
]

And later on load them:

import json

with open('file.json') as json_file:

data = json.load(json_file)

Where data is a python list of dicts (as bruno-desthuilliers pointed out):

data[index]["name"]
GuillemVS
  • 356
  • 1
  • 13
-1

While I am not a python guy. If I had to do that in c++ I would create own parser for JSON file and create a Token tree with all it's property and then use that as your library.

or

Read the docs

or

invert bit time to get the JSON parser made by other fellows

or

see the same question at stackoverflow

Sudip Ghimire
  • 677
  • 6
  • 15