I have a large (around 200Mb) single-line json file and I want to convert this to a more readable multi-line json (or txt) file.
I tried to open the file with text editors like sublime text and it takes forever to open. So, I would like to make the conversion without opening the file.
Therefore, I cannot use the interface suggested in this SO question.
I tried to pretty-print
the json file as suggested in this answer by doing the following.
cat myjsonfile.json | python -m json.tool > pretty.json
But the terminal prints the following message and I get an empty pretty.json
file.
Extra data: line 1 column 34255 - line 1 column 173769197 (char 34254 - 173769196)
I'm thinking of installing visual basic, just to convert the file. But is there a better and efficient way to do the conversion?
October 07, 2022. You can read JSON files in single-line or multi-line mode. In single-line mode, a file can be split into many parts and read in parallel. In multi-line mode, a file is loaded as a whole entity and cannot be split. For further information, see JSON Files.
Now you can store multi-line strings in JSON.
The simplest method would be using jq
to pretty print the json:
jq . myjsonfile.json > pretty.json
But from the python output, I suspect the json file may be ill-formed.
alternatively, you can use jtc
unix utility to pretty-print your one-liner json:
jtc myjsonfile.json
you can use there -t
option to control indentation.
If you like to convert myjsonfile.json
from one-liner into pretty-printed, then use option -f
:
jtc -f myjsonfile.json
btw, to convert it back to one-liner again: jtc -fr myjsonfile.json
PS> Disclosure: I'm the creator of the jtc
- shell cli tool for JSON operations
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With