How to do an unpretty print on pretty JSON file in shell >> serial string JSON >> ES _bulk? How to do an unpretty print on pretty JSON file in shell >> serial string JSON >> ES _bulk? json json

How to do an unpretty print on pretty JSON file in shell >> serial string JSON >> ES _bulk?


You can try the great jq tool for parsing JSON in the shell. To de-pretty print with jq, you can use either method below:

cat pretty-printed.json | jq -c .jq -c . pretty-printed.json

the -c (or --compact-output) tells it to not pretty print (which is the default). The "." tells it to return the JSON content "as is" unmodified other than the reformatting. It gets dumped back to stdout, so you can redirect output or pipe it to something else.

P.S. I was looking to address the same problem and came to this option.


The answer from D_S_toowhite was not a direct answer but it set me thinking in the right way i.e., the problem is to remove all the white space. I found a very simple way to remove all white space using command line tool tr:

tr -d [:space:] inputfile

The :space: tag removes all white space, tabs, spaces, vertical tabs etc. So a pretty JSON input like this:-

{    "version" : "4.0",    "success" : true,    "result" :    {            "Focus" : 0.000590008,            "Arc" : 12    }}

becomes this JSON serial string:

{"version":"4.0","success":true,"result":{"Focus":0.000590008,"Arc":12}}

I still have to solve the \n terminator but I think that is trivial now at least in my special case, just append after closing bracket pair using sed.

Many thanks for the suggestion.

Cheers

Sid


jsonlint is easy to get up and running in the command line with the help of npm, and a simple way to print out 'no fluff' JSON is to give it an indentation character of "".

jsonlint -t ""

As a bonus for command line users, I use this all the time to take paste buffers (on a Mac) and convert them into something else, for instance:

Swap buffer contents for a JSON linted 'compressed' format:

pbpaste | jsonlint -t "" | pbcopy

Swap buffer contents for a pretty printed JSON linted format:

pbpaste | jsonlint | pbcopy

You could also pipe file contents to an ugly (and JSON linted) version of the file:

cat data-pretty.json | jsonlint -t "" > data-ugly.json