'Create JSON with jq
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 1:[1]
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
That said, I suspect you will find the following output format more useful:
{
"Distributor ID": "Ubuntu",
"Description": "Ubuntu 20.04.3 LTS",
"Release": "20.04",
"Codename": "focal"
}
This can be achieved by simply adding | add
.
jq -nR '[ inputs | split(":\t") | { (.[0]) : .[1] } ] | add'
Demo on jqplay
One can also use reduce
.
jq -nR 'reduce ( inputs | split(":\t") ) as [ $k, $v ] ( {}; . + { ($k): $v } )'
Demo on jqplay
Solution 2:[2]
Filter
reduce (inputs / ":\t") as [$key, $value] ({}; .+{($key): $value})
Input
Distributor ID: Ubuntu
Description: Ubuntu 20.04.3 LTS
Release: 20.04
Codename: focal
Output
{
"Distributor ID": "Ubuntu",
"Description": "Ubuntu 20.04.3 LTS",
"Release": "20.04",
"Codename": "focal"
}
Note that each line of $key
and $value
from inputs
is processed and combined by reduce
.
Demo
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Logan Lee |