Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Python json.tool Command



The Python json.tool is a built-in command-line utility that is used to pretty-print JSON data. It helps in formatting and validating JSON files or strings directly from the terminal.

This command line is useful when working with JSON data to ensure proper formatting, making it easier to read and debug.

Usage

The json.tool is used as a command-line utility in Python. It can read JSON input from a file or standard input and output the formatted JSON to the console.

Syntax

To use json.tool from the command line, use the following syntax −

python -m json.tool input.json

Alternatively, JSON data can be piped to json.tool command for pretty-printing −

echo '{"name":"Alice","age":25,"city":"New York"}' | python -m json.tool

Example: Pretty-Printing JSON from a File

Suppose we have a JSON file named data.json with the following content −

JSON File (data.json):

{"name":"Alice","age":25,"city":"New York"}

Executing the following command in the command prompt will pretty-print the JSON data −

C:\Users\Tutorialspoint> py -m json.tool C:\Users\Tutorialspoint\Desktop\data.json

Following is the output obtained −

{
    "name": "Alice",
    "age": 25,
    "city": "New York"
}

Example: Validating JSON Data

The json.tool command also checks whether the input JSON is valid. If the JSON contains syntax errors, it raises an error −

Invalid JSON File (invalid.json):

{
   "name": Alice,
   "age": 25,
   "city": "New York"
}

Executing the following command will validate the JSON −

C:\Users\Tutorialspoint>py -m json.tool C:\Users\Tutorialspoint\Desktop\invalid.json

Following is the output in case of an error −

Expecting value: line 2 column 12 (char 13)

Example: Reading JSON from Standard Input

We can also use json.tool command with standard input (stdin) to format JSON dynamically. I executed it in powershell −

PS C:\Users\Tutorialspoint> '{"name":"Alice","age":25,"city":"New York"}' | py -m json.tool

Following is the output obatined −

{
    "name": "Alice",
    "age": 25,
    "city": "New York"
}

Example: Writing Pretty-Printed JSON to a File

We can redirect the formatted output to a new file using the following command −

PS C:\Users\Tutorialspoint> py -m json.tool C:\Users\Tutorialspoint\Desktop\data.json > C:\Users\Tutorialspoint\Desktop\formatted.json

This will create a new file formatted.json with properly indented JSON data.

Example: Minifying JSON Output

To minify (compress) JSON by removing extra spaces and indentation, use the --compact option −

PS C:\Users\Tutorialspoint> py -m json.tool --compact C:\Users\Tutorialspoint\Desktop\data.json

Following is the output obtained −

{"name":"Alice","age":25,"city":"New York"}

Example: Sorting Keys in JSON

To sort JSON keys alphabetically, use the --sort-keys option −

PS C:\Users\Tutorialspoint> py -m json.tool --sort-keys C:\Users\Tutorialspoint\Desktop\data.json

Following is the output obtained −

{
    "age": 25,
    "city": "New York",
    "name": "Alice"
}
python_json.htm
Advertisements