There are basically two ways.
Give the JSON payload to
jqas a string:$ jq -n --arg rec '{ "id": 1, "name": "x"}' '$ARGS.named' { "rec": "{ \"id\": 1, \"name\": \"x\"}" }Note that
jqwill not validate the supplied string in any way and will preserve it as it was, excessive whitespace and all.Give
jqthe payload as an input JSON document and usetojsonto stringify it:$ jq -n --argjson payload '{ "id": 1, "name": "x"}' '.rec = ($payload|tojson)' { "rec": "{\"id\":1,\"name\":\"x\"}" }Here,
jqwould complain if the given JSON fragment was invalid, and it also "compresses" it by removing unnecessary whitespace.The same thing, but reading from standard input:
$ echo '{ "id": 1, "name": "x"}' | jq -n '.rec = (inputs|tojson)' { "rec": "{\"id\":1,\"name\":\"x\"}" }The difference between
inputandinputsis thatinputreads a single object from the input ofjq, whileinputsread all remaining objects.Or, since it's shorter, pull the input directly into
tojsonby removing the-n/--null-inputoption:$ echo '{ "id": 1, "name": "x"}' | jq '{ rec: tojson }' { "rec": "{\"id\":1,\"name\":\"x\"}" }This would slurp all data from the input of
jq(just like when usinginputs, with-n) and insert it as the value of thereckey, viatojson.
To do the opposite, i.e., to decode the stringified payload, use fromjson:
$ echo '{"rec":"{\"id\":1,\"name\":\"x\"}"}' | jq '.rec | fromjson' { "id": 1, "name": "x" } $ jq -n --argjson doc '{"rec":"{\"id\":1,\"name\":\"x\"}"}' '$doc.rec | fromjson' { "id": 1, "name": "x" } Note that echo, in general, isn't the best utility for passing JSON documents to jq (or to anywhere else, for that matter), as depending on your shell and its configuration, it may choose to expand strings like \n and \t to literal newlines and tabs, which would break the formatting of the provided document.