Scroll to navigation

jc(1) JSON Convert jc(1)

NAME

jc - JSON Convert JSONifies the output of many CLI tools and file-types

SYNOPSIS

COMMAND | jc PARSER [OPTIONS]

or "Magic" syntax:

jc [OPTIONS] COMMAND

DESCRIPTION

jc JSONifies the output of many CLI tools and file-types for easier parsing in scripts. jc accepts piped input from STDIN and outputs a JSON representation of the previous command's output to STDOUT. Alternatively, the "Magic" syntax can be used by prepending jc to the command to be converted. Options can be passed to jc immediately before the command is given. (Note: "Magic" syntax does not support shell builtins or command aliases)

OPTIONS

Parsers:

`acpi` command parser

`airport -I` command parser

`airport -s` command parser

`arp` command parser

ASCII and Unicode table parser

multi-line ASCII and Unicode table parser

`blkid` command parser

`chage --list` command parser

`cksum` and `sum` command parser

`crontab` command and file parser

`crontab` file parser with user support

CSV file parser

CSV file streaming parser

`date` command parser

`df` command parser

`dig` command parser

`dir` command parser

`dmidecode` command parser

`dpkg -l` command parser

`du` command parser

`env` command parser

`file` command parser

`finger` command parser

`free` command parser

`/etc/fstab` file parser

`git log` command parser

`git log` command streaming parser

`/etc/group` file parser

`/etc/gshadow` file parser

`hash` command parser

hashsum command parser (`md5sum`, `shasum`, etc.)

`hciconfig` command parser

`history` command parser

`/etc/hosts` file parser

`id` command parser

`ifconfig` command parser

INI file parser

`iostat` command parser

`iostat` command streaming parser

`iptables` command parser

`iw dev [device] scan` command parser

MANIFEST.MF file parser

`jobs` command parser

Key/Value file parser

`last` and `lastb` command parser

`ls` command parser

`ls` command streaming parser

`lsblk` command parser

`lsmod` command parser

`lsof` command parser

`lsusb` command parser

`mount` command parser

`mpstat` command parser

`mpstat` command streaming parser

`netstat` command parser

`nmcli` command parser

`ntpq -p` command parser

`/etc/passwd` file parser

`pidstat -h` command parser

`pidstat -h` command streaming parser

`ping` and `ping6` command parser

`ping` and `ping6` command streaming parser

`pip list` command parser

`pip show` command parser

`ps` command parser

`route` command parser

`rpm -qi` command parser

`rsync` command parser

`rsync` command streaming parser

`sfdisk` command parser

`/etc/shadow` file parser

`ss` command parser

`stat` command parser

`stat` command streaming parser

`sysctl` command parser

`systemctl` command parser

`systemctl list-jobs` command parser

`systemctl list-sockets` command parser

`systemctl list-unit-files` command parser

`systeminfo` command parser

`/usr/bin/time` command parser

`timedatectl status` command parser

`top -b` command parser

`top -b` command streaming parser

`tracepath` and `tracepath6` command parser

`traceroute` and `traceroute6` command parser

`ufw status` command parser

`ufw app info [application]` command parser

`uname -a` command parser

`update-alternatives --get-selections` command parser

`update-alternatives --query` command parser

`upower` command parser

`uptime` command parser

`vmstat` command parser

`vmstat` command streaming parser

`w` command parser

`wc` command parser

`who` command parser

XML file parser

`xrandr` command parser

YAML file parser

`zipinfo` command parser

Options:

about jc (JSON or YAML output)
force color output even when using pipes (overrides -m and the NO_COLOR env variable)
debug - show traceback (use -dd for verbose traceback)
help (-h --parser_name for parser documentation)
monochrome output
pretty print output
quiet - suppress warnings (use -qq to ignore streaming parser errors)
raw JSON output
unbuffer output (useful for slow streaming data with streaming parsers)
version information
YAML output

EXIT CODES

Any fatal errors within jc will generate an exit code of 100, otherwise the exit code will be 0. When using the "Magic" syntax (e.g. jc ifconfig eth0), jc will store the exit code of the program being parsed and add it to the jc exit code. This way it is easier to determine if an error was from the parsed program or jc.

Consider the following examples using ifconfig:

ifconfig exit code = 0, jc exit code = 0, combined exit code = 0 (no errors)

ifconfig exit code = 1, jc exit code = 0, combined exit code = 1 (error in ifconfig)

ifconfig exit code = 0, jc exit code = 100, combined exit code = 100 (error in jc)

ifconfig exit code = 1, jc exit code = 100, combined exit code = 101 (error in both ifconfig and jc)

ENVIRONMENT

Custom Colors

