Kevin
Kevin

Reputation: 512

Searching for a value in JSON array

I'm very new to Python, so maybe there is an easy way to do this that I just don't know about.

But basically, I have an JSON array of dicts returned by an API:

[
  {
    "jobid": 1842318,
    "0": 1842318,
    "name": "pool-fd-linux-mysql",
    "1": "pool-fd-linux-mysql",
    "type": "B",
    "2": "B",
    "level": "I",
    "3": "I",
    "starttime": "08\/08\/2019 06:21",
    "4": "2019-08-08 06:21:23",
    "endtime": "08\/08\/2019 06:48",
    "5": "2019-08-08 06:48:57",
    "jobfiles": 35501,
    "6": 35501,
    "jobbytes": 10231836742,
    "7": 10231836742,
    "fileset": "pool-fd",
    "8": "pool-fd",
    "id": "node_130080",
    "starttime_sql": "2019-08-08 06:21:23",
    "endtime_sql": "2019-08-08 06:48:57",
    "director": 8
  },
  {
    "jobid": 1834914,
    "0": 1834914,
    "name": "pool-fd-linux-mysql",
    "1": "pool-fd-linux-mysql",
    "type": "B",
    "2": "B",
    "level": "I",
    "3": "I",
    "starttime": "08\/05\/2019 06:09",
    "4": "2019-08-05 06:09:25",
    "endtime": "08\/05\/2019 06:29",
    "5": "2019-08-05 06:29:49",
    "jobfiles": 32581,
    "6": 32581,
    "jobbytes": 6406793157,
    "7": 6406793157,
    "fileset": "pool-fd",
    "8": "pool-fd",
    "id": "node_130080",
    "starttime_sql": "2019-08-05 06:09:25",
    "endtime_sql": "2019-08-05 06:29:49",
    "director": 8
  },
  {
    "jobid": 1832309,
    "0": 1832309,
    "name": "pool-fd-linux-mysql",
    "1": "pool-fd-linux-mysql",
    "type": "B",
    "2": "B",
    "level": "I",
    "3": "I",
    "starttime": "08\/04\/2019 06:39",
    "4": "2019-08-04 06:39:53",
    "endtime": "08\/04\/2019 07:00",
    "5": "2019-08-04 07:00:03",
    "jobfiles": 32517,
    "6": 32517,
    "jobbytes": 7216673446,
    "7": 7216673446,
    "fileset": "pool-fd",
    "8": "pool-fd",
    "id": "node_130080",
    "starttime_sql": "2019-08-04 06:39:53",
    "endtime_sql": "2019-08-04 07:00:03",
    "director": 8
  },
  {
    "jobid": 1829930,
    "0": 1829930,
    "name": "pool-fd-linux-mysql",
    "1": "pool-fd-linux-mysql",
    "type": "B",
    "2": "B",
    "level": "I",
    "3": "I",
    "starttime": "08\/03\/2019 06:25",
    "4": "2019-08-03 06:25:40",
    "endtime": "08\/03\/2019 06:46",
    "5": "2019-08-03 06:46:59",
    "jobfiles": 33757,
    "6": 33757,
    "jobbytes": 7583914632,
    "7": 7583914632,
    "fileset": "pool-fd",
    "8": "pool-fd",
    "id": "node_130080",
    "starttime_sql": "2019-08-03 06:25:40",
    "endtime_sql": "2019-08-03 06:46:59",
    "director": 8
  },
  {
    "jobid": 1827481,
    "0": 1827481,
    "name": "pool-fd-linux-mysql",
    "1": "pool-fd-linux-mysql",
    "type": "B",
    "2": "B",
    "level": "I",
    "3": "I",
    "starttime": "08\/02\/2019 07:01",
    "4": "2019-08-02 07:01:16",
    "endtime": "08\/02\/2019 07:24",
    "5": "2019-08-02 07:24:55",
    "jobfiles": 33724,
    "6": 33724,
    "jobbytes": 6524983779,
    "7": 6524983779,
    "fileset": "pool-fd",
    "8": "pool-fd",
    "id": "node_130080",
    "starttime_sql": "2019-08-02 07:01:16",
    "endtime_sql": "2019-08-02 07:24:55",
    "director": 8
  },
  {
    "jobid": 1825053,
    "0": 1825053,
    "name": "pool-fd-linux-mysql",
    "1": "pool-fd-linux-mysql",
    "type": "B",
    "2": "B",
    "level": "I",
    "3": "I",
    "starttime": "08\/01\/2019 06:09",
    "4": "2019-08-01 06:09:50",
    "endtime": "08\/01\/2019 06:30",
    "5": "2019-08-01 06:30:09",
    "jobfiles": 34335,
    "6": 34335,
    "jobbytes": 7567891160,
    "7": 7567891160,
    "fileset": "pool-fd",
    "8": "pool-fd",
    "id": "node_130080",
    "starttime_sql": "2019-08-01 06:09:50",
    "endtime_sql": "2019-08-01 06:30:09",
    "director": 8
  },
  {
    "jobid": 1822894,
    "0": 1822894,
    "name": "pool-fd-linux-mysql",
    "1": "pool-fd-linux-mysql",
    "type": "B",
    "2": "B",
    "level": "I",
    "3": "I",
    "starttime": "07\/31\/2019 06:42",
    "4": "2019-07-31 06:42:59",
    "endtime": "07\/31\/2019 07:06",
    "5": "2019-07-31 07:06:33",
    "jobfiles": 35396,
    "6": 35396,
    "jobbytes": 7376675799,
    "7": 7376675799,
    "fileset": "pool-fd",
    "8": "pool-fd",
    "id": "node_130080",
    "starttime_sql": "2019-07-31 06:42:59",
    "endtime_sql": "2019-07-31 07:06:33",
    "director": 8
  },
  {
    "jobid": 1820584,
    "0": 1820584,
    "name": "pool-fd-linux-mysql",
    "1": "pool-fd-linux-mysql",
    "type": "B",
    "2": "B",
    "level": "I",
    "3": "I",
    "starttime": "07\/30\/2019 06:39",
    "4": "2019-07-30 06:39:38",
    "endtime": "07\/30\/2019 07:09",
    "5": "2019-07-30 07:09:06",
    "jobfiles": 33396,
    "6": 33396,
    "jobbytes": 6955057010,
    "7": 6955057010,
    "fileset": "pool-fd",
    "8": "pool-fd",
    "id": "node_130080",
    "starttime_sql": "2019-07-30 06:39:38",
    "endtime_sql": "2019-07-30 07:09:06",
    "director": 8
  },
  {
    "jobid": 1817881,
    "0": 1817881,
    "name": "pool-fd-linux-mysql",
    "1": "pool-fd-linux-mysql",
    "type": "B",
    "2": "B",
    "level": "I",
    "3": "I",
    "starttime": "07\/29\/2019 06:27",
    "4": "2019-07-29 06:27:04",
    "endtime": "07\/29\/2019 06:44",
    "5": "2019-07-29 06:44:56",
    "jobfiles": 32177,
    "6": 32177,
    "jobbytes": 5225536483,
    "7": 5225536483,
    "fileset": "pool-fd",
    "8": "pool-fd",
    "id": "node_130080",
    "starttime_sql": "2019-07-29 06:27:04",
    "endtime_sql": "2019-07-29 06:44:56",
    "director": 8
  },
  {
    "jobid": 1815539,
    "0": 1815539,
    "name": "pool-fd-linux-mysql",
    "1": "pool-fd-linux-mysql",
    "type": "B",
    "2": "B",
    "level": "F",
    "3": "F",
    "starttime": "07\/28\/2019 06:42",
    "4": "2019-07-28 06:42:26",
    "endtime": "07\/28\/2019 10:23",
    "5": "2019-07-28 10:23:36",
    "jobfiles": 1348007,
    "6": 1348007,
    "jobbytes": 251894783789,
    "7": 251894783789,
    "fileset": "pool-fd",
    "8": "pool-fd",
    "id": "node_130080",
    "starttime_sql": "2019-07-28 06:42:26",
    "endtime_sql": "2019-07-28 10:23:36",
    "director": 8
  }
]

I am retrieving these items and prompting the user to input an ID. This ID MUST match one of the jobids in the list. If not, I simply want to exit. I'm not sure the best way to take what the user entered and verify that it actually exists in the array.

Upvotes: 1

Views: 72

Answers (3)

Nick
Nick

Reputation: 3845

You could use this function to check if the id exists:

def id_exists(id):
    ids = [x['jobid'] for x in json_array]
    if id in ids:
        return True
    else:
        return False

Upvotes: 1

Alex_6
Alex_6

Reputation: 319

Here's the way that i would do it :

def search_in_json(json_list, id):
   filtered_json = list(filter(lambda x: x['jobid'] == id, json_list))
   if filtered_json:
      return filtered_json # Or whatever you want to return
   else:
      return 0 # Or raise an error

Upvotes: 1

Dan
Dan

Reputation: 45752

You can make a list of jobids and check if the input is in it:

jobids = [job["jobid"] for job in your_json]
if input_id not in jobids:
    raise ValueError(f"Bad jobid: {input_id}")
# The rest of your code that relies on input_id being in the json goes here...

Upvotes: 3

Related Questions