Problem: Given a JSON object. How to pretty print it from the shell/terminal/command line using a Python one-liner?
Minimal Example: You have given the following JSON object:
{"Alice": "24", "Bob": "28"}
And you want to get the following print output:
{ "Alice": "24", "Bob": "28" }
How to accomplish this using a Python one-liner?
Method 0: Python Program + json.dump
The default way to accomplish this in a Python script is to import the json
library to solve the issue:
Exercise: Execute the script. What’s the output? Now change the number of indentation spaces to 2!
However, what if you want to run this from your operating system terminal as a one-liner command? Let’s dive into the four best ways!
Method 1: Terminal / Shell / Command Line with Echo + Pipe + json.tool
The echo command prints the JSON to the standard output. This is then piped as standard input to the json.tool
program that pretty prints the JSON object to the standard output:
echo '{"Alice": "24", "Bob": "28"}' | python -m json.tool
The output is the prettier:
{ "Alice": "24", "Bob": "28" }
The pipe operator |
redirects the output to the standard input of the Python script.
Method 2: Use a File as Input with json.tool
An alternative is the simple:
python -m json.tool file.json
This method is best if you have stored your JSON object in the file.json
file. If the file contains the same data, the output is the same, too:
{ "Alice": "24", "Bob": "28" }
Method 3: Use Web Resource with json.tool
If your JSON file resides on a given URL https://example.com
, you’ll best use the following one-liner:
curl https://example.com/ | python -m json.tool
Again, assuming the same JSON object residing on the server, the output is the same:
{ "Alice": "24", "Bob": "28" }
Method 4: Use jq
This is the simplest way but it assumes that you have the jq
program installed on your machine. You can download jq
here and also read about the excellent quick-start resources here.
Let’s dive into the code you can run in your shell:
jq <<< '{ "foo": "lorem", "bar": "ipsum" }' { "bar": "ipsum", "foo": "lorem" }
The <<<
operator passes the string on the right to the standard input of the command on the left. You can learn more about this special pipe operator in this SO thread.
While this method is not a Python script, it still works beautifully when executed from a Linux or MacOS shell or the Windows Powershell / command line.
Python One-Liners Book: Master the Single Line First!
Python programmers will improve their computer science skills with these useful one-liners.
Python One-Liners will teach you how to read and write “one-liners”: concise statements of useful functionality packed into a single line of code. You’ll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert.
The book’s five chapters cover (1) tips and tricks, (2) regular expressions, (3) machine learning, (4) core data science topics, and (5) useful algorithms.
Detailed explanations of one-liners introduce key computer science concepts and boost your coding and analytical skills. You’ll learn about advanced Python features such as list comprehension, slicing, lambda functions, regular expressions, map and reduce functions, and slice assignments.
You’ll also learn how to:
- Leverage data structures to solve real-world problems, like using Boolean indexing to find cities with above-average pollution
- Use NumPy basics such as array, shape, axis, type, broadcasting, advanced indexing, slicing, sorting, searching, aggregating, and statistics
- Calculate basic statistics of multidimensional data arrays and the K-Means algorithms for unsupervised learning
- Create more advanced regular expressions using grouping and named groups, negative lookaheads, escaped characters, whitespaces, character sets (and negative characters sets), and greedy/nongreedy operators
- Understand a wide range of computer science topics, including anagrams, palindromes, supersets, permutations, factorials, prime numbers, Fibonacci numbers, obfuscation, searching, and algorithmic sorting
By the end of the book, you’ll know how to write Python at its most refined, and create concise, beautiful pieces of “Python art” in merely a single line.