I am trying to create a JSON file with jq from the result of the command "lsb_release"
What i have done :
if [ -x "$(command -v lsb_release)" ]; then
lsb_release -a | jq --raw-input 'split("\t") | { (.[0]) : .[1] }' > ubuntu_release.json
fi
the result is
{
"Distributor ID:": "Ubuntu"
}
{
"Description:": "Ubuntu 20.04.3 LTS"
}
{
"Release:": "20.04"
}
{
"Codename:": "focal"
}
but i want the result
[
{
"Distributor ID:": "Ubuntu"
},
{
"Description:": "Ubuntu 20.04.3 LTS"
},
{
"Release:": "20.04"
},
{
"Codename:": "focal"
}
]
can anybody body help me ? 🙂
>Solution :
Usually, when we want to create an array from a stream of inputs, we can use --slurp/-s. But when combined with --raw-input/-R, this causes the entire input to be provided as a single string (that contains line feeds).
Slurping can also be achieved using --null-input/-n and [ inputs | ... ]. And this works as desired for text files.
jq -nR '[ inputs | split("\t") | { (.[0]) : .[1] } ]'
Demo on jqplay