Recently I had to deal with some huge JSON backup files and needed to do a simple text search. But the JSON file is compact and so huge like 5Mb. None of my text editors could handle that size of a compact JSON file. The thing is when the JSON is in a compact, it's just one line. So when I open it up with a text editor or an IDE, it just crashes. It can't handle that much of a lengthy line.
I could run a grep command in the command line but since it is just a single line, its not really useful to know the line number.
The trick is to format the JSON file with tabbed indentation, but online Json formatters weren't good enough for this task. same as the text editor it just crashes. Or they don't really allow you to format huge JSON files.
The answer
The answer is Python!
Python comes really handy at this work. Python comes with built-in JSON manipulation libraries, and you can run a python function in the command line even if you don't know how to code in python.
Say my JSON file is test.json. So simply run the below command.
python -m json.tool test.json > formatted.json
note that -m is to run the particular function in the command line. test.json is the input file. The greater than ">" allows you to write the output of the python function into a file.
Note that the above command is for Linux or Mac machines. For windows, this might be a little different.
I could run a grep command in the command line but since it is just a single line, its not really useful to know the line number.
The trick is to format the JSON file with tabbed indentation, but online Json formatters weren't good enough for this task. same as the text editor it just crashes. Or they don't really allow you to format huge JSON files.
The answer
The answer is Python!
Python comes really handy at this work. Python comes with built-in JSON manipulation libraries, and you can run a python function in the command line even if you don't know how to code in python.
Say my JSON file is test.json. So simply run the below command.
python -m json.tool test.json > formatted.json
note that -m is to run the particular function in the command line. test.json is the input file. The greater than ">" allows you to write the output of the python function into a file.
Note that the above command is for Linux or Mac machines. For windows, this might be a little different.
Comments
Post a Comment