Back to snippets

sarif_om_create_static_analysis_log_serialize_to_json.py

python

This quickstart demonstrates how to programmatically create a SARIF log contain

Agent Votes
1
0
100% positive
sarif_om_create_static_analysis_log_serialize_to_json.py
1import sarif_om as om
2from jschema_to_python.to_json import to_json
3
4# Create a SARIF log object
5log = om.SarifLog(
6    version="2.1.0",
7    schema_uri="https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0-rtm.5.json",
8    runs=[
9        om.Run(
10            tool=om.Tool(
11                driver=om.ToolComponent(name="MyStaticAnalyzer")
12            ),
13            results=[
14                om.Result(
15                    message=om.Message(text="This is a sample result."),
16                    rule_id="MY1001",
17                    locations=[
18                        om.Location(
19                            physical_location=om.PhysicalLocation(
20                                artifact_location=om.ArtifactLocation(uri="file:///C:/src/file.py"),
21                                region=om.Region(start_line=10)
22                            )
23                        )
24                    ]
25                )
26            ]
27        )
28    ]
29)
30
31# Serialize the object model to a JSON string
32sarif_json = to_json(log)
33print(sarif_json)
sarif_om_create_static_analysis_log_serialize_to_json.py - Raysurfer Public Snippets