JsonToSmartCSVA simple tool to convert JSON data into CSV records. This tool uses a set of rules to construct CSV records from a source JSON file. It can append to an existing CSV file or create a fresh file with headers.
Disclaimer: JsonToSmartCsv was developed on Mac OS, and I’ve done no testing on any other systems. Binaries for Windows and Linux ought to work. Please let me know if you encounter issues.
| Version | Notes |
|---|---|
0.1 |
Initial release. Simple rules defined in a CSV file direct the tool to craft CSV from JSON input. |
0.2 |
Switched to defining the column rules as JSON, allowing for more complex nested rules to match more complex input data. |
0.3 |
Refactor release - builds an intermediary tree before building the final table. Support for aggregation. |
0.4 |
Support for aggregation, and more complete documentation. |
0.5 |
Adds support for .jsonl json lines format data. Adds sample column definitions. |
Release binaries are available for win-x64, osx-x64, and linux-x64 systems. See:
Download the zip file from the release, and use the binary for your system.
JsonToSmartCsv is an application that reads a JSON document, and parses it into a CSV file. It’ll flatten things and repeat values where needed when iterating over arrays or object properties.
Provide a column definitions file to describe the mapping between JSON properties and columns in the output CSV data.
| Sample | Purpose |
|---|---|
resources/aws-transcribe-columns.jsonc |
Column definitions to convert a regular transcript from Amazon Transcribe from JSON to CSV. |
resources/aws-transcribe-diarised-columns.jsonc |
Column definitions to convert a diarised transcript from Amazon Transcribe from JSON to CSV. |
resources/aws-chime-media-pipeline-transcription-jsonl-columns.jsonc |
Column definitions to convert JSON lines generated by an Amazon Chime SDK Media Concatenation Pipeline to CSV. |
JsonToSmartCsv -c <column-csv-file> -s <source-json-file> -t <target-csv-file> [-m <mode>]
-c, --columns Required. Column definitions CSV file.
-s, --source Required. Source data JSON file.
-t, --target Required. Target CSV file.
-m, --mode (Default: Create) Write mode (Append or Create)
--help Display this help screen.
--version Display version information.
Create = create a new target file, backup any existing fileAppend = append to the target file (if it exists)Provide column definitions as a JSON file:
{
"root": "<string>", // topmost object to process, default: "$"
"rules": // array of rules defining columns
[
{
"path": "<string>", // relative path to the field in the current object
"target": "<string>", // name of the column in the target CSV file
"interpretation": "<string>", // how to interpret the value of the field (see below)
"children": [] // optional array of rules to apply to nested objects and lists
}
]
}
AsString - interpret this value as a stringAsNumber - interpret this value as an integer or decimal numberAsJson - convert this object or list to a JSON stringIterateListItems - apply child rules to the items in this listIteratePropertiesAsList - apply child rules to the object properties, as if a listAsIndex - item’s index (IterateListItems), or property (IteratePropertiesAsList)AsAggregateSum - aggregate and sum all numeric values from child rulesAsAggregateMax - aggregate and find the max of numeric values from child rulesAsAggregateMin - aggregate and find the min of numeric values from child rulesAsAggregateAvg - aggregate and find the mean of numeric values from child rulesAsAggregateCount - count all (non-null) values from by child rulesThe root item of a JSON document is either an object or a list.
$.property pathsIteratePropertiesAsListIterateListItems ruleSee test-sample-osx-x64.sh for a working sample. This script invokes JsonToSmartCsv with the following files:
sample-data/sample-list.json - some sample datasample-data/sample-rules.json - rules to interpret themsample-data/sample-out.csv - generated CSV outputTake a look at the sample data, and sample rules to see how they interact to generate the output CSV.
Check which binary the script uses. You may need to download a copy of the release binary, or create your own with: publish.sh
See: Developer notes