Skip to content

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
class JSONExporter:
    """Export DPP to plain JSON without JSON-LD context.

    Useful for systems that don't support JSON-LD or need
    simplified output.
    """

    def __init__(self, exclude_none: bool = True) -> None:
        """Initialize exporter.

        Args:
            exclude_none: Exclude None values from output
        """
        self.exclude_none = exclude_none

    def export(
        self,
        passport: DigitalProductPassport,
        *,
        indent: int | None = 2,
        by_alias: bool = True,
    ) -> str:
        """Export passport to JSON string.

        Args:
            passport: Validated DigitalProductPassport
            indent: JSON indentation (None for compact)
            by_alias: Use field aliases in output (e.g., camelCase)

        Returns:
            JSON formatted string
        """
        data = self.export_dict(passport, by_alias=by_alias)
        return json.dumps(data, indent=indent, ensure_ascii=False)

    def export_dict(
        self,
        passport: DigitalProductPassport,
        *,
        by_alias: bool = True,
    ) -> dict[str, Any]:
        """Export passport to dictionary.

        Args:
            passport: Validated DigitalProductPassport
            by_alias: Use field aliases in output

        Returns:
            Dictionary representation
        """
        return passport.model_dump(
            mode="json",
            by_alias=by_alias,
            exclude_none=self.exclude_none,
        )

    def export_to_file(
        self,
        passport: DigitalProductPassport,
        path: Path | str,
        *,
        indent: int | None = 2,
        by_alias: bool = True,
    ) -> None:
        """Export passport to JSON file.

        Args:
            passport: Validated DigitalProductPassport
            path: Output file path
            indent: JSON indentation
            by_alias: Use field aliases in output
        """
        content = self.export(passport, indent=indent, by_alias=by_alias)
        Path(path).write_text(content, encoding="utf-8")

__init__(exclude_none=True)

Initialize exporter.

Parameters:

Name Type Description Default
exclude_none bool

Exclude None values from output