You can specify custom colors via the JC_COLORS environment variable. The JC_COLORS environment variable takes four comma separated string values in the following format:

JC_COLORS=<keyname_color>,<keyword_color>,<number_color>,<string_color>

Where colors are: black, red, green, yellow, blue, magenta, cyan, gray, brightblack, brightred, brightgreen, brightyellow, brightblue, brightmagenta, brightcyan, white, or default

For example, to set to the default colors:

JC_COLORS=blue,brightblack,magenta,green

or

JC_COLORS=default,default,default,default

Disable Color Output

You can set the NO_COLOR environment variable to any value to disable color output in jc. Note that using the -C option to force color output will override both the NO_COLOR environment variable and the -m option.

STREAMING PARSERS

Most parsers load all of the data from STDIN, parse it, then output the entire JSON document serially. There are some streaming parsers (e.g. ls-s, ping-s, etc.) that immediately start processing and outputing the data line-by-line as JSON Lines (aka NDJSON) while it is being received from STDIN. This can significantly reduce the amount of memory required to parse large amounts of command output (e.g. ls -lR /) and can sometimes process the data more quickly. Streaming parsers have slightly different behavior than standard parsers as outlined below.

Note: Streaming parsers cannot be used with the "magic" syntax

Ignoring Errors

You may want to ignore parsing errors when using streaming parsers since these may be used in long-lived processing pipelines and errors can break the pipe. To ignore parsing errors, use the -qq cli option. This will add a _jc_meta object to the JSON output with a success attribute. If success is true, then there were no issues parsing the line. If success is false, then a parsing issue was found and error and line fields will be added to include a short error description and the contents of the unparsable line, respectively:

Successfully parsed line with -qq option:
{

"command_data": "data",
"_jc_meta": {
"success": true
} }

Unsuccessfully parsed line with -qq option:

{

"_jc_meta": {
"success": false,
"error": "error message",
"line": "original line data"
} }

Unbuffering Output

Most operating systems will buffer output that is being piped from process to process. The buffer is usually around 4KB. When viewing the output in the terminal the OS buffer is not engaged so output is immediately displayed on the screen. When piping multiple processes together, though, it may seem as if the output is hanging when the input data is very slow (e.g. ping):

$ ping 1.1.1.1 | jc --ping-s | jq
<slow output>

This is because the OS engages the 4KB buffer between jc and jq in this example. To display the data on the terminal in realtime, you can disable the buffer with the -u (unbuffer) cli option:

$ ping 1.1.1.1 | jc --ping-s -u | jq
{"type":"reply","pattern":null,"timestamp":null,"bytes":"64",...}
{"type":"reply","pattern":null,"timestamp":null,"bytes":"64",...}
etc...

Note: Unbuffered output can be slower for large data streams.

CUSTOM PARSERS

Custom local parser plugins may be placed in a jc/jcparsers folder in your local "App data directory":

- Linux/unix: $HOME/.local/share/jc/jcparsers
- macOS: $HOME/Library/Application Support/jc/jcparsers
- Windows: $LOCALAPPDATA\jc\jc\jcparsers

Local parser plugins are standard python module files. Use the jc/parsers/foo.py or jc/parsers/foo_s.py (streaming) parser as a template and simply place a .py file in the jcparsers subfolder.

Local plugin filenames must be valid python module names and therefore must start with a letter and consist entirely of alphanumerics and underscores. Local plugins may override default parsers.

Note: The application data directory follows the XDG Base Directory Specification

CAVEATS

Locale

For best results set the LANG locale environment variable to C or en_US.UTF-8. For example, either by setting directly on the command-line:

$ LANG=C date | jc --date

or by exporting to the environment before running commands:

$ export LANG=C

On some older systems UTF-8 output will be downgraded to ASCII with \u escape sequences if the C locale does not support UTF-8 encoding.

Timezones

Some parsers have calculated epoch timestamp fields added to the output. Unless a timestamp field name has a _utc suffix it is considered naive. (i.e. based on the local timezone of the system the jc parser was run on).

If a UTC timezone can be detected in the text of the command output, the timestamp will be timezone aware and have a _utc suffix on the key name. (e.g. epoch_utc) No other timezones are supported for aware timestamps.

EXAMPLES

Standard Syntax:

$ dig www.google.com | jc --dig -p

Magic Syntax:

$ jc -p dig www.google.com

For parser documentation:

$ jc -h --dig

AUTHOR

Kelly Brazil (kellyjonbrazil@gmail.com)

https://github.com/kellyjonbrazil/jc

COPYRIGHT

Copyright (c) 2019-2022 Kelly Brazil

License: MIT License

2022-05-31 1.20.0