3 ms·
> It's about sanitized inputs. Less sanitized and more correctly formatted. Writing literal JSON by hand at the CLI is not fun.
by masklinn 5y ago
> It's about sanitized inputs.
Less sanitized and more correctly formatted. Writing literal JSON by hand at the CLI is not fun.
- oconnor663 5y agoIf I really had to get some JSON into a Bash script, and I couldn't just stick it in a file, I'd probably use a "heredoc" something like this: $ my_json="$(cat <<EOF > { > "foo": "bar", > "baz": 42 > } > EOF > )" $ echo "$my_json" { "foo": "bar", "baz": 42 } But I definitely didn't remember how to handle the closing paren and closing quote correctly, and I had to google for an example just now. So I'm not allowed to say this is easy to remember :)
- aidenn0 5y agoRandom bit of trivia: on ksh you can do foo="$(cat <<EOF)" whatever EOF This is left undefined in the POSIX standard and bourne shells don't allow it.
- _wolfie_ 5y agoThat is why you use jq --arg for the input instead of crafting it by string concatenation. data=$(jq -n \ --arg title 'what"ever' \ --arg endpoint 'foo"bar' \ '{ "title": $title, "endpoint": $endpoint, "enabled": true }')
- aesyondu 5y agoI use jq exclusively for parsing output. Didn't know you could craft JSON with jq as well. Thanks for the tip!