Yet another command-line tool for converting JSON strings into Golang struct definitions. It’s designed to be flexible, allowing for JSON input from both files and pipes, and it handles pointer types for nested structures when specified.
- Supports both inline and outline Golang structs definitions
- Support options that change the interpretation of json
null
- Receives JSON strings directly or from a file
- Outputs formatted Golang structs use
gofmt
- Option to define struct and array fields as pointers
- Flexible with file extensions (no need for
.json
) - Option to disable the pager for larger outputs
To install kirke
, clone the repository and build the binary:
git clone https://github.com/magicdrive/kirke.git
cd kirke
make install
Alternatively, use go install
:
go install github.com/magicdrive/kirke@latest
curl -s https://api.github.com/repos/magicdrive/kirke | kirke
git clone https://github.com/magicdrive/kirke.git
cd kirke
source ./misc/completion/kirke-completion.sh
kirke [OPTIONS] [ARGUMENTS]
Receives a JSON string and outputs it as a Golang struct definition.
Option | Description | Default |
---|---|---|
-h , --help |
Show this help message and exit | |
-v , --version |
Show version and exit | |
-n , --name <root-name> |
Specify the name of the root struct. Converts to camel case automatically. | AutoGenerated |
-f , --file <json-file> |
Specify the input file containing JSON. Accepts files without .json extension. |
|
-a , --null-as <null-type-name> |
Specify the null-type name. Used to replace null type from json. |
interface{} |
-j , --json <json-string> |
Specify the JSON string to be converted to a struct. | |
-p , --pipe |
Receive JSON from a pipe instead of a direct argument above all else. | false |
--pointer-struct , --pointer <on|off> |
Define struct and array fields as pointers. | off |
--auto-pager , --pager <on|off> |
Prevents usage of a pager even if output exceeds terminal size. | on |
-o , --outline |
Defines struct and array fields as outline struct. (default: true). | true |
-i , --inline |
Defines struct and array fields as inline struct. (default: false) | false |
<root-name>
: Used as the name of the root struct in the output. Automatically converted to camel case.<json-file>
: File containing JSON data. Can be read even if the extension is not.json
.<json-string>
: Direct JSON string input.<null-type-name>
: The type name used to replacenull
type from json.
Environment | Description | Default |
---|---|---|
KIRKE_DEFAULT_ROOT_NAME |
Specified default used <root-name> . |
AutoGenerated |
KIRKE_DEFAULT_NULL_AS |
Specified default used <null-type-name> . |
interface{} |
KIRKE_DEFAULT_OUTPUT_MODE |
Specified the default used output mode. Only outline and inline are valid. |
outline |
KIRKE_DEFAULT_POINTER_STRUCT_MODE |
Specified default <pointer-struct> option. Only on and off are valid.(default: "off") |
off |
KIRKE_DEFAULT_AUTO_PAGER_MODE |
Specified default <auto-pager> option. Only on and off are valid.(default: "on") |
on |
Convert a JSON string to a struct without using a pager and with a specified root name:
kirke -j '{"key": "value"}' --pager off --name MyStruct
Convert JSON data from a specified file to a struct with a specified root name:
kirke -f ./path/to/example.json --name MyExample
Read JSON data from a pipe, use pointers for nested fields, and output as a struct:
echo '{"key": "value"}' | kirke --pointer on
Read JSON data from a pipe, use pointers for nested fields, and output as a struct (-j option is ignored.):
echo '{"key": "value"}' | kirke --pipe --pointer on -j '{"key2": "value2"}'
Read JSON data from a pipe, use pointers for nested fields, and output as a struct (-j option is ignored.):
echo '{"key": "value", "obj": {"address": null} }' | kirke -a "*string"