- How to Pretty Print JSON File in Linux Terminal
- Pretty print JSON with jq command in Linux
- Bonus: Minify a JSON file with jq command
- Using Python to pretty print JSON file in Linux
- How to read and print pretty JSON with PHP
- Example-1: Print JSON without formatting
- Example-2: Print JSON using JSON_PRETTY_PRINT option and header() function
- Example-3: Print JSON using JSON_PRETTY_PRINT option and » ; > ? >
- Conclusion
- About the author
- Fahmida Yesmin
- Linux pretty print json
- About
- docker inspect -f json pretty print #110
- Comments
- gtseres commented Sep 22, 2017 •
- Expected behavior
- Actual behavior
- Steps to reproduce the behavior
- SimonHeimberg commented Dec 7, 2018
- mikzuit commented Mar 4, 2020
- thaJeztah commented May 29, 2020
- Python JSON pretty print
- The json.dump() function
- Python pretty print JSON file
- Conclusion
- About the author
- Kamran Sattar Awaisi
How to Pretty Print JSON File in Linux Terminal
Last updated December 8, 2020 By Abhishek Prakash 2 Comments
JSON files are awesome because they store collection of data in a human-readable format. However, reading the JSON file can be a pain if the JSON file is minified.
Take this for an example:
A computer can easily read it. Even a human can still read it but if JSON file is properly formatted to display the content, it will be much easier. I mean JSON files are supposed to read like this after all:
You can use most text editor with some plugins to display it with proper formatting. However, if you are stuck to a terminal or if you want to do it in your shell script, things will be different.
If you got a minified file, let me show you how to pretty print the JSON file in Linux terminal.
Pretty print JSON with jq command in Linux
jq is a command line JSON processor. You can use it to slice, filter, map and transform structured data. I am not going in details about using jq command line tool here.
To use jq, you need to install it first. You can use your distribution’s package manager to install it. With universe repository enabled, you can install it on Ubuntu using the apt command:
Once you have it installed, use it in the following manner to pretty print JSON file on the display:
You may also tempt to use cat but I believe it one of the useless use of cat command.
Keep in mind that the above command will not impact the original JSON file. No changes will be written to it.
You probably already know how to redirect the command output to a file in Linux. You probably also know that you cannot redirect to the same file and the tee command is not guaranteed to work all the time.
If you want to modify the original JSON file with pretty print format, you can pipe the parsed output to a new file and then copy it to the original JSON file.
Bonus: Minify a JSON file with jq command
Let’s take a reverse stance and minify a well formatted JSON file. To minify a JSON file, you can use the compact option -c.
You can also use cat and redirection if you want:
Using Python to pretty print JSON file in Linux
It’s more likely that you have Python installed on your system. If that’s the case, you can use it pretty print the JSON file in the terminal:
I know there are other ways to parse JSON file and print it with proper format. You may explore them on your own but these two are sufficient to do the job which is to pretty print JSON file.
Like what you read? Please share it with others.
Источник
How to read and print pretty JSON with PHP
Example-1: Print JSON without formatting
json_encode() function of PHP is used to parse any JSON data. Create a file named exp1.php with the following code to read a simple JSON data and print the output. Here, an associative array is declared to generate JSON data. No formatting is applied for JSON data in the code. So, JSON data will be printed in a single line in JSON format.
exp1.php
// Declare the array
$courses =array ( «Module-1» = > «HTML» , «Module-2» = > «JavaScript» , «Module-3» = > «CSS3» ,
«Module-4» = > «PHP» ) ;
// Print the array in a simple JSON format
echo json_encode ( $courses ) ;
? >
Output:
The following output will appear after executing the file from the browser.
Example-2: Print JSON using JSON_PRETTY_PRINT option and header() function
PHP has an option named ‘JSON_PRETTY_PRINT’ which is used with json_encode() function to print JSON data with proper alignment and particular format. Create a file named exp2.php with the following code. In the code, the same array of the previous example is used to see the use JSON_PRETTY_PRINT option. header() function is used here to inform the browser about the file content. No formatting will be applied without this function.
exp2.php
?php
// Declare the array
$courses =array ( «Module-1» = > «HTML» , «Module-2» = > «JavaScript» , «Module-3» = > «CSS3» ,
«Module-4» = > «PHP» ) ;
// Notify the browser about the type of the file using header function
header ( ‘Content-type: text/javascript’ ) ;
// Print the array in a simple JSON format
echo json_encode ( $courses , JSON_PRETTY_PRINT ) ;
? >
Output:
The following output will appear after executing the file from the browser. A specific font and alignment will be applied.
Example-3: Print JSON using JSON_PRETTY_PRINT option and » ;
>
? >
Output:
The following output will appear after executing the file from the browser. Here, each question and answer of the JSON data will be printed with blue color and bold format and, another part will be printed with red color.
Conclusion
How you can print formatted JSON data by using various PHP options are tried to show in this article. Hope, the reader will be able to apply the PHP to format JSON data and generate pretty JSON output after practicing the above examples properly.
About the author
Fahmida Yesmin
I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.
Источник
Linux pretty print json
Pretty-print JSON data into HTML to indent and colorize (written in TypeScript)
For dark mode, replace pretty-print-json.css with pretty-print-json.dark-mode.css in the
tag.
Install package for node:
Import into your application:
Or for older CommonJS/UMD environments:
Pass data into prettyPrintJson.toHtml() and display the results.
Name (key) | Type | Default | Description |
---|---|---|---|
indent | integer | 3 | Number of spaces for indentation. |
linkUrls | boolean | true | Create anchor tags for URLs. |
quoteKeys | boolean | false | Always double quote key names. |
4) TypeScript Declarations
The TypeScript Declaration File file is pretty-print-json.d.ts in the dist folder.
The output of the prettyPrintJson.toHtml(thing: unknown, options?: FormatOptions) function is configured with a FormatOptions object:
Example TypeScript usage with explicit types:
5) Contributor Notes
To be a contributor, fork the project and run the commands npm install and npm test on your local clone. Make your edits and rerun the tests. Pull requests welcome.
About
🦋 Pretty-print JSON data into HTML to indent and colorize (written in TypeScript)
Источник
docker inspect -f json pretty print #110
Comments
gtseres commented Sep 22, 2017 •
|
Expected behavior
When executing docker inspect -f=’< >>’ for containers, networks, etc., the json object should be formatted just like in the case where we don’t use the -f option; that is, pretty-printed, e.g.: The JSON object is not pretty printed: I could not find any option for that either in the available template functions of inspect , or in the inspect command help: Run docker inspect for an existing container or network, using the -f option: Output of docker version : Output of docker info : Additional environment details (AWS, VirtualBox, physical, etc.) The text was updated successfully, but these errors were encountered: work around (any hint for myself): pipe the output to a json-pretty-printer I like the current output on one line for docker ls commands (docker stack ls, docker container ls, docker container ps, . ). There is jq to which you can pipe your output. ej. like this below: This was discussed at some point, and decided not to add in order to not overload the options for —format , and because there’s great tools, such as jq mentioned above that allow formatting, colorising, and more manipulation of JSON output. Closing this for now, but feel free to continue the conversation You can’t perform that action at this time. You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. Источник JSON (JavaScript Object Notation) is a widely used format to store the data. It is used to exchange data between servers and software applications, i.e., web application, mobile application, etc. Python provides a built-in JSON module to perform JSON related tasks. The Python object (i.e., list, dictionary, tuple) can be converted into JSON. We use the dumps() function from the JSON module to perform this conversion. Whenever the Python object is converted into a JSON, and if we print the converted JSON object, results will be displayed the same as the Python dictionary. The JSON pretty print refers to display the JSON object in a good format and presentable way. This article explains the JSON pretty print concept with the help of examples. As discussed previously, the json.dump() is a built-in Python function that converts the Python object into JSON format. Let’s convert a Python dictionary object into JSON format. Output The Python dictionary object is successfully converted to the JSON format. The output seems like a Python dictionary. It is not in a good JSON format and presentable way. It is not prettified. The “indent” property is used inside the json.dumps() function to present the JSON data in a proper presentable format with space. Let’s use the indent property with the json.dumps() function. The “indent=1” adds the one space in JSON data. Output Now the output is prettified, and JSON data is presented in the proper format. As we keep increasing the number of indents, the spaces will increase in the data. #importing json module #creating a Python dictionary object Output Output Now, let’s try to open a JSON file and display it in pretty print format. The json.loads() function parse the JSON data. Output JSON is a widely used data format to store data and exchange data between servers and software applications. Python has a built-in JSON module to perform JSON related tasks. The JSON pretty print displays the JSON output in a well-formed and presentable way. This article explains the Python JSON pretty print with explains. I am a software engineer and a research scholar. I like to write article and make tutorial on various IT topics including Python, Cloud Computing, Fog Computing and Deep Learning. I love to use Linux based operating systems. ИсточникActual behavior
Steps to reproduce the behavior
Running in a Virtualbox Ubuntu 16.04.3 LTS relesaeSimonHeimberg commented Dec 7, 2018
mikzuit commented Mar 4, 2020
thaJeztah commented May 29, 2020
Python JSON pretty print
The json.dump() function
import json
my_dict = < "name" : "David" , "age" : 30 , "email" : "[email protected]" , "coutry" : "USA" >
#converting to JSON format
result_json = json. dumps ( my_dict , indent = 5 )
#printing the converted json object
print ( result_json )Python pretty print JSON file
Conclusion
About the author
Kamran Sattar Awaisi