JavaScript object notation (JSON) is a common human-readable data format. When working with APIs, may need to make it more compact (minified) to save space, or format it to be more readable. There are different ways to switch between minified or pretty-printed JSON, including several good services online. But do you really want to upload a JSON document to a web server for formatting?

In addition to the potential for an accidental security breach, feels wasteful to be transmitting a document over the internet to be formatted on someone else’s computer. Your local computer can handle compressing or formatting JSON, and your system likely already has the tools to do it.

Many modern IDEs and text editors have JSON conversion tools built-in or available as extensions.

Tool Description
Visual Studio Code Install extension JSON Tools by Erik Lynd. After installing, use the shortcut Alt+M to minify JSON and Ctrl+Alt+F (Windows) or Cmd+Alt+M (Mac) to format it.
Visual Studio 2022 or 2019 Install extension JSON Formatter by Kenton Standard. After installing, right-click your toolbar and select Json Formatter.
IntelliJ IDEs Select text Ctrl+Shift+J to minify (join lines), and Ctrl+Alt+L (Windows) or Cmd+Opt+L (Mac) or select Code > Reformat Code from the menu to format it.
Eclipse Install plugin JSON Editor. After installing, press Ctrl+Shift+F to format or minify
Notepad++ Install the [JSTool plugin by Sandi]. After installing, press Ctrl+Alt+M or select Plugins > JSTool > JSMin to minify. Select Plugins > JSTool > JSFormat to format it

Use command-line tools to minify or format JSON

To format JSON

Windows comes with the PowerShell cmdlet ConvertTo-Json built-in. To convert a minified JSON file named myfile.json to formatted JSON:

# PowerShell script to format a JSON file
Get-Content myfile.json | ConvertTo-Json

You can also create a script to take content from the clipboard and minify it. For example, the script below takes JSON from the clipboard and minifies or compresses it.

# PowerShell command to format JSON from the clipboard
if (Get-Clipboard | Test-Json 2>$null) { Get-Clipboard | ConvertFrom-Json | ConvertTo-Json }

On Mac, Linux, or Windows Subsystem for Linux, install the open-source jq package (see https://stedolan.github.io/jq/ for documentation). jq is a command-line tool that can be used to parse JSON files. The binary is open-source and is available for most systems. It also can be installed using a variety of package managers like Homebrew (Mac), Chocolatey (Windows) and most Linux package managers. Once installed, you can use it to parse JSON files from the command line.

# Bash command to format a JSON file
jq . myfile.json

# Bash command to send a file to jq
cat myfile.json | jq .

To minify JSON

On Windows, minify JSON by adding -Compress to the call to ConvertTo-Json:

$ Get-Content myfile.json | ConvertTo-Json -Compress

To minify JSON from the clipboard:

# PowerShell command to Minify JSON from the clipboard
if (Get-Clipboard | Test-Json 2>$null) { Get-Clipboard | ConvertFrom-Json | ConvertTo-Json -Compress }

On Linux or WSL, use jq with the -r tostring switch to minify JSON:

$ cat myfile.json | json -r tostring

Convert directly to and from the clipboard in Windows Subsystem for Linux (WSL)

In PowerShell, you can use the Get-Clipboard and Set-Clipboard cmdets to pipe to and from the clipboard, but WSL doesn’t have access to the Windows clipboard. However, WSL does let you call PowerShell scripts on your host system, so you can get and set the Windows clipboard by calling PowerShell from inside WSL like this:

# WSL Bash command to get JSON from the Windows clipboard
powershell.exe Get-Clipboard | jq .