Warning: sackmesser is a prototype at the moment, do not expect the api to be stable
Remember all those times when you had to save JSON in order to find and update a single field? Or worse, where you had to count spaces in yaml? Fear no more, sackmesser will take care of that.
- Supports mutation only, you cannot query JSON with
sackmesser
- Input and output formats are disconnected, both yaml and JSON are supported
- Operations: set field, delete field
- Supports multiple operations in one go
Operations have a signature like op_name(path, args*)
where
-
path is dot delimited, you could use quotes in case field names contain spaces, dots, commas, etc. For array access you can use array notation:
echo '{ "a" : { "test prop": 1 } }' | sackmesser mod 'set(a."test prop", "test")' { "a": { "test prop": "test" } }
echo '{ "a" : { "test prop": [ 1 , true ] } }' | sackmesser mod 'set(a."test prop"[1], "test")' "a": { "test prop": [ 1, "test" ] }
-
Zero or more arguments are required for an operation. An argument could be one of
- a number
null
- A string, you could use single, double quotes and backticks as quotes to minimize escaping
- A JSON value
Available operations:
Operation | Description |
---|---|
set(path, value) | assign field to a particular value |
del(path) | delete a key |
merge(path, value) | merge json value into the path. Only JSON values are allowed |
pop(path) | remove last element from an array |
push(path, value) | add new element to an array |
$ echo '{ "a":1, "prop": { "b": [1,2,3] } }' | sackmesser mod --output-format yaml
a: 1
prop:
b:
- 1
- 2
- 3
$ echo '{ "a":1 }' | sackmesser mod 'set(prop, `{ "test": 123 })`'
{
"a": 1,
"prop": "{ \"test\": 123 }"
}
Please note that you can use three types of quotes for strings - double quotes, single quotes, and backticks
$ echo '{ "a":1 }' | sackmesser mod "set(prop, 'value')"
{
"a": 1,
"prop": "value"
}
$ echo '{ "a":1 }' | sackmesser mod "set(prop, `value`)"
{
"a": 1,
"prop": "value"
}
$ echo '{ "a":1 }' | sackmesser mod 'set(prop, { "test": 123 })'
{
"a": 1,
"prop": {
"test": 123
}
}
You can always spit out a different format if you want!
$ echo '{ "a":1 }' | sackmesser mod --output-format yaml 'set(prop, { "test": 123 })'
{
a: 1
prop:
test: 123
This may come in handy in case you want to apply some specific values to a json template
echo '{ "a" : { "test prop": { "abc": true } } }' | sackmesser mod 'merge(a."test prop", { "testme": true })'
{
"a": {
"test prop": {
"abc": true,
"testme": true
}
}
}
echo '{ "a":1, "deleteme": "please" }' | sackmesser mod 'del(deleteme)'
{
"a": 1
}
You can supply as many commands as you like if needed
echo '{ "a":1, "deleteme": "please" }' | sackmesser mod 'set(b, "test")' 'del(deleteme)'
{
"a": 1,
"b "test",
}
See TODO section for possible changes
Download sackmesser
and install it into a local bin directory.
Latest version:
curl -L https://raw.githubusercontent.com/can3p/sackmesser/master/generated/install.sh | sh
Specific version:
curl -L https://raw.githubusercontent.com/can3p/sackmesser/master/generated/install.sh | sh -s 0.0.4
The script will install the binary into the $HOME/bin
folder by default, you can override this by setting
$CUSTOM_INSTALL
environment variable
Get the archive that fits your system from the Releases page and
extract the binary into a folder that is mentioned in your $PATH
variable.
The project has been scaffolded with the help of kleiner
- More operations
- Some tests will be helpful
Or something like this, suggestions are welcome!
There are awesome alternatives to sackmesser
, which should be considered as well!
- jq - legendary json processor. Compared to
sackmesser
has infinite capabilities heavily skewed towards reading the data, however, mutation is also possible,jq
works with JSON only - jj - this tool is optimized for speed and supports JSON lines. Compared to
sackmesser
it only supports one operation at a time and is optimized for speed
See the License