How to format pasted JSON in IntelliJ / Android Studio

JsonAndroid StudioIntellij IdeaCode Formatting

Json Problem Overview


I often need to use a text editor while writing code to paste random notes but especially JSON responses, where I format them using a plugin (for Sublime).

I recently heard about the 'scratch file' feature in IntelliJ / Android Studio which does exactly what I need it to - except I can't make it format JSON I paste in nicely.

How can I make Android Studio format JSON in a scratch buffer file?

Json Solutions


Solution 1 - Json

I highlight the code and run Command Option L (a short-cut for the Code->Reformat Code menu).

On Windows use Ctrl Alt L.

Note that this only works if the code is well-formed JSON (clear any red squiggles).

Solution 2 - Json

You are asking about two seperate things: scratch files and scratch buffers.

When you create a scratch file in IntelliJ you can choose the type of the file (e.g. JSON) that you want to create. Based on file's type, IntelliJ provides code formatting (use Code->Reformat code).

However, scratch buffers are just simple .txt files and the only formatting that can be used is the one associated to .txt format. So, if you put JSON into scratch buffer it won't get formatted with JSON type formatter.

I would encourage you to use scratch files instead of scratch buffers if you want JSON formatting.

More information can be found at IntelliJ's official page https://www.jetbrains.com/help/idea/2016.2/scratches.html.

Solution 3 - Json

choose from the menu : code -> reformat code

hotkey On Windows -:

CTRL + Alt + L

Important note- if you have sensitive information in your JSON- NEVER use online tools to parse or beautify it. because this will compromise your organization's and customer's information

Solution 4 - Json

If you run the latest version I'd create a new Scratch File of type JSON. It's really easy, e.g. hit double shift, then search for new scratch file, select JSON as the language, paste your snippet and then use the shortcut that you usually use for formatting any file in intelliJ.

Solution 5 - Json

There is a plugin Save Actions that does auto-formatting (also of JSON) every time when I save the file:

select Android Studio > Preferences > Plugins

check Martketplace tab and search for Save Actions (it has a red square-ish icon) and press Install next to it.

At this point you will need to restart your android studio, and then you can enable "Reformat on save" by:

select Android Studio > Preferences > Other Settings > Save actions, in there, select:

✅ General > activate save actions on save

✅ Formatting actions > Reformat File

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaniel WilsonView Question on Stackoverflow
Solution 1 - JsonJason DView Answer on Stackoverflow
Solution 2 - JsonKamil KubackiView Answer on Stackoverflow
Solution 3 - JsonDrorView Answer on Stackoverflow
Solution 4 - JsonThanosView Answer on Stackoverflow
Solution 5 - JsondrpaweloView Answer on Stackoverflow