Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
  "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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading