CLI Tool Usage
The OctoFHIR FHIRPath CLI tool provides a convenient way to evaluate and validate FHIRPath expressions from the command line.
Installation
Section titled “Installation”First, make sure you have the CLI tool installed:
cargo install --path fhirpath-cli
Commands
Section titled “Commands”The CLI provides two main commands:
eval
- Evaluate FHIRPath Expressions
Section titled “eval - Evaluate FHIRPath Expressions”Evaluate FHIRPath expressions against FHIR resources.
Basic Syntax
Section titled “Basic Syntax”octofhir-fhirpath eval <EXPRESSION> <RESOURCE_FILE> [OPTIONS]
Examples
Section titled “Examples”# Evaluate a simple expressionoctofhir-fhirpath eval "Patient.name.given" patient.json
# Evaluate with pretty formatting (default)octofhir-fhirpath eval "name.family" patient.json --format pretty
# Evaluate with JSON formattingoctofhir-fhirpath eval "name.family" patient.json --format json
# Evaluate complex expressionsoctofhir-fhirpath eval "name.where(use = 'official').family" patient.json
Options
Section titled “Options”--format <FORMAT>
: Output format (pretty
orjson
)pretty
: Human-readable format (default)json
: JSON format for programmatic use
Working with Different Resource Types
Section titled “Working with Different Resource Types”# Patient resourceoctofhir-fhirpath eval "name.given" patient.json
# Observation resourceoctofhir-fhirpath eval "code.coding.system" observation.json
# Bundle resourceoctofhir-fhirpath eval "entry.resource.resourceType" bundle.json
# Medication resourceoctofhir-fhirpath eval "ingredient.item.display" medication.json
validate
- Validate FHIRPath Expressions
Section titled “validate - Validate FHIRPath Expressions”Check if FHIRPath expressions are syntactically valid.
Basic Syntax
Section titled “Basic Syntax”octofhir-fhirpath validate <EXPRESSION>
Examples
Section titled “Examples”# Valid expressionoctofhir-fhirpath validate "Patient.name.family"# Output: Expression is valid
# Invalid expressionoctofhir-fhirpath validate "Patient.invalid..syntax"# Output: Syntax error: unexpected token at position 15
Common Use Cases
Section titled “Common Use Cases”Data Extraction
Section titled “Data Extraction”Extract specific data from FHIR resources:
# Get patient demographicsoctofhir-fhirpath eval "name.family" patient.jsonoctofhir-fhirpath eval "gender" patient.jsonoctofhir-fhirpath eval "birthDate" patient.json
# Get observation valuesoctofhir-fhirpath eval "valueQuantity.value" observation.jsonoctofhir-fhirpath eval "valueQuantity.unit" observation.json
Data Filtering
Section titled “Data Filtering”Filter data based on conditions:
# Get official names onlyoctofhir-fhirpath eval "name.where(use = 'official')" patient.json
# Get active medicationsoctofhir-fhirpath eval "entry.resource.where(status = 'active')" medication-statement.json
Data Validation
Section titled “Data Validation”Validate data presence and format:
# Check if required fields existoctofhir-fhirpath eval "name.exists()" patient.jsonoctofhir-fhirpath eval "identifier.exists()" patient.json
# Validate data typesoctofhir-fhirpath eval "birthDate.matches('[0-9]{4}-[0-9]{2}-[0-9]{2}')" patient.json
Output Examples
Section titled “Output Examples”Pretty Format (Default)
Section titled “Pretty Format (Default)”$ octofhir-fhirpath eval "name.given" patient.json["John", "Michael"]
JSON Format
Section titled “JSON Format”$ octofhir-fhirpath eval "name.given" patient.json --format json["John","Michael"]
Error Handling
Section titled “Error Handling”The CLI provides detailed error messages for common issues:
Syntax Errors
Section titled “Syntax Errors”$ octofhir-fhirpath validate "Patient..name"Error: Syntax error at position 8: unexpected token '..'
File Not Found
Section titled “File Not Found”$ octofhir-fhirpath eval "name" nonexistent.jsonError: File not found: nonexistent.json
Invalid JSON
Section titled “Invalid JSON”$ octofhir-fhirpath eval "name" invalid.jsonError: Invalid JSON in file: invalid.jsonLine 3: unexpected character '{'
Tips and Best Practices
Section titled “Tips and Best Practices”- Use quotes: Always wrap FHIRPath expressions in quotes to prevent shell interpretation
- Test expressions: Use the
validate
command to check syntax before evaluation - Format output: Use
--format json
for programmatic processing - Complex expressions: Break down complex expressions into simpler parts for debugging
Integration with Scripts
Section titled “Integration with Scripts”The CLI tool can be easily integrated into shell scripts:
#!/bin/bash
# Extract patient dataFAMILY_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"
Next Steps
Section titled “Next Steps”- Explore more usage examples
- Learn about Rust library usage
- Check out Node.js integration