0

This is my very first day learning Python (I do TypeScript and .NET). I am wondering if there is a way, like LINQ in .NET, to read and parse a JSON file for a particular value and when that value is found grab all those objects. I have a JSON file below and can load it with the following code but am trying to find the specific object value and get all it's data.

JSON File

{
   "10":{
      "objectId":10,
      "groups":[
         {
            "group":"GR100",
            "ages":[
               1,
               10,
               15
            ]
         },
         {
            "group":"GR201",
            "ages":[
               2,
               17
            ]
         }
      ],
      "connections":[
         {
            "group":"GR303",
            "ages":[
               40,
               51
            ],
            "groupCheck":true,
            "groupCheckExceptions":[
               "GR402"
            ]
         },
         {
            "group":"GR303",
            "ages":[
               1,
               2,
               3,
               4,
               5,
               6
            ],
            "groupB":"GR402",
            "agesB":[
               2,
               5
            ]
         }
      ]
   },
   "11":{
      "objectId":20,
      "groups":[
          { ... similar to above }
      ],
      "connections":[
          { ... similar to above }
      ]
   }
}

For the JSON File, these are the rules:

  • objectId is required and unique, not null int
  • group is required but can be empty
    • group is required and unique with groups, not null string
    • ages is required and unique with groups, not null comma separated ints
  • connections is required but can be empty
    • group is required and unique with connections, not null string

    • ages is required and unique with connections, not null comma separated ints

      REQUIRED (one or the other together)

    • groupCheck is boolean

    • groupCheckExceptions not null comma separated string of group names

      or

    • groupB is unique, not null string (group and groupB can be the same)

    • agesB is not null comma separated ints

Python File

import json

# JSON objects
json_objects = None

with open("thejsonfile.json", "r") as file:
    json_objects = json.load(file)

# Need to find when an input comes in as an int (already have that part), like so where x is an int
input_object = x 

# then need to find out if that input_object is equal to an objectId in the file and bring in
# all it's data to the JSONClass
allMyObjects = JSONClass(**json_objects)

Still have to figure out the JSONCLass yet as remember this is my first day of Python. I want a list of groups and connections to use. Am I on the right track?

cdub
  • 24,555
  • 57
  • 174
  • 303

0 Answers0