1 min read | by Jordi Prats
We can define jq tool as what's sed but for JSON data: We can use it for multiple purposes, for example to create a valid JSON object.
Let's assume we want to create a JSON containing a multi-line string, we can simulate it like follows or just use some file with cat:
$ echo -e "multi\nline"
multi
line
In order to generate a string from it that can be used on a JSON object we can use jq to generate the object from a template like this:
$ jq --null-input --arg input "$(echo -e "multi\nline")" '{ "multi-line-string": $input }'
{
"multi-line-string": "multi\nline"
}
By using a different command as an argument we can transform a file into a multi-line string:
$ jq --null-input --arg input "$(cat multiline-file.txt)" '{ "multi-line-string": $input }'
{
"multi-line-string": "multi\nline"
}
Posted on 07/12/2022