Nim reimplementation of the great gron tool.
Converts json text into Javascript statements, and makes it easy to grep!
Lets take a very simple json
file
{
"integer": 1,
"float": 2.1,
"string": "string",
"array": [
0,
"1",
2.0
],
"nest": {
"ted": "ಠ_ಠ",
"boolean": true,
"null": null
}
}
We can save it as test.json
. Now lets try to play with it using ngron
Lets convert it to gron
, which outputs valid json assignment statements that would generate the json input
> ./ngron test.json
json = {};
json.integer = 1;
json.float = 2.1;
json.string = "string";
json.array = [];
json.array[0] = 0;
json.array[1] = "1";
json.array[2] = 2.0;
json.nest = {};
json.nest.ted = "ಠ_ಠ";
json.nest.boolean = true;
json.nest.null = null;
Or jgron
, which outputs a stream of paths and values tuples
> ./ngron -o jgron test.json
[[],{}]
[["integer"],1]
[["float"],2.1]
[["string"],"string"]
[["array"],[]]
[["array",0],0]
[["array",1],"1"]
[["array",2],2.0]
[["nest"],{}]
[["nest","ted"],"ಠ_ಠ"]
[["nest","boolean"],true]
[["nest","null"],null]
Lets save the gron output to a file out.gron
> ./ngron test.json > out.gron
And finally lets convert our out.gron
file to get the original json input
> ./ngron -o json out.gron
{
"integer": 1,
"float": 2.1,
"string": "string",
"array": [
0,
"1",
2.0
],
"nest": {
"ted": "ಠ_ಠ",
"boolean": true,
"null": null
}
}
You can also use your clipboard as an input source with the -c
flag
Transform JSON (from a file, URL, clipboard or stdin) into discrete assignments to make it greppable
Usage:
[options] [input]
Arguments:
[input] Path to file or URL path. Ignored if piping from stdin (default: stdin)
Options:
-h, --help
--version Print version information
-s, --sort Sort keys (slower)
-v, --values Print just the values of provided assignments
-c, --clipboard Use clipboard as input
-r, --raw Print without color (ignores input argument)
-i, --input-type=INPUT_TYPE
Input type (Inferred from file extension) Possible values: [json, gron, jgron] (default: json)
-o, --output-type=OUTPUT_TYPE
Output type Possible values: [json, gron, jgron] (default: gron)
To build and develop you will need
git
nimble
nim >= 2.0.4
- Input Types
- File
- URL (http/https)
- Clipboard
- stdin
- Input formats
- JSON
- JSON stream
- gron
- jgron
- Output formats
- JSON
- gron
- jgron
- Output formatting
- Sorted keys
- Colorized
- Values only
- Broader testing
- Large files, weird characters.
- Windows, Mac OS
- Full compliance with JSON spec
- Identifiers are not really compliant with the spec
- Benchmarks and test
- Memory consumption and performance vs original implementation
- Optimizations
- Compact json object data structure
- Stream input instead of reading all string in memory
- Better handling of allocations in tokenizer/parser
- Multithreading
- Prebuilt binaries
The test files of the original gron are part of this repository, they are located in tests/resources/gron
Other tests files are added to the folder tests/resources
The original testing and development has been carried on Ubuntu 20.4
- gron repository
- Nim std library docs
- Crafting interpreters book. The parsers are heavily inspired on the book!
- Scripter's notes on tty on Nim, has some great info on Nim tricks all around