Skip to main content

jtbl

A simple cli tool to print JSON data as a table in the terminal.

jtbl accepts piped JSON data from stdin and outputs a text table representation to stdout. e.g:

$ cat cities.json | jtbl 
  LatD    LatM    LatS  NS      LonD    LonM    LonS  EW    City               State
------  ------  ------  ----  ------  ------  ------  ----  -----------------  -------
    41       5      59  N         80      39       0  W     Youngstown         OH
    42      52      48  N         97      23      23  W     Yankton            SD
    46      35      59  N        120      30      36  W     Yakima             WA
    42      16      12  N         71      48       0  W     Worcester          MA
    43      37      48  N         89      46      11  W     Wisconsin Dells    WI
    36       5      59  N         80      15       0  W     Winston-Salem      NC
    49      52      48  N         97       9       0  W     Winnipeg           MB

jtbl expects a JSON array of JSON objects or JSON Lines.

It can be useful to JSONify command line output with jc, filter through jq, and present in jtbl:

$ jc ifconfig | jq -c '.[] | {name, type, ipv4_addr, ipv4_mask}'| jtbl 
name     type            ipv4_addr       ipv4_mask
-------  --------------  --------------  -------------
docker0  Ethernet        172.17.0.1      255.255.0.0
ens33    Ethernet        192.168.71.146  255.255.255.0
lo       Local Loopback  127.0.0.1       255.0.0.0

Notice the -c flag used in jq to produce valid JSON Lines output for jtbl consumption.

Installation

pip3 install --upgrade jtbl

Usage

Just pipe JSON data to jtbl. (e.g. cat a JSON file, jc, jq, aws cli, kubectl, etc.)

$ <JSON Source> | jtbl

Compatible JSON Formats

jtbl works best with a shallow array of JSON objects. Each object should have a few elements that will be turned into table columns. Fortunately, this is how many APIs present their data.

JSON Array Example

[
  {
    "unit": "proc-sys-fs-binfmt_misc.automount",
    "load": "loaded",
    "active": "active",
    "sub": "waiting",
    "description": "Arbitrary Executable File Formats File System Automount Point"
  },
  {
    "unit": "sys-devices-pci0000:00-0000:00:07.1-ata2-host2-target2:0:0-2:0:0:0-block-sr0.device",
    "load": "loaded",
    "active": "active",
    "sub": "plugged",
    "description": "VMware_Virtual_IDE_CDROM_Drive"
  },
  ...
]

jtbl can also work with JSON Lines format with similar features.

JSON Lines Example

