需要从站点获取或转换数据才能成为有效的JSON

2024-04-24 12:11:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个脚本,将从一个网站(思科补丁网站)获得数据,并根据收到的数据,我需要发布到另一个网站(ServiceNow事件管理)。POST需要是REST/JSON,并带有特定的键才能工作。你知道吗

我有足够的代码来获取数据,我有代码来发布工作。你知道吗

我很难将从get获取的数据转换为有效的JSON键值对,以进行POST。你知道吗

我使用以下代码从Cisco网站获取新修补程序列表。我得到的是正确的数据,但是如果数据的格式不正确,我可以用它来发布到另一个JSON格式的工具(使用不同的键但是返回的信息中的值)。你知道吗

这起作用-

def getjson(ciscourl):
    response = urllib.request.urlopen(ciscourl)
    ciscodata = response.read().decode("utf-8")
    return json.loads(ciscodata)

我得到的数据如下所示(这个查询产生了2个补丁):

[{"identifier":"cisco-sa-20180521-cpusidechannel","title":"CPU Side-Channel Information Disclosure Vulnerabilities: May 2018","version":"1.5","firstPublished":"2018-05-22T01:00:00.000+0000","lastPublished":"2018-05-31T20:44:16.123+0000","workflowStatus":null,"id":1,"name":"Cisco Security Advisory","url":"https://tools.cisco.com/security/center/content/CiscoSecurityAdvisory/cisco-sa-20180521-cpusidechannel","severity":"Medium","workarounds":"No","cwe":null,"cve":"CVE-2018-3639,CVE-2018-3640","ciscoBugId":"","status":"Updated","summary":"On May 21, 2018, researchers disclosed two vulnerabilities that take advantage of the implementation of speculative execution of instructions on many modern microprocessor architectures to perform side-channel information disclosure attacks. These vulnerabilities could allow an unprivileged, ","totalCount":6,"relatedResource":[]},{"identifier":"cisco-sa-20180516-firepwr-pb","title":"Cisco Firepower Threat Defense Software Policy Bypass Vulnerability","version":"1.0","firstPublished":"2018-05-16T16:00:00.000+0000","lastPublished":"2018-05-16T16:00:00.000+0000","workflowStatus":null,"id":1,"name":"Cisco Security Advisory","url":"https://tools.cisco.com/security/center/content/CiscoSecurityAdvisory/cisco-sa-20180516-firepwr-pb","severity":"Medium","workarounds":"No","cwe":"CWE-693","cve":"CVE-2018-0297","ciscoBugId":"CSCvg09316","status":"New","summary":"A vulnerability in the detection engine of Cisco Firepower Threat Defense software could allow an unauthenticated, remote attacker to bypass a configured Secure Sockets Layer (SSL) Access Control (AC) policy to block SSL traffic.The vulnerability is due to the incorrect handling ","totalCount":6,"relatedResource":[]}]

我可以从中提取值,比如print(jarray.get('identifier')),但是我很难用我定义的键将这些值映射到我自己的JSON映射中。所以我得到的键identifier的值需要映射到JSON映射中名为"node"的键。你知道吗

我试过json.loadsjson.loadjson.dumpjson.dumps。每次错误都是属性类型错误。你知道吗

这是我困惑的代码:

def createJson(l):
#try:

    jsonarray = l
    o_source = "CiscoUpdatePatchChecker"
    o_node = (jsonarray.get('identifier')) #this does not work
    o_metric_name = ("Critical")
    o_type = ("test")
    o_resource = ("test_resource")
    o_description = jsonarray  #this works
    o_event_class = ("test event class")
    o_additional_info = jsonarray
    print ("-" * 50)
    print (o_source, o_node, o_metric_name, o_type, o_resource, o_description, o_event_class, o_additional_info)
    print ("-" * 50)
    data = {"source": o_source, "node": o_node, "metric_name": o_metric_name, "type": o_type, "resource": o_resource, "event_class": o_event_class, "description": o_description, "additional_info": o_additional_info}
    return json.dumps(data)
# except:
    #pass

除此之外,其余代码只是将数据发布到正在工作的ITSM。-你知道吗

def postjson(data):
    # try:
    url = posturl
    auth = HTTPBasicAuth(username, password)
    head = {'Content-type': 'application/json',
            'Accept': 'application/json'}
    payld = data
    ret = requests.post(url, auth=auth , data=payld, headers=head)
    # sys.stdout.write(ret.text)
    returned_data = ret.json()
    print(returned_data)

所以我的问题是把我得到的数据映射回我的键:值对在JSON映射中,我将需要循环代码,循环次数与检索到的修补程序的次数相同。我目前正计划在main函数中循环一些需要发布的JSON映射。你知道吗

现在,我只是把我得到的所有数据映射到"description""additional_info"字段。这个工作,我可以张贴的数据罚款。你知道吗

如果有人能给我举一些例子,说明如何操作从GET请求中获取的数据,这将对我有很大的帮助。你知道吗


Tags: 数据代码nameeventnodejsondatatype
1条回答
网友
1楼 · 发布于 2024-04-24 12:11:21

json.loads(ciscodata)返回字典数组。你知道吗

语句o_node = (jsonarray.get('identifier'))失败,因为它试图在jsonarray是列表时获取字典键。你知道吗

试试o_node = jsonarray[0].get('identifier')

我不知道你为什么在jsonarray附近有那些不应该在那里的人。你知道吗

一般来说,您可以执行以下操作:

for elem in jsonarray:
    identifier = elem.get('identifier')
    title = elem.get('title')
    ... etc
    do_something(identifier, title, ...)

这使得调试更容易,因为您可以测试从api调用返回的值。你知道吗

如果你的字典里总是有相同的键,你可以跳过这个任务,直接做

for elem in jsonarray:
    do_something(elem.get('key1'), elem.get('key2'), ...)`

希望有帮助

相关问题 更多 >