Home > Software design >  Python json library only retrieve root element
Python json library only retrieve root element

Time:02-19

I am having a hard time to retrieve json data from a long text using json library. Data is retrieved from cisco bug search tool api via curl (text.txt).

My code only recognize the the root element. Sub elements are not retrieved.

I am not sure what I am missing.

Code:

with open('text.txt','r', encoding='utf8', errors='ignore') as file:
  filecontent = file.readlines()
for line in filecontent:
  if re.search("^\{\"advisories\"(.*?)\}", line):
  y=json.loads(line)
  for key, value in y.items():
    print("Key:" key)

Output

root@server#python3 security.py
Key:advisories
root@server#

text.txt

{"advisories":[{"advisoryId":"cisco-sa-fxo-pattern-bypass-jUXgygYv","advisoryTitle":"Cisco IOS and IOS XE Software FXO Interface Destination Pattern Bypass Vulnerability","bugIDs":["CSCvw53542"],"ipsSignatures":["NA"],"cves":["CVE-2021-34705"],"cvrfUrl":"https://tools.cisco.com/security/center/contentxml/CiscoSecurityAdvisory/cisco-sa-fxo-pattern-bypass-jUXgygYv/cvrf/cisco-sa-fxo-pattern-bypass-jUXgygYv_cvrf.xml","cvssBaseScore":"5.3","cwe":["CWE-232"],"iosRelease":["15.1(4)M4"],"firstFixed":["15.7(3)M8"],"firstPublished":"2021-09-22T16:00:00","lastUpdated":"2021-09-22T16:00:00","status":"Final","version":"1.0","productNames":["Cisco IOS ","Cisco IOS XE Software ","Cisco IOS 12.3(9a)","Cisco IOS 12.3(15)",(...),"Cisco IOS XE Software 17.3.1w","Cisco IOS XE Software 17.3.2a","Cisco IOS XE Software 17.3.1x","Cisco IOS XE Software 17.3.1z","Cisco IOS XE Software 17.4.1","Cisco IOS XE Software 17.4.1a","Cisco IOS XE Software 17.4.1b","Cisco IOS XE Software 17.4.1c","Cisco IOS XE Software 17.6.1w"],"publicationUrl":"https://tools.cisco.com/security/center/content/CiscoSecurityAdvisory/cisco-sa-fxo-pattern-bypass-jUXgygYv","sir":"High","summary":"\n<p>A vulnerability in the Voice Telephony Service Provider (VTSP) service of Cisco&nbsp;IOS Software and Cisco&nbsp;IOS XE Software could allow an unauthenticated, remote attacker to bypass configured destination patterns and dial arbitrary numbers.</p>\n<p>This vulnerability is due to insufficient validation of dial strings at Foreign Exchange Office (FXO) interfaces. An attacker could exploit this vulnerability by sending a malformed dial string to an affected device via either the ISDN protocol or SIP. A successful exploit could allow the attacker to conduct toll fraud, resulting in unexpected financial impact to affected customers.</p>\n<p>Cisco&nbsp;has released software updates that address this vulnerability. There are no workarounds that address this vulnerability.</p>\n<p>This advisory is available at the following link:<br><a href=\"https://tools.cisco.com/security/center/content/CiscoSecurityAdvisory/cisco-sa-fxo-pattern-bypass-jUXgygYv\" target=\"_blank\">https://tools.cisco.com/security/center/content/CiscoSecurityAdvisory/cisco-sa-fxo-pattern-bypass-jUXgygYv</a></p>\n\n<p>This advisory is part of the September 2021 release of the Cisco&nbsp;IOS and IOS XE Software Security Advisory Bundled Publication. For a complete list of the advisories and links to them, see <a href=\"https://tools.cisco.com/security/center/viewErp.x?alertId=ERP-74581\">Cisco&nbsp;Event Response: September 2021 Semiannual Cisco&nbsp;IOS and IOS XE Software Security Advisory Bundled Publication.</a></p>\n\n"},{"advisoryId":"cisco-sa-info-disclosure-V4BmJBNF",etc (it repeats)

Any help is appreciated. Thank you

CodePudding user response:

It seems like you want to use these advisories within Python, or maybe reformat and print them out.

The most important thing to understand is that json.load will do all the work for you here, so you don't have to use re or readlines.

Here's an example:

with open('test.txt', errors='ignore') as file:
    data = json.load(file)

for advisory in data['advisories']:
    print('advisoryId = '   repr(advisory['advisoryId']))

CodePudding user response:

first of all, you need to validate json syntax

  1. go http://json.parser.online.fr/
  2. paste your text.txt file and see there is no error

and then

with open('text.txt','r', encoding='utf8', errors='ignore') as file:
    data = json.load(file)

for advisories in data['advisories']:
    keyList =  advisories.keys()
    for key in keyList:
        print(key,'::', advisories[key])

result is

advisoryId :: cisco-sa-fxo-pattern-bypass-jUXgygYv

advisoryTitle :: Cisco IOS and IOS XE Software FXO Interface Destination

bugIDs :: ['CSCvw53542']

ipsSignatures :: ['NA']

......

  • Related