Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pretty print using jq, so that multiple values are on the same line?

What I get from jq is:

{
  "frameGrid": {
    "size": [
      24,
      24
    ],
    "dimensions": [
      1,
      1
    ],
    "names": [
      [
        "default"
      ]
    ]
  }
}

What I would like to see is something more like this:

{
  "frameGrid": {
    "size": [24,24], 
    "dimensions": [1,1],
    "names": [["default"]]
  }
}

I know both forms are valid, and that jq has a compact/pretty print mode. But is there something in-between? I'm looking to somehow format a larger json file that has many more array values than this, so that it's easily readable and printable. Maybe I'm just using the wrong tool for this job?

(please excuse the horrid formating choice. Seems code-sample doesn't like json formats much)

like image 828
Alex Summers Avatar asked Jan 03 '23 07:01

Alex Summers


1 Answers

While it is probably best to use a tool like the one peak suggested if your json isn't too complex you could use a second jq invocation to postprocess the output of the first. For example if your data is in data.json

$ jq -M . data.json | jq -MRsr 'gsub("\n      +";"")|gsub("\n    ]";"]")'

produces

{
  "frameGrid": {
    "size": [24,24],
    "dimensions": [1,1],
    "names": [["default"]]
  }
}
like image 181
jq170727 Avatar answered Jan 07 '23 12:01

jq170727