skip to Main Content

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

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

2

Answers


  1. 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
    Login or Signup to reply.
  2. Just pipe the output of jq to format it:

    jq ... | paste -d, - - 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search