Python Forum

Full Version: Code errors
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have the following Python Script. This is to fetch data from a JSON file.
This JSON file is fetched from Sentinel using API Calls (URL) and APID-using postman.
The JSON file I get has much more data than I needed. I want to fetch a few headers of JSON data and the DATA under those headers.
Next, I want to map them to different set of headers (Mapping ) and fetch the new file as a CSV file.
Here is the script. But it gives errors.

import requests
import json
import csv

csv_columns = ['Application ID', 'Vulnerability ID', 'Vulnerability Name', 'Severity', 'Category', 'Status', 'Date Found', 'Tester', 'Description']

with open('C:\Users\jthomas\Documents/H1_findings.csv', 'w') as csvfile:
    writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
    writer.writeheader()

    headers = {
    'Accept': 'application/json'
    }

    for i in range(1, 15):
        r = requests.get(
        'https://sentinel.whitehatsec.com/api/vuln?query_status=open%2C%20closed%2C%20accepted&query_asset_status=active&display_vulnerability=1&display_description=1&display_solution=custom&display_default_solution=1&display_cvss=1&display_risk=1&display_first_opened=1&display_custom_risk=1&display_custom_policy=0&display_text=0&display_attack_vectors=open%2C%20closed%2C%20accepted&display_attack_vectors_limit=1&display_attack_vector_notes=1&display_body=0&display_abbr=1&display_headers=0&display_param=0&display_has_notes=0&display_stats=0&display_errors=0&page%3Aorder_by=id&format=xml',
        auth=('APID', 'aCuSCxtedT89vwrvNW24nXSprOSCR2vEDRs8HVXNrrp79NveTl7jxh8FFUZZ8zlciLRF8Vgv1YSsELa%2B60irOR9AdrX35rCRBO3sDqWDVTmGgJsRuKEeYim1UFWYwlKM'),
        params={''},
        headers = headers
        )

        print("page number: ++++++++++++++++++++", i)

        json_parsed = json.loads(r.text)
        if len(json_parsed['data']) < 1:
            break


        for finding in json_parsed['data']:

            vulnerability_name = finding['attributes']['Vulnerability Name'] #Description
        Application_ID= finding['attributes']['vulnerability/@site']
        Vulnerability_ID= finding['attributes']['vulnerability/@id']
            Category = finding['attributes']['vulnerability/@class']
            Severity = finding['attributes']['vulnerability/@Severity']
            Status = finding['attributes']['vulnerability/@status']
            date_found = finding['attributes']['vulnerability/@opened']
            tester = finding['relationships']['reporter']['data']['attributes'][Jomi]
            description = finding['attributes']['vulnerability/description']


            #write rows to CSV
            #writer.writerow({'Application ID' :  application_id, 'Vulnerability ID' : vulnerability_id, 'Vulnerability Name' :  vulnerability_name, 'Category' : category, 'Severity' : severity, 'Status' : status, 'Date Found' : date_found, 'Tester' : tester, 'Description' : description })
            #removed description as it was breaking CSV
            writer.writerow({'Application ID' :  application_id, 'Vulnerability ID' : vulnerability_id, 'Vulnerability Name' :  vulnerability_name, 'Category' : category, 'Severity' : severity, 'Status' : status, 'Date Found' : date_found, 'Tester' : tester })
Can somebody help me to correct this and get the final CSV file with data in it.
If API call is the problem, Its alright to have a script to fetch and map data from JSON to CSV also is fine.

Please help.
Thank you