在JSON数组中搜索值

时间:2019-08-08 15:00:30

标签: python

我是Python的新手,所以也许有一种我不知道的简单方法。

但基本上,我有一个由API返回的dict的JSON数组:

[
  {
    "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
  }
]

我正在检索这些项目并提示用户输入ID。此ID必须与列表中的jobid之一匹配。如果没有,我只是想退出。我不确定获取用户输入的内容并验证其实际存在于数组中的最佳方法。

3 个答案:

答案 0 :(得分:3)

您可以列出Jobid并检查其中是否包含输入内容:

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...

答案 1 :(得分:1)

这就是我要做的方式:

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

答案 2 :(得分:1)

您可以使用此功能检查ID是否存在:

as.Date()