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

json jq formatting unnamed objects onto single line

Another question re ‘jq’ formatting onto a single line. Here is my json file:

  "facet_counts":{
    "facet_queries":{},
    "facet_fields":{
      "title":[
        "primary",5981,
        "database",5965,
        "source",5963,
        "eecm",5949,
        "the",5066,
        "research",4888]},
    "facet_ranges":{}
  }
}

As you see there is no label assigned for the tuplets within the title array. I want to print the output like: –

        "primary",5981,
        "database",5965,
        "source",5963,
        "eecm",5949,
        "the",5066,
        "research",4888

So far, I’ve been trying jq -c ‘.facet_counts.facet_fields.title[]’, but members of the tuplet are being output on different lines (i.e. effectively decoupled): –

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

"primary"
5981
"database"
5965
"source"
5963
"eecm"
5949
"the"
5066
"research"
4888

>Solution :

You can split the array into chunks with _nwise, then join() those with a char of your liking:

.facet_counts.facet_fields.title | _nwise(2) | join(": ")
primary: 5981
database: 5965
source: 5963
eecm: 5949
the: 5066
research: 4888

JqPlay Demo
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