How to use the command jtbl (with examples)
The jtbl command is a utility that allows you to print JSON and JSON Lines data as a table in the terminal. It can be especially useful when you have JSON data that you want to view in a tabular format for easier readability.
Use case 1: Print a table from JSON or JSON Lines input
Code:
cat file.json | jtbl
Motivation: You may have a file containing JSON data and want to view it in a table format for better readability. The jtbl command allows you to easily convert the JSON data into a table that is easier to comprehend.
Explanation:
The command cat file.json
reads the contents of the file.json file and passes it as input to the jtbl command using the pipe (|
) operator. The jtbl command then converts the JSON data into a table and prints it in the terminal.
Example output:
+------+--------+---------+
| name | age | country |
+------+--------+---------+
| John | 25 | USA |
| Jane | 30 | Canada |
+------+--------+---------+
Use case 2: Print a table and specify the column width for wrapping
Code:
cat file.json | jtbl --cols=width
Motivation: Sometimes, the data in your JSON file may be too long to fit in a single table cell. By specifying the column width for wrapping, you can ensure that the data is displayed properly without any truncation.
Explanation:
The --cols
option is used to specify the column width for wrapping. In the example command, --cols=width
sets the column width for wrapping to the default width value. The jtbl command then converts the JSON data into a table, taking into account the specified column width for wrapping.
Example output:
+------+--------+---------+
| name | age | country |
+------+--------+---------+
| John | 25 | USA |
| Jane | 30 | Canada |
+------+--------+---------+
Use case 3: Print a table and truncate rows instead of wrapping
Code:
cat file.json | jtbl -t
Motivation: In some cases, you may want to truncate rows instead of wrapping them when they exceed the available column width. This can be useful when you want to limit the display size of the table but still have all the data visible.
Explanation:
The -t
option is used to truncate rows instead of wrapping them. In the example command, -t
is specified to enable row truncation. The jtbl command converts the JSON data into a table and truncates rows that exceed the available column width.
Example output:
+------+--------+---------+
| name | age | country |
+------+--------+---------+
| John | 25 | USA |
| Jane | ... | ... |
+------+--------+---------+
Use case 4: Print a table and don’t wrap or truncate rows
Code:
cat file.json | jtbl -n
Motivation: There may be cases where you want to view the full content of each cell in the table without any wrapping or truncation. This can be useful when the data in your JSON file contains important information that should not be hidden.
Explanation:
The -n
option is used to disable wrapping and truncation of rows. When the -n
option is used, the jtbl command converts the JSON data into a table without any wrapping or truncation of rows.
Example output:
+------+--------+---------+
| name | age | country |
+------+--------+---------+
| John | 25 | USA |
| Jane | 30 | Canada |
+------+--------+---------+
Conclusion:
The jtbl command is a powerful utility for converting JSON and JSON Lines data into a table format in the terminal. Whether you need to view your data in a table for better readability or want to control how the data is displayed, the jtbl command provides various options to suit your needs.