Valentin Leblanc
Valentin Leblanc

Reputation: 115

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 ? :)

Upvotes: 3

Views: 4440

Answers (2)

Logan Lee
Logan Lee

Reputation: 977

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

https://jqplay.org/s/ZBvKf6vQ0F

Upvotes: 0

ikegami
ikegami

Reputation: 385565

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

Upvotes: 9

Related Questions