{"name": "docker0", type": "Ethernet", "ipv4_addr": "172.17.0.1", "ipv4_mask": "255.255.0.0"}
{"name": "ens33", "type": "Ethernet", "ipv4_addr": "192.168.71.146", "ipv4_mask": "255.255.255.0"}
{"name": "lo", "type": "Local Loopback", "ipv4_addr": "127.0.0.1", "ipv4_mask": "255.0.0.0"}

Filtering the JSON Input

If there are too many elements, or the data in the elements are too large, the table may not fit in the terminal screen. In this case you can use a JSON filter like jq to send jtbl only the elements you are interested in:

The following example uses jq to filter and then again to 'slurp' the filtered elements into a proper JSON array.

jq Slurp Method

$ cat /etc/passwd | jc --passwd | jq '.[] | {username, shell}' | jq -s
[
  {
    "username": "root",
    "shell": "/bin/bash"
  },
  {
    "username": "bin",
    "shell": "/sbin/nologin"
  },
  {
    "username": "daemon",
    "shell": "/sbin/nologin"
  },
  ...
]

Or you can use the -c option in jq to send the data compact, which will effectively be JSON Lines format, which jtbl can understand:

jq JSON Lines Method

$ cat /etc/passwd | jc --passwd | jq -c '.[] | {username, shell}'
{"username":"root","shell":"/bin/bash"}
{"username":"bin","shell":"/sbin/nologin"}
{"username":"daemon","shell":"/sbin/nologin"}
...

When piping either of these to jtbl you get the following result:

username         shell
---------------  --------------
root             /bin/bash
bin              /sbin/nologin
daemon           /sbin/nologin
...

Working with Deeper JSON Structures

jtbl will happily dump deeply nested JSON structures into a table, but usually this is not what you are looking for.

$ jc dig www.cnn.com | jtbl 
   id  opcode    status    flags                query_num    answer_num    authority_num    additional_num  question           answer               query_time    server  when                 rcvd
-----  --------  --------  -----------------  -----------  ------------  ---------------  ----------------  -----------------  -----------------  ------------  --------  -----------------  ------
25618  QUERY     NOERROR   ['qr', 'rd', 'ra'            1             5                0                 1  {'name': 'www.cnn  [{'name': 'www.cn            34      2600  Fri Mar 06 07:25:     143
                           ]                                                                                .com.', 'class':   n.com.', 'class':                          23 PST 2020
                                                                                                            'IN', 'type': 'A'   'IN', 'type': 'C
                                                                                                            }                  NAME', 'ttl': 264
                                                                                                                               , 'data': 'turner
                                                                                                                               -tls.map.fastly.n
                                                                                                                               et.'}, {'name': '
                                                                                                                               turner-tls.map.fa
                                                                                                                               stly.net.', 'clas
                                                                                                                               s': 'IN', 'type':
                                                                                                                               ...

To get to the data you are interested in you can use a JSON filter like jq do dive deeper.

Diving Deeper into the JSON with jq

$ jc dig www.cnn.com | jq '.[].answer' 
[
  {
    "name": "www.cnn.com.",
    "class": "IN",
    "type": "CNAME",
    "ttl": 90,
    "data": "turner-tls.map.fastly.net."
  },
  {
    "name": "turner-tls.map.fastly.net.",
    "class": "IN",
    "type": "A",
    "ttl": 20,
    "data": "151.101.1.67"
  }
  ...
]

This will produce the following table in jtbl

name               class    type      ttl  data
-----------------  -------  ------  -----  -----------------
www.cnn.com.       IN       CNAME      72  turner-tls.map.fa
                                           stly.net.
turner-tls.map.fa  IN       A          17  151.101.1.67
stly.net.
turner-tls.map.fa  IN       A          17  151.101.193.67
stly.net.
turner-tls.map.fa  IN       A          17  151.101.65.67
stly.net.
turner-tls.map.fa  IN       A          17  151.101.129.67
stly.net.

Column Width

Today jtbl will always wrap each column at 17 characters. In the future, jtbl will be smarter and only wrap when necessary and also provide the option to truncate vs. wrap lines. Stay tuned!

Metadata

Release files for jtbl 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jtbl 0.1.2
File Size Uploaded Provenance Actions
jtbl-0.1.2.tar.gz 4.9 kB Without provenance Details for jtbl-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for jtbl 0.1.2
File Interpreter ABI Platform Actions
jtbl-0.1.2-py3-none-any.whl Python 3 none any Details for jtbl-0.1.2-py3-none-any.whl

Total release size: 10.4 kB

Release files / jtbl-0.1.2.tar.gz

Download URL jtbl-0.1.2.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
1c0bdef40cbaa519e15e383dfaf374e90f9da477bdd74b2e41e8e9dad24e3371
BLAKE2b-256 checksum
How to use checksums
44fc8512cc78ca4c852464cb2dcfd2cf27a537eebc7a64840ab1d815729ba2d7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.6

Release files / jtbl-0.1.2-py3-none-any.whl

Download URL jtbl-0.1.2-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d1f1e620ec54b740552d38e88b53e999c7c3975e6ed2782b571d3fac4817002f
BLAKE2b-256 checksum
How to use checksums
41b4e96e06d28f1909dfedb822a5bf4bd58109470ddc7ed7ba7b87a6d8123e11
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.6

Security for jtbl 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page