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?
5 Answers
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
-
6The 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.– mxmlnknCommented 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 ♦Commented 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. Usingsed
with some regex rules, worked just fine enough for me.– mxmlnknCommented 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 ♦Commented 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.– GhermanCommented 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
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 can also do this in one-line from the shell using PHP 5.4+:
php -r 'echo json_encode(json_decode(file_get_contents("file.json")), JSON_PRETTY_PRINT);'
If you just need the first 100 lines, e.g. to preview the contents, do this:
php -r 'echo json_encode(json_decode(file_get_contents("file.json")), JSON_PRETTY_PRINT);' | head -n 100
M-X indent-selection
).jq
. Also very handy if you want to process JSON.