.TH jc 1 2022-03-05 1.18.4 "JSON Convert" .SH NAME jc \- JSONifies the output of many CLI tools and file-types .SH SYNOPSIS COMMAND | jc PARSER [OPTIONS] or "Magic" syntax: jc [OPTIONS] COMMAND .SH DESCRIPTION jc JSONifies the output of many CLI tools and file-types for easier parsing in scripts. jc accepts piped input from \fBSTDIN\fP and outputs a JSON representation of the previous command's output to \fBSTDOUT\fP. 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) .SH OPTIONS .B Parsers: .RS .TP .B \fB--acpi\fP `acpi` command parser .TP .B \fB--airport\fP `airport -I` command parser .TP .B \fB--airport-s\fP `airport -s` command parser .TP .B \fB--arp\fP `arp` command parser .TP .B \fB--blkid\fP `blkid` command parser .TP .B \fB--cksum\fP `cksum` and `sum` command parser .TP .B \fB--crontab\fP `crontab` command and file parser .TP .B \fB--crontab-u\fP `crontab` file parser with user support .TP .B \fB--csv\fP CSV file parser .TP .B \fB--csv-s\fP CSV file streaming parser .TP .B \fB--date\fP `date` command parser .TP .B \fB--df\fP `df` command parser .TP .B \fB--dig\fP `dig` command parser .TP .B \fB--dir\fP `dir` command parser .TP .B \fB--dmidecode\fP `dmidecode` command parser .TP .B \fB--dpkg-l\fP `dpkg -l` command parser .TP .B \fB--du\fP `du` command parser .TP .B \fB--env\fP `env` command parser .TP .B \fB--file\fP `file` command parser .TP .B \fB--finger\fP `finger` command parser .TP .B \fB--free\fP `free` command parser .TP .B \fB--fstab\fP `/etc/fstab` file parser .TP .B \fB--group\fP `/etc/group` file parser .TP .B \fB--gshadow\fP `/etc/gshadow` file parser .TP .B \fB--hash\fP `hash` command parser .TP .B \fB--hashsum\fP hashsum command parser (`md5sum`, `shasum`, etc.) .TP .B \fB--hciconfig\fP `hciconfig` command parser .TP .B \fB--history\fP `history` command parser .TP .B \fB--hosts\fP `/etc/hosts` file parser .TP .B \fB--id\fP `id` command parser .TP .B \fB--ifconfig\fP `ifconfig` command parser .TP .B \fB--ini\fP INI file parser .TP .B \fB--iostat\fP `iostat` command parser .TP .B \fB--iostat-s\fP `iostat` command streaming parser .TP .B \fB--iptables\fP `iptables` command parser .TP .B \fB--iw-scan\fP `iw dev [device] scan` command parser .TP .B \fB--jar-manifest\fP MANIFEST.MF file parser .TP .B \fB--jobs\fP `jobs` command parser .TP .B \fB--kv\fP Key/Value file parser .TP .B \fB--last\fP `last` and `lastb` command parser .TP .B \fB--ls\fP `ls` command parser .TP .B \fB--ls-s\fP `ls` command streaming parser .TP .B \fB--lsblk\fP `lsblk` command parser .TP .B \fB--lsmod\fP `lsmod` command parser .TP .B \fB--lsof\fP `lsof` command parser .TP .B \fB--lsusb\fP `lsusb` command parser .TP .B \fB--mount\fP `mount` command parser .TP .B \fB--netstat\fP `netstat` command parser .TP .B \fB--nmcli\fP `nmcli` command parser .TP .B \fB--ntpq\fP `ntpq -p` command parser .TP .B \fB--passwd\fP `/etc/passwd` file parser .TP .B \fB--ping\fP `ping` and `ping6` command parser .TP .B \fB--ping-s\fP `ping` and `ping6` command streaming parser .TP .B \fB--pip-list\fP `pip list` command parser .TP .B \fB--pip-show\fP `pip show` command parser .TP .B \fB--ps\fP `ps` command parser .TP .B \fB--route\fP `route` command parser .TP .B \fB--rpm-qi\fP `rpm -qi` command parser .TP .B \fB--rsync\fP `rsync` command parser .TP .B \fB--rsync-s\fP `rsync` command streaming parser .TP .B \fB--sfdisk\fP `sfdisk` command parser .TP .B \fB--shadow\fP `/etc/shadow` file parser .TP .B \fB--ss\fP `ss` command parser .TP .B \fB--stat\fP `stat` command parser .TP .B \fB--stat-s\fP `stat` command streaming parser .TP .B \fB--sysctl\fP `sysctl` command parser .TP .B \fB--systemctl\fP `systemctl` command parser .TP .B \fB--systemctl-lj\fP `systemctl list-jobs` command parser .TP .B \fB--systemctl-ls\fP `systemctl list-sockets` command parser .TP .B \fB--systemctl-luf\fP `systemctl list-unit-files` command parser .TP .B \fB--systeminfo\fP `systeminfo` command parser .TP .B \fB--time\fP `/usr/bin/time` command parser .TP .B \fB--timedatectl\fP `timedatectl status` command parser .TP .B \fB--tracepath\fP `tracepath` and `tracepath6` command parser .TP .B \fB--traceroute\fP `traceroute` and `traceroute6` command parser .TP .B \fB--ufw\fP `ufw status` command parser .TP .B \fB--ufw-appinfo\fP `ufw app info [application]` command parser .TP .B \fB--uname\fP `uname -a` command parser .TP .B \fB--upower\fP `upower` command parser .TP .B \fB--uptime\fP `uptime` command parser .TP .B \fB--vmstat\fP `vmstat` command parser .TP .B \fB--vmstat-s\fP `vmstat` command streaming parser .TP .B \fB--w\fP `w` command parser .TP .B \fB--wc\fP `wc` command parser .TP .B \fB--who\fP `who` command parser .TP .B \fB--xml\fP XML file parser .TP .B \fB--xrandr\fP `xrandr` command parser .TP .B \fB--yaml\fP YAML file parser .TP .B \fB--zipinfo\fP `zipinfo` command parser .RE .PP .B Options: .RS .TP .B \fB-a\fP about jc (JSON output) .TP .B \fB-C\fP force color output even when using pipes (overrides \fB-m\fP and the \fBNO_COLOR\fP env variable) .TP .B \fB-d\fP debug - show traceback (use \fB-dd\fP for verbose traceback) .TP .B \fB-h\fP help (\fB-h --parser_name\fP for parser documentation) .TP .B \fB-m\fP monochrome output .TP .B \fB-p\fP pretty print output .TP .B \fB-q\fP quiet - suppress warnings (use \fB-qq\fP to ignore streaming parser errors) .TP .B \fB-r\fP raw JSON output .TP .B \fB-u\fP unbuffer output (useful for slow streaming data with streaming parsers) .TP .B \fB-v\fP version information .SH EXIT CODES Any fatal errors within jc will generate an exit code of \fB100\fP, otherwise the exit code will be \fB0\fP. When using the "Magic" syntax (e.g. \fBjc ifconfig eth0\fP), 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 \fBifconfig\fP: .RS ifconfig exit code = \fB0\fP, jc exit code = \fB0\fP, combined exit code = \fB0\fP (no errors) ifconfig exit code = \fB1\fP, jc exit code = \fB0\fP, combined exit code = \fB1\fP (error in ifconfig) ifconfig exit code = \fB0\fP, jc exit code = \fB100\fP, combined exit code = \fB100\fP (error in jc) ifconfig exit code = \fB1\fP, jc exit code = \fB100\fP, combined exit code = \fB101\fP (error in both ifconfig and jc) .RE .SH ENVIRONMENT \fBCustom Colors\fP You can specify custom colors via the \fBJC_COLORS\fP environment variable. The \fBJC_COLORS\fP environment variable takes four comma separated string values in the following format: JC_COLORS=,,, Where colors are: \fBblack\fP, \fBred\fP, \fBgreen\fP, \fByellow\fP, \fBblue\fP, \fBmagenta\fP, \fBcyan\fP, \fBgray\fP, \fBbrightblack\fP, \fBbrightred\fP, \fBbrightgreen\fP, \fBbrightyellow\fP, \fBbrightblue\fP, \fBbrightmagenta\fP, \fBbrightcyan\fP, \fBwhite\fP, or \fBdefault\fP For example, to set to the default colors: .RS JC_COLORS=blue,brightblack,magenta,green or JC_COLORS=default,default,default,default .RE \fBDisable Color Output\fP You can set the \fBNO_COLOR\fB environment variable to any value to disable color output in \fBjc\fP. Note that using the \fB-C\fP option to force color output will override both the \fBNO_COLOR\fP environment variable and the \fB-m\fP option. .SH STREAMING PARSERS Most parsers load all of the data from \fBSTDIN\fP, parse it, then output the entire JSON document serially. There are some streaming parsers (e.g. \fBls-s\fP and \fBping-s\fP) that immediately start processing and outputing the data line-by-line as JSON Lines (aka NDJSON) while it is being received from \fBSTDIN\fP. This can significantly reduce the amount of memory required to parse large amounts of command output (e.g. \fBls -lR /\fP) and can sometimes process the data more quickly. Streaming parsers have slightly different behavior than standard parsers as outlined below. .RS Note: Streaming parsers cannot be used with the "magic" syntax .RE \fBIgnoring Errors\fP 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 \fB-qq\fP cli option. This will add a \fB_jc_meta\fP object to the JSON output with a \fBsuccess\fP attribute. If \fBsuccess\fP is \fBtrue\fP, then there were no issues parsing the line. If \fBsuccess\fP is \fBfalse\fP, then a parsing issue was found and \fBerror\fP and \fBline\fP fields will be added to include a short error description and the contents of the unparsable line, respectively: .RS Successfully parsed line with \fB-qq\fP option: .RS .na .nf { "command_data": "data", "_jc_meta": { "success": true } } .RE Unsuccessfully parsed line with \fB-qq\fP option: .RS .na .nf { "_jc_meta": { "success": false, "error": "error message", "line": "original line data" } } .fi .RE .RE \fBUnbuffering Output\fP 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. \fBping\fP): .RS .na .nf $ ping 1.1.1.1 | jc --ping-s | jq .fi .RE This is because the OS engages the 4KB buffer between \fBjc\fP and \fBjq\fP in this example. To display the data on the terminal in realtime, you can disable the buffer with the \fB-u\fP (unbuffer) cli option: .RS .na .nf $ 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... .fi Note: Unbuffered output can be slower for large data streams. .RE .SH CUSTOM PARSERS Custom local parser plugins may be placed in a \fBjc/jcparsers\fP folder in your local "App data directory": .RS - Linux/unix: \fB$HOME/.local/share/jc/jcparsers\fP - macOS: \fB$HOME/Library/Application Support/jc/jcparsers\fP - Windows: \fB$LOCALAPPDATA\\jc\\jc\\jcparsers\fP .RE Local parser plugins are standard python module files. Use the \fBjc/parsers/foo.py\fP parser as a template and simply place a \fB.py\fP file in the \fBjcparsers\fP subfolder. Local plugin filenames must be valid python module names, therefore must consist entirely of alphanumerics and start with a letter. Local plugins may override default plugins. Note: The application data directory follows the XDG Base Directory Specification .SH CAVEATS \fBLocale:\fP For best results set the \fBLANG\fP locale environment variable to \fBC\fP or \fBen_US.UTF-8\fP. For example, either by setting directly on the command-line: \fB$ LANG=C date | jc --date\fP or by exporting to the environment before running commands: \fB$ export LANG=C\fP \fBTimezones:\fP Some parsers have calculated epoch timestamp fields added to the output. Unless a timestamp field name has a \fB_utc\fP suffix it is considered naive. (i.e. based on the local timezone of the system the \fBjc\fP 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 \fB_utc\fP suffix on the key name. (e.g. \fBepoch_utc\fP) No other timezones are supported for aware timestamps. .SH EXAMPLES Standard Syntax: .RS $ dig www.google.com | jc \fB--dig\fP \fB-p\fP .RE Magic Syntax: .RS $ jc \fB-p\fP dig www.google.com .RE For parser documentation: .RS $ jc \fB-h\fP \fB--dig\fP .RE .SH AUTHOR Kelly Brazil (kellyjonbrazil@gmail.com) https://github.com/kellyjonbrazil/jc .SH COPYRIGHT Copyright (c) 2019-2022 Kelly Brazil License: MIT License