Daniel Wilson
Daniel Wilson

Reputation: 19824

How to format pasted JSON in IntelliJ / Android Studio

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?

Upvotes: 116

Views: 183585

Answers (6)

cellepo
cellepo

Reputation: 4479

There is now a plugin (that leverages Reformat Code): https://plugins.jetbrains.com/plugin/13931-json-formatter

Although it seems to do essentially the same thing as Jason D's Answer (maybe with 1 less step), so not really much added functionality.

Upvotes: 0

Dror
Dror

Reputation: 5505

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

Upvotes: 30

drpawelo
drpawelo

Reputation: 2570

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

Upvotes: 2

Jason D
Jason D

Reputation: 8643

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).

Upvotes: 206

Kamil Kubacki
Kamil Kubacki

Reputation: 897

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.

Upvotes: 70

Thanos
Thanos

Reputation: 340

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.

Upvotes: 10

Related Questions