One of those people who is guilty of frequently relying on online 3rd party JSON formatting tools so messily formatted JSON strings or one-liners can be saner to read. If you are like me who deal with JSON almost every day for development, reading log messages, error tracing, etc., readability for JSON is a must.
A readily available tool or alternative should be within your arsenal, just an arms length away whenever you need it. Also, if you deal with sensitive information, better stay away from copy-pasting JSON string into 3rd party online formatter tools. You don’t want to accidentally expose data, do you?
We will need Gedit text editor GUI application for this, so one might say this method is Linux specific. But I guess if you can run it on other platforms, perhaps on Windows using WSL (Windows Subsystem for Linux), that would work too. I’ve been using this trick on Ubuntu for a while.
(1) Open Gedit
(2) Go to: Tools > Manage External Tools
Below are screenshots. On the left, pull down the Tools menu and select the highlighted entry. Then on the right side a smaller window will appear.
(3) Click on the + sign on the lower left hand side of the window to Add A New Tool. Name the entry to something meaningful. In my case I named it – Format JSON.
(4) Copy the block of Python code below in a new tool entry
#! /usr/bin/env python import json import sys j = json.load(sys.stdin) print json.dumps(j, sort_keys=True, indent=2)
(5) Fill out the fields
- Shortcut key: (Is optional)
- Save: Nothing (default)
- Input: Current document
- Output: Replace current document
- Applicability: All documents (default)
Finally, it should look like this:
Now we are all set. Changes are saved automatically.
Test it out.
Navigate to: Tools > External Tools > Format JSON
(Or use the shortcut key, if one was assigned for the tool.)
The only caveat to this is that it is pretty rudimentary. You won’t be able to collapse the JSON. It is monochromatic too.
- > Format JSON String From IntelliJ IDEA November 20, 2020
- > Change Guake Width Permanently November 6, 2011
- > Switching Windows Of Same App On Unity July 14, 2011
- > Bind Super Key To Application Launcher on KDE March 6, 2014
- > Gnome new window placement October 19, 2010