Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code errors
#1
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Are there errors in the code for my coin toss systems? Matlibplot is too perfect . Coolkat 0 377 Nov-13-2023, 11:54 AM
Last Post: Coolkat
  My code won't say Player wins or computer wins. No errors. its_a_meLuigi64 2 1,639 Dec-01-2021, 04:43 PM
Last Post: its_a_meLuigi64
  Rmarkdown opened by python code - errors Rav013 0 2,094 Apr-27-2021, 03:13 PM
Last Post: Rav013
  [split] Kera Getting errors when following code example Image classification from scratch hobbyist 3 4,639 Apr-13-2021, 01:26 PM
Last Post: amirian
  2 errors in code numbnumb54 2 2,029 Jul-24-2020, 02:28 PM
Last Post: Marbelous
  Errors in code coolcassie 2 2,129 Dec-03-2019, 12:10 AM
Last Post: coolcassie
  Errors trying to run code ziggyztz 3 3,673 Nov-03-2019, 04:24 AM
Last Post: newbieAuggie2019
  errors in code sylvie1987100 3 2,317 Sep-11-2019, 12:54 PM
Last Post: sylvie1987100
  Decorator toy code throws syntax errors kevinxhi 3 3,578 Sep-04-2017, 03:01 AM
Last Post: kevinxhi

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020