{"library":"cyclonedx-python-lib","code":"from cyclonedx.model.bom import Bom\nfrom cyclonedx.model.component import Component\nfrom cyclonedx.model.dependency import Dependency\nfrom packageurl.contrib.url2purl import url2purl\nfrom cyclonedx.output import JsonV15\n\n# 1. Create a new BOM\nbom = Bom()\n\n# 2. Define components\ncomponent_a = Component(name='my-app', version='1.0.0')\ncomponent_a.bom_ref.value = 'pkg-a-1.0.0'\n\ncomponent_b_purl = url2purl('pkg:pypi/requests@2.28.1')\ncomponent_b = Component(name='requests', version='2.28.1', purl=component_b_purl)\ncomponent_b.bom_ref.value = 'pkg-b-2.28.1'\n\n# 3. Add components to the BOM\nbom.add_component(component_a)\nbom.add_component(component_b)\n\n# 4. Add a dependency relationship (optional)\ndep_a_to_b = Dependency(ref=component_a.bom_ref)\ndep_a_to_b.add_dependency(component_b.bom_ref)\nbom.add_dependency(dep_a_to_b)\n\n# 5. Serialize the BOM to JSON (using CycloneDX Schema Version 1.5)\noutputter = JsonV15(bom)\njson_output = outputter.output_as_string(indent=2)\n\nprint(json_output)\n\n# Example of deserialization (requires `validation` extra)\n# from cyclonedx.validation.schema import SchemaVersion\n# from cyclonedx.parsers.json.parser import JsonParser\n# parsed_bom = JsonParser(json_output).parse(SchemaVersion.V1_5)\n# print(f\"Parsed BOM version: {parsed_bom.get_spec_version().to_string()}\")","lang":"python","description":"This quickstart demonstrates how to programmatically create a simple CycloneDX SBOM with two components and a dependency relationship, then serialize it to JSON using Schema Version 1.5. It also includes commented-out code for deserialization as an example.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":1},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":1},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":1},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":1},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":1}]}