build_json.sh
- 1 minutes read - 177 wordsThis might seem silly, but I’ve beeing playing with some json.sh scripts that build legitimate json bodies and are easily filled into a shell script variable as needed.
The basic driving idea was that there are lots of slick ways to pull data out of JSON(either by programming something with python’s json or running a command line tool like jq or whatever), but not as many friendly ways to build some JSON out of a given token or whatever. Often, you have a list of identifiers and you need to build a bunch of JSON blobs from that list.
For example, say we have a file called things
that contains
thing1
thing2
thing3
thing4
and we need to generate
{"value":"$THINGTOKEN"}
for each token in things
. Then we can simply run a tiny shell command like
Traviss-MacBook-Pro% while read token; do
> echo $(newBuilder | set value $(quoted $token) | build)
> done < things
{"value":"thing1"}
{"value":"thing2"}
{"value":"thing3"}
{"value":"thing4"}
Easy as that! There’s no waiting for heavy VMs to start up or anything like that, just run it.