jqlang / jq

Command-line JSON processor
https://jqlang.github.io/jq/
Other
30.08k stars 1.56k forks source link

de-beautify arrays (keep them on a single line) #1462

Open digger18 opened 7 years ago

digger18 commented 7 years ago

I was unable to "de-beautify" arrays after a simple select with jq.

Example Input (less.json): {"name":"configuration","version":"1.0.0","description":"settings","parameters":{"threshold":[[0.7,0.7],0.7,0.7],"margin":[0,0,0,0],"type":"frame"}}

I want to beautify the json but keep the arrays on a single line without new-lines after each element, as in this example:

jq-win64.exe "." less.json { "name": "configuration", "version": "1.0.0", "description": "settings", "parameters": { "threshold": [ [ 0.7, 0.7 ], 0.7, 0.7 ], "margin": [ 0, 0, 0, 0 ], "type": "frame" } }

My intended result will look like:

{ "name": "configuration", "version": "1.0.0", "description": "settings", "parameters": { "threshold": [[0.7,0.7],0.7,0.7], "margin": [0,0,0,0], "type": "frame" } }

How can I achieve this?

adeck commented 7 years ago

This shouldn't really be an issue ticket. It's not an issue with the code itself, nor does it seem to even be a feature request.

You haven't said what you've tried, nor have you specified whether you're using the jq binary from the command line, or the jq python module, or something else.

From the command line there are lots of ways to format text. That's a pretty trivially solved problem. Removing leading whitespace is also a pretty trivially solved problem. You could use sed, or possibly json_pp, or any number of other tools.

That having been said, I just tested --indent 0, and that... behaves really weirdly in that it also causes suppression of newlines. Which feels like a bug. And I'll file an issue ticket for that if it doesn't already exist. But this... is not that ticket.

jbristow commented 7 years ago

It should be noted that jsonlint (a common json formatter/checker) ALSO doesn't do this.

{
  "name": "configuration",
  "version": "1.0.0",
  "description": "settings",
  "parameters": {
    "threshold": [
      [
        0.7,
        0.7
      ],
      0.7,
      0.7
    ],
    "margin": [
      0,
      0,
      0,
      0
    ],
    "type": "frame"
  }
}