A Java application that converts multi-level JSON files to Excel format, handling nested objects and arrays by flattening them into a tabular structure.
- Multi-level JSON Support: Handles nested objects and arrays of any depth
- Automatic Flattening: Converts nested structures into flat columns using dot notation (e.g.,
address.city) - Array Handling: Arrays are indexed in column names (e.g.,
skills[0],skills[1]) - Formatted Output: Creates Excel files with styled headers and borders
- Auto-sized Columns: Automatically adjusts column widths for better readability
- Java 11 or higher
- Maven 3.6 or higher
java/
├── pom.xml
├── README.md
└── src/
└── main/
├── java/
│ └── com/
│ └── jsontoexcel/
│ └── JsonToExcelConverter.java
└── resources/
└── sample.json
mvn clean packageThis will create a JAR file in the target/ directory.
java -jar target/json-to-excel-1.0.0.jar <input.json> <output.xlsx>Example:
java -jar target/json-to-excel-1.0.0.jar src/main/resources/sample.json output.xlsximport com.jsontoexcel.JsonToExcelConverter;
// Convert from JSON file
JsonToExcelConverter converter = new JsonToExcelConverter();
converter.convertJsonToExcel("input.json", "output.xlsx");
converter.close();
// Convert from JSON string
String jsonString = "{\"name\":\"John\",\"age\":30}";
converter.convertJsonStringToExcel(jsonString, "output.xlsx");
converter.close();- JSON Parsing: Uses Jackson to parse JSON into a tree structure
- Flattening: Recursively flattens nested objects and arrays:
- Objects:
user.address.citybecomes a column - Arrays:
skills[0],skills[1]become separate columns - Nested arrays:
projects[0].name,projects[0].statusbecome columns
- Objects:
- Excel Creation: Uses Apache POI to create styled Excel worksheets with:
- Bold headers with gray background
- Borders around all cells
- Auto-sized columns
- Data Mapping: Ensures all rows have consistent columns, filling missing values with empty strings
The included sample.json demonstrates:
- Top-level objects
- Nested objects (address)
- Arrays of primitives (skills)
- Arrays of objects (employees, projects)
The Excel output will have columns like:
companyemployees[0].idemployees[0].nameemployees[0].address.streetemployees[0].address.cityemployees[0].skills[0]employees[0].projects[0].namedepartments[0].name- etc.
- Apache POI 5.2.5: Excel file creation and manipulation
- Jackson 2.15.2: JSON parsing and processing
This project is provided as-is for educational and development purposes.
Build image:
docker build -t json2excel:latest .Run with mounted data directory (Windows PowerShell example):
docker run --rm -v ${PWD}/data:/data json2excel:latest /data/input.json /data/output.xlsxNotes:
- The container's entrypoint is
java -jar /app/json-to-excel.jar, so you only pass the two file paths as args. - Ensure
dataexists and containsinput.json.
This app is best run as a Job (batch) that reads a JSON file and writes an Excel file.
- Build and load the image to a registry or your cluster:
# Option A: Kind/minikube local load (example: minikube)
minikube image build -t json2excel:latest .
# Option B: Push to a registry
docker build -t <registry>/json2excel:latest .
docker push <registry>/json2excel:latest
# then update k8s/job.yaml image field- Apply manifests:
kubectl apply -f k8s/job.yaml- Get logs and retrieve the output:
kubectl logs job/json2excel-job
kubectl get pods -l job-name=json2excel-job
POD=$(kubectl get pods -l job-name=json2excel-job -o jsonpath='{.items[0].metadata.name}')
kubectl cp $POD:/data/output.xlsx ./output.xlsxCustomize:
- Edit
k8s/job.yamlConfigMap to changeinput.json. - Replace the
emptyDirvolume with aPersistentVolumeClaimif you want the output to persist independent of the pod lifecycle.