过滤 json 并使用 python 提取特定值

Filter json and extract specific value with python

我试图通过点击 api 来提取多个工件的大小。 json 以下数据是单个工件的数据,其大小重复于。

{
    "name" : "Payload",
    "type" : "table",
    "value" : {
      "Content-Type" : "",
      "Size" : -1
    }

{
    "name" : "Payload",
    "type" : "table",
    "value" : {
      "Content-Type" : "application/zip",
      "Size" : 3369
    }

在这个示例数据中:3369 是我需要的正确值,但尽管尝试了多个过滤器我还是无法提取它。

完成json单个工件的输出

  "parameters" : {
    "path" : "/mysql.odbc/5.1.14",
    "nexusUrl" : "http://fqdn"
  },
  "items" : [ {
    "name" : "Request",
    "type" : "topic",
    "value" : "Request"
  }, {
    "name" : "Details",
    "type" : "table",
    "value" : {
      "Action" : "GET",
      "path" : "/mysql.odbc/5.1.14"
    }
  }, {
    "name" : "Parameters",
    "type" : "table",
    "value" : {
      "describe" : "json"
    }
  }, {
    "name" : "Headers",
    "type" : "table",
    "value" : {
      "Accept" : "application/json",
      "User-Agent" : "curl/7.78.0",
      "Host" : "fqdn"
    }
  }, {
    "name" : "Attributes",
    "type" : "table",
    "value" : {
      "org.apache.shiro.subject.support.DefaultSubjectContext.SESSION_CREATION_ENABLED" : false,
      "Key[type=org.sonatype.nexus.security.SecurityFilter, annotation=[none]].FILTERED" : true,
      "authcAntiCsrf.FILTERED" : true,
      "nx-apikey-authc.FILTERED" : true
    }
  }, {
    "name" : "Payload",
    "type" : "table",
    "value" : {
      "Content-Type" : "",
      "Size" : -1
    }
  }, {
    "name" : "Response",
    "type" : "topic",
    "value" : "Response"
  }, {
    "name" : "Status",
    "type" : "table",
    "value" : {
      "Code" : 200,
      "Message" : ""
    }
  }, {
    "name" : "Headers",
    "type" : "table",
    "value" : {
      "ETag" : "\"df4f013db18103f1b9541cdcd6ba8632\"",
      "Content-Disposition" : "attachment; filename=mysql.odbc.5.1.14.nupkg",
      "Last-Modified" : "Tue, 13 Oct 2015 03:54:48 GMT"
    }
  }, {
    "name" : "Attributes",
    "type" : "table",
    "value" : { }
  }, {
    "name" : "Payload",
    "type" : "table",
    "value" : {
      "Content-Type" : "application/zip",
      "Size" : 3369
    }
  } ]
}

我是python的新手,下面是我不完整的代码及其输出供参考

import requests
import json
import re

repo_name = "repo"

file_list = ["/mysql.odbc/5.1.11","/mysql.odbc/5.1.14"]
for i in file_list:
   url = "http://fqdn/repository/{0}/{1}?describe=json".format(repo_name, i)
   response = requests.get(url)
   json_data = response.text
   data = json.loads(json_data)
   for size in data['items']:
       if size['name'] == 'Payload':
           print(size['value'])
{'Content-Type': '', 'Size': -1}
{'Content-Type': 'application/zip', 'Size': 3109}
{'Content-Type': '', 'Size': -1}
{'Content-Type': 'application/zip', 'Size': 3369}

感谢任何帮助。

也许这会让您走上正轨?

valid_values = [
    v for v in [
        e.get('value').get('Size', -1)
        for e in data['items']
        if isinstance(e.get('value'), dict)
    ]
    if v > 0
]

在您的数据中,只有一个有效值:

>>> valid_values
[3369]

所以要找到非 -1 值,只需检测那些并只打印其他值:

for i in file_list:
   url = "http://fqdn/repository/{0}/{1}?describe=json".format(repo_name, i)
   response = requests.get(url)
   json_data = response.text
   data = json.loads(json_data)
   for size in data['items']:
       if size['name'] == 'Payload':
           value_size = size['value']['Size']
           if value_size != -1:
               print(value_size)

请注意,我不是 requests 方面的专家,但我看过其他提取 json 信息的代码,代码如下:

response = requests.get(url)
data = response.json()

我不知道这是否适用于你的情况。