jj
jj copied to clipboard
Multiple changes to the same file in one run
Hey there,
Thank you for an awesome tool! For my use case I need to do multiple small modifications to the same json document and I'd like to do them in one call to the binary.
So for example, instead of:
jj-v1.2.1 -v 500 -O -i my_file.json -o my_file.json bytes_received
jj-v1.2.1 -v 1500 -O -i my_file.json -o my_file.json bytes_expected
jj-v1.2.1 -v 75.62 -O -i my_file.json -o my_file.json upload_duration
I'd like to do something like this:
jj-v1.2.1 -v 500,1500,75.62 -O -i my_file.json -o my_file.json bytes_received,bytes_expected,upload_duration
Reasoning is, less child processes (which are costly in node.js, which I am using) and reading in the same json file only once in Go instead of multiple times (https://github.com/tidwall/jj/blob/master/cmd/jj/main.go#L151) and also saving it only once to disk instead of multiple times (https://github.com/tidwall/jj/blob/master/cmd/jj/main.go#L246).
Is this something that you'd consider implementing, or maybe even accepting a PR for it? 😱
This is a good idea.
I recommend using standard command line args for each key/value rather than comma-delimited, because there's the possibility that commas are a part of the JSON key it's associated value, and we should avoid any additional escaping.
jj -v 500 -v 1500 -v 75.62 -O -i my_file.json -o my_file.json bytes_received bytes_expected upload_duration
This way the jj
command will recognize that there are three updates occuring to the JSON file and expect three keys.
How does this sound?
Good propasal, @tidwall. What should happen if someone if trying to updates multiple properties via stdin/file and does not supply the values using -v
? Should that be prohibited?
Yeah. I think that should be prohibited. Multiple keys for update/set operations only. Unless we want to accept multiple keys for a get operations and return the values as a JSON array.
Temporary solution - edit for your requirements:
jj_v() {
__val="$1";
__key="$2";
jj -v "$__val" "$__key" | (test -n "$3" && f "${@:3}" || cat)
}
echo '{"name":{"last":"Smith"}}' | jj_v Andy name.first Dandy name.middle
outputs:
{
"name": {
"middle": "Dandy",
"first": "Andy",
"last": "Smith"
}
}