I have a JSON file on CentOS where all text is on the same line. How can I pretty format it with all the correct indents and everything?

  • 1
    Could you show us an example file? Does this need to be automatic? If not, just load it in a decent text editor and do it manually (in emacs, select everything and M-X indent-selection).
    – terdon
    May 18, 2018 at 13:42
  • Have a look at jq. Also very handy if you want to process JSON.
    – dirkt
    May 18, 2018 at 14:43

4 Answers 4


Use jq a very good JSON processor and from personal preference, its the best available in the market

for just pretty print, use

jq . file_name
  • 5
    The problem I currently have with jq, is that it is very liberal. I basically just want it to change whitespaces, but instead it changes things like 1.23e5 to 123000 and 0 to 0.0 and NaN to null and such things. Because of this I can't trust it anymore. It's not just a formatter it's also a sanitizer at the same time, which is not what I want right now.
    – mxmlnkn
    Jun 7, 2019 at 19:52
  • @mxmlnkn It would not do this if you stored those values as strings. If they are not strings, then I don't really see how it matters.
    – Kusalananda
    Apr 30, 2021 at 18:04
  • @Kusalananda Storing numbers as strings just feels wrong, even if it would only add two characters in JSON. I understand your confusion to some extent, JSON doesn't even discern between float and integers and doesn't even allow NaN for their "numbers". However, I had some (non JSON compliant, JSON-like) data and I had a very finicky reader for that data and all I wanted was to add and format whitespaces. All other changes may and did break something. jq just wasn't the right tool for that job. Using sed with some regex rules, worked just fine enough for me.
    – mxmlnkn
    May 1, 2021 at 9:26
  • @mxmlnkn Well, if what you had wasn't JSON, with its own rules about data types etc., then you should obviously use some other parser for it.
    – Kusalananda
    May 1, 2021 at 9:28
  • @mxmlnkn In many cases storing numbers as either integers or strings may be big advantage or even requirement. Especially for money. IEEE 754's floats introduce rounding errors in a lot of situations.
    – Gherman
    Jul 28, 2021 at 11:45

If you don't want to install an additional package and have python available you can do:

python -m json.tool myfile.json

It also supports reading from STDIN


Use the package yajl, Yet Another JSON Library. On CentOS 7 it is located in the base repo, and is most probably already installed on the machine.

To pretty-print a JSON file:

json_reformat < myfile.json

To verify that a file is in correct JSON syntax:

json_verify < myfile.json
  • 3
    install with sudo apt install yajl-tools in Ubuntu.
    – WesternGun
    Sep 9, 2019 at 12:54

Found 4 already available tools in my Gentoo system:

From package dev-libs/json-glib 19K ELF

json-glib-format -p file.json

From package dev-lang/perl 4,9K Perl script

(keeps unicode symbols as-is)

cat file.json | json_pp

From package dev-libs/yajl 43K ELF

cat file.json | json_reformat

From package dev-lang/python

(escapes unicode symbols to unreadable \u hex notation, had to replace python with python3 on debian system)

python -m json.tool file.json

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .