Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Pretty Print JSON [Python One-Liner]

#1
Pretty Print JSON [Python One-Liner]

<div><p><strong>Problem</strong>: Given a JSON object. How to pretty print it from the shell/terminal/command line using a Python one-liner?</p>
<p><strong>Minimal Example</strong>: You have given the following JSON object:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">{"Alice": "24", "Bob": "28"}</pre>
<p>And you want to get the following print output:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">{ "Alice": "24", "Bob": "28"
}</pre>
<p>How to accomplish this using a Python one-liner?</p>
<h2>Method 0: Python Program + json.dump</h2>
<p>The default way to accomplish this <strong><em>in a Python script</em></strong> is to import the <code>json</code> library to solve the issue:</p>
<p> <iframe height="400px" width="100%" src="https://repl.it/@finxter/jsonprettyprint?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe> </p>
<p><em><strong>Exercise</strong>: Execute the script. What’s the output? Now change the number of indentation spaces to 2!</em></p>
<p>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!</p>
<h2>Method 1: Terminal / Shell / Command Line with Echo + Pipe + json.tool</h2>
<figure class="wp-block-image size-large"><img src="https://blog.finxter.com/wp-content/uploads/2020/08/image-12.png" alt="" class="wp-image-12298" srcset="https://blog.finxter.com/wp-content/uploads/2020/08/image-12.png 657w, https://blog.finxter.com/wp-content/uplo...300x42.png 300w, https://blog.finxter.com/wp-content/uplo...150x21.png 150w" sizes="(max-width: 657px) 100vw, 657px" /></figure>
<p>The echo command prints the JSON to the standard output. This is then piped as standard input to the <code>json.tool</code> program that pretty prints the JSON object to the standard output:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">echo '{"Alice": "24", "Bob": "28"}' | python -m json.tool</pre>
<p>The output is the prettier:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">{ "Alice": "24", "Bob": "28"
}</pre>
<p>The pipe operator <code>|</code> redirects the output to the standard input of the Python script. </p>
<h2>Method 2: Use a File as Input with json.tool</h2>
<p>An alternative is the simple:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">python -m json.tool file.json</pre>
<p>This method is best if you have stored your JSON object in the <code>file.json</code> file. If the file contains the same data, the output is the same, too:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">{ "Alice": "24", "Bob": "28"
}</pre>
<h2>Method 3: Use Web Resource with json.tool</h2>
<p>If your JSON file resides on a given URL <code>https://example.com</code>, you’ll best use the following one-liner:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">curl https://example.com/ | python -m json.tool</pre>
<p>Again, assuming the same JSON object residing on the server, the output is the same:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">{ "Alice": "24", "Bob": "28"
}</pre>
<h2>Method 4: Use jq</h2>
<p>This is the simplest way but it assumes that you have the <code>jq</code> program installed on your machine. You can download <code>jq</code> here and also read about the excellent quick-start resources <a href="https://stedolan.github.io/jq/" target="_blank" rel="noreferrer noopener" title="https://stedolan.github.io/jq/">here</a>.</p>
<p>Let’s dive into the code you can run in your shell:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">jq &lt;&lt;&lt; '{ "foo": "lorem", "bar": "ipsum" }'
{ "bar": "ipsum", "foo": "lorem"
}</pre>
<p>The <code>&lt;&lt;&lt;</code> 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 <a href="https://unix.stackexchange.com/questions/80362/what-does-mean" target="_blank" rel="noreferrer noopener" title="https://unix.stackexchange.com/questions/80362/what-does-mean">this </a>SO thread. </p>
<p>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. </p>
<h2>Python One-Liners Book</h2>
<p><strong>Python programmers will improve their computer science skills with these useful one-liners.</strong></p>
<figure class="wp-block-image size-medium is-resized"><a href="https://www.amazon.com/gp/product/B07ZY7XMX8" target="_blank" rel="noopener noreferrer"><img src="https://blog.finxter.com/wp-content/uploads/2020/06/3D_cover-1024x944.jpg" alt="Python One-Liners" class="wp-image-10007" width="512" height="472" srcset="https://blog.finxter.com/wp-content/uploads/2020/06/3D_cover-scaled.jpg 1024w, https://blog.finxter.com/wp-content/uplo...00x277.jpg 300w, https://blog.finxter.com/wp-content/uplo...68x708.jpg 768w" sizes="(max-width: 512px) 100vw, 512px" /></a></figure>
<p><a href="https://amzn.to/2WAYeJE" target="_blank" rel="noreferrer noopener" title="https://amzn.to/2WAYeJE"><em>Python One-Liners</em> </a>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.</p>
<p>The book’s five chapters cover tips and tricks, regular expressions, machine learning, core data science topics, and 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:</p>
<p><strong>•</strong>&nbsp;&nbsp;Leverage data structures to solve real-world problems, like using Boolean indexing to find cities with above-average pollution<br /><strong>•</strong>&nbsp;&nbsp;Use NumPy basics such as array, shape, axis, type, broadcasting, advanced indexing, slicing, sorting, searching, aggregating, and statistics<br /><strong>•</strong>&nbsp;&nbsp;Calculate basic statistics of multidimensional data arrays and the K-Means algorithms for unsupervised learning<br /><strong>•</strong>&nbsp;&nbsp;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<br /><strong>•</strong>&nbsp;&nbsp;Understand a wide range of computer science topics, including anagrams, palindromes, supersets, permutations, factorials, prime numbers, Fibonacci numbers, obfuscation, searching, and algorithmic sorting</p>
<p>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.</p>
<p><strong><a href="https://amzn.to/2WAYeJE" target="_blank" rel="noreferrer noopener" title="https://amzn.to/2WAYeJE"><em>Get your Python One-Liners Now!!</em></a></strong></p>
</div>


https://www.sickgaming.net/blog/2020/08/...one-liner/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016