Exporters API¶
Export Digital Product Passports to various formats.
JSONExporter¶
Export passports to JSON format.
dppvalidator.exporters.JSONExporter
¶
Export DPP to plain JSON without JSON-LD context.
Useful for systems that don't support JSON-LD or need simplified output.
Source code in src/dppvalidator/exporters/json.py
__init__(exclude_none=True)
¶
Initialize exporter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exclude_none
|
bool
|
Exclude None values from output |
True
|
export(passport, *, indent=2, by_alias=True)
¶
Export passport to JSON string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
passport
|
DigitalProductPassport
|
Validated DigitalProductPassport |
required |
indent
|
int | None
|
JSON indentation (None for compact) |
2
|
by_alias
|
bool
|
Use field aliases in output (e.g., camelCase) |
True
|
Returns:
| Type | Description |
|---|---|
str
|
JSON formatted string |
Source code in src/dppvalidator/exporters/json.py
export_dict(passport, *, by_alias=True)
¶
Export passport to dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
passport
|
DigitalProductPassport
|
Validated DigitalProductPassport |
required |
by_alias
|
bool
|
Use field aliases in output |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary representation |
Source code in src/dppvalidator/exporters/json.py
export_to_file(passport, path, *, indent=2, by_alias=True)
¶
Export passport to JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
passport
|
DigitalProductPassport
|
Validated DigitalProductPassport |
required |
path
|
Path | str
|
Output file path |
required |
indent
|
int | None
|
JSON indentation |
2
|
by_alias
|
bool
|
Use field aliases in output |
True
|
Source code in src/dppvalidator/exporters/json.py
options: show_source: false
JSONLDExporter¶
Export passports to JSON-LD format with W3C VC context.
dppvalidator.exporters.JSONLDExporter
¶
Export DPP to JSON-LD with W3C VC v2 compliance.
Produces valid JSON-LD output conforming to UNTP DPP specification and W3C Verifiable Credentials v2.
Source code in src/dppvalidator/exporters/jsonld.py
__init__(version=DEFAULT_VERSION)
¶
Initialize exporter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
version
|
str
|
Schema version for context resolution |
DEFAULT_VERSION
|
export(passport, *, indent=2, include_type=True)
¶
Export passport to JSON-LD string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
passport
|
DigitalProductPassport
|
Validated DigitalProductPassport |
required |
indent
|
int | None
|
JSON indentation (None for compact) |
2
|
include_type
|
bool
|
Include type array in output |
True
|
Returns:
| Type | Description |
|---|---|
str
|
JSON-LD formatted string |
Source code in src/dppvalidator/exporters/jsonld.py
export_dict(passport, *, include_type=True)
¶
Export passport to JSON-LD dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
passport
|
DigitalProductPassport
|
Validated DigitalProductPassport |
required |
include_type
|
bool
|
Include type array in output |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
JSON-LD formatted dictionary |
Source code in src/dppvalidator/exporters/jsonld.py
export_to_file(passport, path, *, indent=2)
¶
Export passport to JSON-LD file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
passport
|
DigitalProductPassport
|
Validated DigitalProductPassport |
required |
path
|
Path | str
|
Output file path |
required |
indent
|
int | None
|
JSON indentation |
2
|
Source code in src/dppvalidator/exporters/jsonld.py
options: show_source: false
Usage Example¶
from dppvalidator.exporters import JSONExporter, JSONLDExporter
from dppvalidator.models import DigitalProductPassport, CredentialIssuer
# Create a passport
passport = DigitalProductPassport(
id="https://example.com/dpp/001",
issuer=CredentialIssuer(id="https://example.com/issuer", name="Acme Corp"),
)
# Export to JSON
json_exporter = JSONExporter()
json_output = json_exporter.export(passport)
print(json_output)
# Export to JSON-LD
jsonld_exporter = JSONLDExporter()
jsonld_output = jsonld_exporter.export(passport)
print(jsonld_output)
JSON-LD Context¶
The JSON-LD exporter adds W3C Verifiable Credentials context: