Skip to content

Instantly share code, notes, and snippets.

@aurbano
Created December 9, 2024 17:37
Show Gist options
  • Save aurbano/008e55ce8c69fc00c84a48a00d66ac2b to your computer and use it in GitHub Desktop.
Save aurbano/008e55ce8c69fc00c84a48a00d66ac2b to your computer and use it in GitHub Desktop.
Threat Dragon JSON to CSV
import json
import csv
input_file = "threat-model_threat-dragon.json"
output_file = "threat_model.csv"
with open(input_file, "r") as f:
data = json.load(f)
with open(output_file, "w", newline="") as csvfile:
csvwriter = csv.writer(csvfile)
header = ["Threat ID", "Component", "Type", "Title", "Description", "Severity", "Mitigation", "Status"]
csvwriter.writerow(header)
cells = data["detail"]["diagrams"][0]["cells"]
threats = data.get("threats", [])
for cell in cells:
if "data" not in cell or "threats" not in cell["data"]:
continue
for threat in cell["data"]["threats"]:
threat_id = f"TM-{threat['number']}"
component = cell["data"]["name"]
sectype = threat.get("type", "")
title = threat.get("title", "")
description = threat.get("description", "")
severity = threat.get("severity", "")
mitigation = threat.get("mitigation", "")
status = threat.get("status", "")
csvwriter.writerow([threat_id, component, sectype, title, description, severity, mitigation, status])
print(f"Threat model converted to {output_file}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment