|
| 1 | +# 🩺 FHIR to Flat JSON Conversion Engine |
| 2 | + |
| 3 | +This project is designed to convert FHIR-compliant JSON data (e.g., Immunization records) into a flat JSON format based on a configurable schema layout. It is intended to support synchronization of Immunisation API generated data from external sources to DPS (Data Processing System) data system |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## 📁 File Structure Overview |
| 8 | + |
| 9 | +| File Name | What It Does | |
| 10 | +|------------------------|---------------| |
| 11 | +| **`converter.py`** | 🧠 The main brain — applies the schema, runs conversions, handles errors. | |
| 12 | +| **`FHIRParser.py`** | 🪜 Knows how to dig into nested FHIR structures and pull out values like dates, IDs, and patient names. | |
| 13 | +| **`SchemaParser.py`** | 📐 Reads your schema layout and tells the converter which FHIR fields to extract and how to rename/format them. | |
| 14 | +| **`ConversionLayout.py`** | ✍️ A plain Python list that defines which fields you want, and how they should be formatted (e.g. date format, renaming rules). | |
| 15 | +| **`ConversionChecker.py`** | 🔧 Handles transformation logic — e.g. turning a FHIR datetime into `YYYY-MM-DD`, applying lookups, gender codes, defaults, etc. | |
| 16 | +| **`Extractor.py`** | 🎣 Specialized logic to pull practitioner names, site codes, addresses, and apply time-aware rules. | |
| 17 | +| **`ExceptionMessages.py`** | 🚨 Holds reusable error messages and codes for clean debugging and validation feedback. | |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | + |
| 22 | +## 🛠️ Key Features |
| 23 | + |
| 24 | +- Schema-driven field extraction and formatting |
| 25 | +- Support for custom date formats like `YYYYMMDD`, and CSV-safe UTC timestamps |
| 26 | +- Robust handling of patient, practitioner, and address data using time-aware logic |
| 27 | +- Extendable structure with static helper methods and modular architecture |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | +## 📦 Example Use Case |
| 32 | + |
| 33 | +- Input: FHIR `Immunization` resource (with nested fields) |
| 34 | +- Output: Flat JSON object with 34 standardized key-value pairs |
| 35 | +- Purpose: To export into CSV or push into downstream ETL systems |
| 36 | + |
| 37 | +--- |
| 38 | + |
| 39 | +## ✅ Getting Started with `check_conversion.py` |
| 40 | + |
| 41 | +To quickly test your conversion, use the provided `check_conversion.py` script. |
| 42 | +This script loads sample FHIR data, runs it through the converter, and automatically saves the output in both JSON and CSV formats. |
| 43 | + |
| 44 | +### 🔄 How to Use It |
| 45 | + |
| 46 | +1. Add your FHIR data (e.g., a dictionary or sample JSON) into the `fhir_sample` variable inside `check_conversion.py` |
| 47 | +2. Ensure the field mapping in `ConversionLayout.py` matches your desired output |
| 48 | +3. Run the script from the `tests` folder: |
| 49 | + |
| 50 | +```bash |
| 51 | +python check_conversion.py |
| 52 | +``` |
| 53 | + |
| 54 | +### 📁 Output Location |
| 55 | +When the script runs, it will automatically: |
| 56 | +- Save a **flat JSON file** as `output.json` |
| 57 | +- Save a **CSV file** as `output.csv` |
| 58 | + |
| 59 | +These will be located one level up from the `src/` folder: |
| 60 | + |
| 61 | +``` |
| 62 | +/mnt/c/Users/USER/desktop/shn/immunisation-fhir-api/delta_backend/output.json |
| 63 | +/mnt/c/Users/USER/desktop/shn/immunisation-fhir-api/delta_backend/output.csv |
| 64 | +``` |
| 65 | + |
| 66 | +### 👀 Visualization |
| 67 | +You can now: |
| 68 | +- Open `output.csv` in Excel or Google Sheets to view cleanly structured records |
| 69 | +- Inspect `output.json` to validate the flat key-value output programmatically |
| 70 | + |
| 71 | +--- |
0 commit comments