Serialization

All models read and write FHIR JSON out of the box. XML and YAML are available with the xml and yaml extras (see Installation).

Format

Read

Write

Python dict

Model.model_validate(data)

obj.model_dump()

JSON

Model.model_validate_json(str | bytes)

obj.model_dump_json() → str

XML

Model.model_validate_xml(str | bytes)

obj.model_dump_xml() → bytes

YAML

Model.model_validate_yaml(str | bytes)

obj.model_dump_yaml() → str

What every serializer does

  • Writes elements in the order defined by the FHIR specification.

  • Puts resourceType first.

  • Leaves out None values and empty lists.

  • Uses the FHIR element names (class, not class_fhir) and writes primitive extensions as _element.

Every model_dump* method accepts the same extra keyword arguments:

summary_only

Only include elements flagged as _summary in the specification. See Summary mode and comments.

exclude_comments

Strip fhir_comments. See Summary mode and comments.

Other Pydantic keyword arguments (include, exclude …) are passed through.

JSON

>>> from fhir.resources.patient import Patient
>>> patient = Patient(
...     active=True, gender="male", birthDate="2000-09-18",
...     name=[{"text": "Primal Kons", "family": "Kons", "given": ["Primal"]}],
... )
>>> patient.model_dump_json()
'{"resourceType":"Patient","active":true,"name":[{"text":"Primal Kons","family":"Kons","given":["Primal"]}],"gender":"male","birthDate":"2000-09-18"}'

Pass indent for pretty output. model_dump() returns Python objects (date, Decimal …); use model_dump(mode="json") for a JSON-compatible dict:

>>> patient.model_dump(mode="json")["birthDate"]
'2000-09-18'

XML

>>> xml = patient.model_dump_xml(pretty_print=True)
>>> print(xml.decode())
<?xml version='1.0' encoding='utf-8'?>
<Patient xmlns="http://hl7.org/fhir">
  <active value="true"/>
  <name>
    <text value="Primal Kons"/>
    <family value="Kons"/>
    <given value="Primal"/>
  </name>
  <gender value="male"/>
  <birthDate value="2000-09-18"/>
</Patient>

Pass xml_declaration=False to leave out the <?xml …?> line.

Reading XML gives the same object as reading the equivalent JSON:

>>> Patient.model_validate_xml(xml) == patient
True

Validating against the FHIR XML schema

You can provide your own lxml parser, for example one bound to the official FHIR XSD files (download them from the FHIR downloads page):

import lxml.etree
from fhir.resources.patient import Patient

schema = lxml.etree.XMLSchema(file="fhir-all-xsd/patient.xsd")
parser = lxml.etree.XMLParser(schema=schema)

patient = Patient.model_validate_xml(xml_bytes, xmlparser=parser)

The test suite uses this to check generated XML against the R4B patient.xsd and observation.xsd schemas. We still recommend that you check output for your own production data, especially with comments.

YAML

FHIR does not define a YAML format, so this is an experimental convenience feature. It maps the JSON structure to YAML one-to-one.

>>> print(patient.model_dump_yaml())
resourceType: Patient
active: true
name:
- text: Primal Kons
  family: Kons
  given:
  - Primal
gender: male
birthDate: 2000-09-18

>>> Patient.model_validate_yaml(patient.model_dump_yaml()) == patient
True

Note

YAML comments are not supported; use JSON-style fhir_comments. PyYAML is used for parsing; suggestions for a faster backend are welcome.

Files

There is no parse_file in Pydantic V2. Use pathlib:

import pathlib
from fhir.resources.patient import Patient

patient = Patient.model_validate_json(pathlib.Path("patient.json").read_bytes())
pathlib.Path("patient.xml").write_bytes(patient.model_dump_xml(pretty_print=True))