README
JSON to Go
Convert JSON to Go struct from CLI using JTG.
Installation
$ npm i -g @fatihege/jtg
Usage
You can type jtg
and optionally use the -h
flag to get help.
$ jtg
:======================: JSON TO GO :======================:
-h --help Learn about how to use JTG.
-v --version See the current JTG version.
-n --normal [json] Convert JSON to Go from terminal.
-f --file [path] [from?] [to?] Convert JSON to Go from a file.
Type jtg -v
to see the version of JTG you are using.
$ jtg -v
:====: JTG-v1.0.0 :====:
______________
/ /_ __/ ____/
__ / / / / / / __
/ /_/ / / / / /_/ /
\____/ /_/ \____/
Type the command jtg -n [json]
to convert JSON to Go in the terminal. In the current version you need to type the \
character before the double quotes, but this will be fixed in future versions.
$ jtg -n {\"lorem\": \"ipsum\"}
type StructName struct {
Lorem string `json:"lorem"`
}
If you want to convert the contents of any file to Go struct, you have to use the -f
flag.
test.json
{
"lorem": "ipsum",
"foo": {
"bar": 0
}
}
$ jtg -f ./test.json
type StructName struct {
Lorem string `json:"lorem"`
Foo Foo `json:"foo"`
}
type Foo struct {
Bar int `json:"bar"`
}
If you want to convert certain lines in a file to the Go struct, specify the file path in the -f
flag and then write
the [from] [to]
arguments.
$ jtg -f ./test.json 3 5
type StructName struct {
Bar int `json:"bar"`
}
Note: JTG ignores the "foo":
part at the very beginning of the 3rd line.
If you are converting from a file, the file does not have to be a JSON file. It is sufficient to have a JSON structure in any file.
test.txt
Cras ultricies ligula sed magna dictum porta. Quisque velit nisi, pretium ut lacinia in, elementum id enim.
Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec velit neque, auctor sit
amet aliquam vel, ullamcorper sit amet ligula. Pellentesque in ipsum id orci porta dapibus. Donec sollicitudin
molestie malesuada.
{
"foo": "bar"
}
Curabitur aliquet quam id dui posuere blandit. Vivamus magna justo, lacinia eget consectetur sed, convallis at tellus.
Lines 6 and 8 in the test.txt file conform to JSON format.
$ jtg -f ./test.txt 6 8
type StructName struct {
Foo string `json:"foo"`
}
Issue Report
If you have any problems with usage, you can reach me at ifatihege@gmail.com e-mail address.
Thanks
While making this package, I used the codes written by Matthew Holt. Thank you to him.