Is there some accepted 'best practice' of generating JSON documents using bash and jq? I have a script to gather various data, and to make it easier to further process using other tools I'd like to output the data in JSON format. So I'm using jq to make sure all the quoting etc. gets done correctly, as recommended in this answer: https://stackoverflow.com/a/48470227/75652. However, I'm struggling with how to generate it piecemeal instead of one giant jq call at the end. E.g. something like
read foo <<<$(</path/to/some/oneliner/file)
jq -n --arg f $foo '{foo: $f}'
bar=$(some_command)
jq -n --arg b $bar '{bar: $b}'
Will generate two separate objects (which can be processed with tools that support various more or less informal "JSON streaming" formats, including jq) whereas I'd want a single object, something like
{ "foo": SOMETHING, "bar": SOMETHING_ELSE }
but I can't do that with multiple jq calls as jq will complain that the incomplete JSON is malformed.
And to further add some complexity, in some cases I need to generate nested JSON structures. In another language like python I'd just put all the data in a set of nested dictionaries and then dump it to JSON in the end, but nested dictionaries in bash seem very tedious..