True
Source code in src/dppvalidator/exporters/json.py
def __init__(self, exclude_none: bool = True) -> None:
    """Initialize exporter.

    Args:
        exclude_none: Exclude None values from output
    """
    self.exclude_none = exclude_none

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
def export(
    self,
    passport: DigitalProductPassport,
    *,
    indent: int | None = 2,
    by_alias: bool = True,
) -> str:
    """Export passport to JSON string.

    Args:
        passport: Validated DigitalProductPassport
        indent: JSON indentation (None for compact)
        by_alias: Use field aliases in output (e.g., camelCase)

    Returns:
        JSON formatted string
    """
    data = self.export_dict(passport, by_alias=by_alias)
    return json.dumps(data, indent=indent, ensure_ascii=False)

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
def export_dict(
    self,
    passport: DigitalProductPassport,
    *,
    by_alias: bool = True,
) -> dict[str, Any]:
    """Export passport to dictionary.

    Args:
        passport: Validated DigitalProductPassport
        by_alias: Use field aliases in output

    Returns:
        Dictionary representation
    """
    return passport.model_dump(
        mode="json",
        by_alias=by_alias,
        exclude_none=self.exclude_none,
    )

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
def export_to_file(
    self,
    passport: DigitalProductPassport,
    path: Path | str,
    *,
    indent: int | None = 2,
    by_alias: bool = True,
) -> None:
    """Export passport to JSON file.

    Args:
        passport: Validated DigitalProductPassport
        path: Output file path
        indent: JSON indentation
        by_alias: Use field aliases in output
    """
    content = self.export(passport, indent=indent, by_alias=by_alias)
    Path(path).write_text(content, encoding="utf-8")

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
class 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.
    """

    def __init__(self, version: str = DEFAULT_VERSION) -> None:
        """Initialize exporter.

        Args:
            version: Schema version for context resolution
        """
        self.version = version
        self._context_manager = ContextManager(version)

    def export(
        self,
        passport: DigitalProductPassport,
        *,
        indent: int | None = 2,
        include_type: bool = True,
    ) -> str:
        """Export passport to JSON-LD string.

        Args:
            passport: Validated DigitalProductPassport
            indent: JSON indentation (None for compact)
            include_type: Include type array in output

        Returns:
            JSON-LD formatted string
        """
        data = self._to_jsonld_dict(passport, include_type=include_type)
        return json.dumps(data, indent=indent, ensure_ascii=False)

    def export_dict(
        self,
        passport: DigitalProductPassport,
        *,
        include_type: bool = True,
    ) -> dict[str, Any]:
        """Export passport to JSON-LD dictionary.

        Args:
            passport: Validated DigitalProductPassport
            include_type: Include type array in output

        Returns:
            JSON-LD formatted dictionary
        """
        return self._to_jsonld_dict(passport, include_type=include_type)

    def export_to_file(
        self,
        passport: DigitalProductPassport,
        path: Path | str,
        *,
        indent: int | None = 2,
    ) -> None:
        """Export passport to JSON-LD file.

        Args:
            passport: Validated DigitalProductPassport
            path: Output file path
            indent: JSON indentation
        """
        content = self.export(passport, indent=indent)
        Path(path).write_text(content, encoding="utf-8")

    def _to_jsonld_dict(
        self,
        passport: DigitalProductPassport,
        *,
        include_type: bool = True,
    ) -> dict[str, Any]:
        """Convert passport to JSON-LD dictionary.

        Args:
            passport: Validated DigitalProductPassport
            include_type: Include type array

        Returns:
            JSON-LD dictionary with @context and type
        """
        data = passport.to_jsonld(
            include_context=True,
            context_urls=tuple(self._context_manager.get_context()),
        )

        if include_type and "type" not in data:
            data["type"] = self._context_manager.get_type()

        return data

__init__(version=DEFAULT_VERSION)

Initialize exporter.

Parameters:

Name Type Description Default
version str

Schema version for context resolution

DEFAULT_VERSION
Source code in src/dppvalidator/exporters/jsonld.py
def __init__(self, version: str = DEFAULT_VERSION) -> None:
    """Initialize exporter.

    Args:
        version: Schema version for context resolution
    """
    self.version = version
    self._context_manager = ContextManager(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
def export(
    self,
    passport: DigitalProductPassport,
    *,
    indent: int | None = 2,
    include_type: bool = True,
) -> str:
    """Export passport to JSON-LD string.

    Args:
        passport: Validated DigitalProductPassport
        indent: JSON indentation (None for compact)
        include_type: Include type array in output

    Returns:
        JSON-LD formatted string
    """
    data = self._to_jsonld_dict(passport, include_type=include_type)
    return json.dumps(data, indent=indent, ensure_ascii=False)

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
def export_dict(
    self,
    passport: DigitalProductPassport,
    *,
    include_type: bool = True,
) -> dict[str, Any]:
    """Export passport to JSON-LD dictionary.

    Args:
        passport: Validated DigitalProductPassport
        include_type: Include type array in output

    Returns:
        JSON-LD formatted dictionary
    """
    return self._to_jsonld_dict(passport, include_type=include_type)

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
def export_to_file(
    self,
    passport: DigitalProductPassport,
    path: Path | str,
    *,
    indent: int | None = 2,
) -> None:
    """Export passport to JSON-LD file.

    Args:
        passport: Validated DigitalProductPassport
        path: Output file path
        indent: JSON indentation
    """
    content = self.export(passport, indent=indent)
    Path(path).write_text(content, encoding="utf-8")

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:

{
  "@context": [
    "https://www.w3.org/ns/credentials/v2",
    "https://vocabulary.uncefact.org/untp/dpp/0.6.1"
  ],
  "type": ["DigitalProductPassport", "VerifiableCredential"],
  ...
}