Skip to content

CLI Tool Usage

The OctoFHIR FHIRPath CLI tool provides a convenient way to evaluate and validate FHIRPath expressions from the command line.

First, make sure you have the CLI tool installed:

Terminal window
cargo install --path fhirpath-cli

The CLI provides two main commands:

Evaluate FHIRPath expressions against FHIR resources.

Terminal window
octofhir-fhirpath eval <EXPRESSION> <RESOURCE_FILE> [OPTIONS]
Terminal window
# Evaluate a simple expression
octofhir-fhirpath eval "Patient.name.given" patient.json
# Evaluate with pretty formatting (default)
octofhir-fhirpath eval "name.family" patient.json --format pretty
# Evaluate with JSON formatting
octofhir-fhirpath eval "name.family" patient.json --format json
# Evaluate complex expressions
octofhir-fhirpath eval "name.where(use = 'official').family" patient.json
  • --format <FORMAT>: Output format (pretty or json)
    • pretty: Human-readable format (default)
    • json: JSON format for programmatic use
Terminal window
# Patient resource
octofhir-fhirpath eval "name.given" patient.json
# Observation resource
octofhir-fhirpath eval "code.coding.system" observation.json
# Bundle resource
octofhir-fhirpath eval "entry.resource.resourceType" bundle.json
# Medication resource
octofhir-fhirpath eval "ingredient.item.display" medication.json

Check if FHIRPath expressions are syntactically valid.

Terminal window
octofhir-fhirpath validate <EXPRESSION>
Terminal window
# Valid expression
octofhir-fhirpath validate "Patient.name.family"
# Output: Expression is valid
# Invalid expression
octofhir-fhirpath validate "Patient.invalid..syntax"
# Output: Syntax error: unexpected token at position 15

Extract specific data from FHIR resources:

Terminal window
# Get patient demographics
octofhir-fhirpath eval "name.family" patient.json
octofhir-fhirpath eval "gender" patient.json
octofhir-fhirpath eval "birthDate" patient.json
# Get observation values
octofhir-fhirpath eval "valueQuantity.value" observation.json
octofhir-fhirpath eval "valueQuantity.unit" observation.json

Filter data based on conditions:

Terminal window
# Get official names only
octofhir-fhirpath eval "name.where(use = 'official')" patient.json
# Get active medications
octofhir-fhirpath eval "entry.resource.where(status = 'active')" medication-statement.json

Validate data presence and format:

Terminal window
# Check if required fields exist
octofhir-fhirpath eval "name.exists()" patient.json
octofhir-fhirpath eval "identifier.exists()" patient.json
# Validate data types
octofhir-fhirpath eval "birthDate.matches('[0-9]{4}-[0-9]{2}-[0-9]{2}')" patient.json
Terminal window
$ octofhir-fhirpath eval "name.given" patient.json
["John", "Michael"]
Terminal window
$ octofhir-fhirpath eval "name.given" patient.json --format json
["John","Michael"]

The CLI provides detailed error messages for common issues:

Terminal window
$ octofhir-fhirpath validate "Patient..name"
Error: Syntax error at position 8: unexpected token '..'
Terminal window
$ octofhir-fhirpath eval "name" nonexistent.json
Error: File not found: nonexistent.json
Terminal window
$ octofhir-fhirpath eval "name" invalid.json
Error: Invalid JSON in file: invalid.json
Line 3: unexpected character '{'
  1. Use quotes: Always wrap FHIRPath expressions in quotes to prevent shell interpretation
  2. Test expressions: Use the validate command to check syntax before evaluation
  3. Format output: Use --format json for programmatic processing
  4. Complex expressions: Break down complex expressions into simpler parts for debugging

The CLI tool can be easily integrated into shell scripts:

#!/bin/bash
# Extract patient data
FAMILY_NAME=$(octofhir-fhirpath eval "name.family" patient.json --format json | jq -r '.[0]')
GIVEN_NAME=$(octofhir-fhirpath eval "name.given[0]" patient.json --format json | jq -r '.')
echo "Patient: $GIVEN_NAME $FAMILY_NAME"