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).
Answer from Jason D on Stack OverflowI 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).
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.
Videos
You can use inject language feature.
Ex:
String myJsonString = "";
- Place cursor between " and ".
- Alt + Enter -> Inject language or reference -> select json from the dropdown appears,
- Now you have injected json to that String. Again Alt + Enter -> Edit Json Fragment.
- Now you can give your json inside pop-up tool as a normal json, Intellij will convert it as a String with escape characters for you. :))

Refer official documentation for more info.
For anyone that is using STS (Spring Tool Suite) and generally Eclipse the escape feature can be enabled by checking the checkbox under:
Preferences -> Java -> Editor -> Typing -> Escape text when pasting into a string literal
after this just define a String and paste the json content and the IDE will take care of the escaping.