Man page - jc(1)
Packages contains this manual
Manual
jc
NAMESYNOPSIS
DESCRIPTION
OPTIONS
SLURP
EXIT CODES
ENVIRONMENT
STREAMING PARSERS
PARSER PLUGINS
CAVEATS
EXAMPLES
AUTHOR
COPYRIGHT
NAME
jc - JSON Convert JSONifies the output of many CLI tools, file-types, and strings
SYNOPSIS
Standard syntax:
COMMAND | jc [SLICE] [OPTIONS] PARSER
cat FILE | jc [SLICE] [OPTIONS] PARSER
echo STRING | jc [SLICE] [OPTIONS] PARSER
Magic syntax:
jc [SLICE] [OPTIONS] COMMAND
jc [SLICE] [OPTIONS] /proc/<path-to-procfile>
DESCRIPTION
jc JSONifies the output of many CLI tools, file-types, and common strings 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 |
âacpiâ command parser |
--airport
âairport -Iâ command parser
--airport-s
âairport -sâ command parser
--apt-cache-show
âapt-cache showâ command parser
--apt-get-sqq
âapt-get -sqqâ command parser
|
--arp |
âarpâ command parser |
--asciitable
ASCII and Unicode table parser
--asciitable-m
multi-line ASCII and Unicode table parser
--blkid
âblkidâ command parser
--bluetoothctl
âbluetoothctlâ command parser
|
--cbt |
âcbtâ (Google Bigtable) command parser |
|||
|
--cef |
CEF string parser |
--cef-s
CEF string streaming parser
--certbot
âcertbotâ command parser
--chage
âchage --listâ command parser
--cksum
âcksumâ and âsumâ command parser
|
--clf |
Common and Combined Log Format file parser |
--clf-s
Common and Combined Log Format file streaming parser
--crontab
âcrontabâ command and file parser
--crontab-u
âcrontabâ file parser with user support
|
--csv |
CSV file parser |
--csv-s
CSV file streaming parser
--curl-head
âcurl --headâ command parser
|
--date |
âdateâ command parser |
--datetime-iso
ISO 8601 Datetime string parser
--debconf-show
âdebconf-showâ command parser
|
--df |
âdfâ command parser |
|||
|
--dig |
âdigâ command parser |
|||
|
--dir |
âdirâ command parser |
--dmidecode
âdmidecodeâ command parser
--dpkg-l
âdpkg -lâ command parser
|
--du |
âduâ command parser |
--efibootmgr
âefibootmgrâ command parser
--email-address
Email Address string parser
|
--env |
âenvâ command parser |
--ethtool
âethtoolâ command parser
|
--file |
âfileâ command parser |
|||
|
--find |
âfindâ command parser |
--findmnt
âfindmntâ command parser
--finger
âfingerâ command parser
|
--free |
âfreeâ command parser |
--fstab
â/etc/fstabâ file parser
--git-log
âgit logâ command parser
--git-log-s
âgit logâ command streaming parser
--git-ls-remote
âgit ls-remoteâ command parser
|
--gpg |
âgpg --with-colonsâ command parser |
--group
â/etc/groupâ file parser
--gshadow
â/etc/gshadowâ file parser
|
--hash |
âhashâ command parser |
--hashsum
hashsum command parser (âmd5sumâ, âshasumâ, etc.)
--hciconfig
âhciconfigâ command parser
--history
âhistoryâ command parser
|
--host |
âhostâ command parser |
--hosts
â/etc/hostsâ file parser
--http-headers
HTTP headers parser
|
--id |
âidâ command parser |
--ifconfig
âifconfigâ command parser
|
--ini |
INI file parser |
--ini-dup
INI with duplicate key file parser
--iostat
âiostatâ command parser
--iostat-s
âiostatâ command streaming parser
--ip-address
IPv4 and IPv6 Address string parser
--ipconfig
âipconfigâ Windows command parser
--iptables
âiptablesâ command parser
--ip-route
âip routeâ command parser
--iw-scan
âiw dev [device] scanâ command parser
--iwconfig
âiwconfigâ command parser
--jar-manifest
Java MANIFEST.MF file parser
|
--jobs |
âjobsâ command parser |
|||
|
--jwt |
JWT string parser |
|||
|
--kv |
Key/Value file and string parser |
--kv-dup
Key/Value with duplicate key file and string parser
|
--last |
âlastâ and âlastbâ command parser |
|||
|
--ls |
âlsâ command parser |
|||
|
--ls-s |
âlsâ command streaming parser |
--lsattr
âlsattrâ command parser
--lsb-release
âlsb_releaseâ command parser
--lsblk
âlsblkâ command parser
--lsmod
âlsmodâ command parser
|
--lsof |
âlsofâ command parser |
--lspci
âlspci -mmvâ command parser
--lsusb
âlsusbâ command parser
|
--m3u |
M3U and M3U8 file parser |
--mdadm
âmdadmâ command parser
--mount
âmountâ command parser
--mpstat
âmpstatâ command parser
--mpstat-s
âmpstatâ command streaming parser
--needrestart
âneedrestart -bâ command parser
--netstat
ânetstatâ command parser
--nmcli
ânmcliâ command parser
--nsd-control
ânsd-controlâ command parser
|
--ntpq |
ântpq -pâ command parser |
--openvpn
openvpn-status.log file parser
--os-prober
âos-proberâ command parser
--os-release
â/etc/os-releaseâ file parser
--pacman
âpacmanâ command parser
--passwd
â/etc/passwdâ file parser
|
--path |
POSIX path string parser |
--path-list
POSIX path list string parser
--pci-ids
âpci.idsâ file parser
--pgpass
PostgreSQL password file parser
--pidstat
âpidstat -Hâ command parser
--pidstat-s
âpidstat -Hâ command streaming parser
|
--ping |
âpingâ and âping6â command parser |
--ping-s
âpingâ and âping6â command streaming parser
--pip-list
âpip listâ command parser
--pip-show
âpip showâ command parser
--pkg-index-apk
Alpine Linux Package Index file parser
--pkg-index-deb
Debian Package Index file parser
--plist
PLIST file parser
--postconf
âpostconf -Mâ command parser
|
--proc |
â/proc/â file parser |
--proc-buddyinfo
â/proc/buddyinfoâ file parser
--proc-cmdline
â/proc/cmdlineâ file parser
--proc-consoles
â/proc/consolesâ file parser
--proc-cpuinfo
â/proc/cpuinfoâ file parser
--proc-crypto
â/proc/cryptoâ file parser
--proc-devices
â/proc/devicesâ file parser
--proc-diskstats
â/proc/diskstatsâ file parser
--proc-filesystems
â/proc/filesystemsâ file parser
--proc-interrupts
â/proc/interruptsâ file parser
--proc-iomem
â/proc/iomemâ file parser
--proc-ioports
â/proc/ioportsâ file parser
--proc-loadavg
â/proc/loadavgâ file parser
--proc-locks
â/proc/locksâ file parser
--proc-meminfo
â/proc/meminfoâ file parser
--proc-modules
â/proc/modulesâ file parser
--proc-mtrr
â/proc/mtrrâ file parser
--proc-pagetypeinfo
â/proc/pagetypeinfoâ file parser
--proc-partitions
â/proc/partitionsâ file parser
--proc-slabinfo
â/proc/slabinfoâ file parser
--proc-softirqs
â/proc/softirqsâ file parser
--proc-stat
â/proc/statâ file parser
--proc-swaps
â/proc/swapsâ file parser
--proc-uptime
â/proc/uptimeâ file parser
--proc-version
â/proc/versionâ file parser
--proc-vmallocinfo
â/proc/vmallocinfoâ file parser
--proc-vmstat
â/proc/vmstatâ file parser
--proc-zoneinfo
â/proc/zoneinfoâ file parser
--proc-driver-rtc
â/proc/driver/rtcâ file parser
--proc-net-arp
â/proc/net/arpâ file parser
--proc-net-dev
â/proc/net/devâ file parser
--proc-net-dev-mcast
â/proc/net/dev_mcastâ file parser
--proc-net-if-inet6
â/proc/net/if_inet6â file parser
--proc-net-igmp
â/proc/net/igmpâ file parser
--proc-net-igmp6
â/proc/net/igmp6â file parser
--proc-net-ipv6-route
â/proc/net/ipv6_routeâ file parser
--proc-net-netlink
â/proc/net/netlinkâ file parser
--proc-net-netstat
â/proc/net/netstatâ file parser
--proc-net-packet
â/proc/net/packetâ file parser
--proc-net-protocols
â/proc/net/protocolsâ file parser
--proc-net-route
â/proc/net/routeâ file parser
--proc-net-tcp
â/proc/net/tcpâ and â/proc/net/tcp6â file parser
--proc-net-unix
â/proc/net/unixâ file parser
--proc-pid-fdinfo
â/proc/<pid>/fdinfo/<fd>â file parser
--proc-pid-io
â/proc/<pid>/ioâ file parser
--proc-pid-maps
â/proc/<pid>/mapsâ file parser
--proc-pid-mountinfo
â/proc/<pid>/mountinfoâ file parser
--proc-pid-numa-maps
â/proc/<pid>/numa_mapsâ file parser
--proc-pid-smaps
â/proc/<pid>/smapsâ file parser
--proc-pid-stat
â/proc/<pid>/statâ file parser
--proc-pid-statm
â/proc/<pid>/statmâ file parser
--proc-pid-status
â/proc/<pid>/statusâ file parser
|
--ps |
âpsâ command parser |
--resolve-conf
â/etc/resolve.confâ file parser
--route
ârouteâ command parser
--rpm-qi
ârpm -qiâ command parser
--rsync
ârsyncâ command parser
--rsync-s
ârsyncâ command streaming parser
--semver
Semantic Version string parser
--sfdisk
âsfdiskâ command parser
--shadow
â/etc/shadowâ file parser
|
--srt |
SRT file parser |
|||
|
--ss |
âssâ command parser |
--ssh-conf
âsshâ config file and âssh -Gâ command parser
--sshd-conf
âsshdâ config file and âsshd -Tâ command parser
|
--stat |
âstatâ command parser |
--stat-s
âstatâ command streaming parser
--swapon
âswaponâ command parser
--sysctl
âsysctlâ command parser
--syslog
Syslog RFC 5424 string parser
--syslog-s
Syslog RFC 5424 string streaming parser
--syslog-bsd
Syslog RFC 3164 string parser
--syslog-bsd-s
Syslog RFC 3164 string streaming parser
--systemctl
âsystemctlâ command parser
--systemctl-lj
âsystemctl list-jobsâ command parser
--systemctl-ls
âsystemctl list-socketsâ command parser
--systemctl-luf
âsystemctl list-unit-filesâ command parser
--systeminfo
âsysteminfoâ command parser
|
--time |
â/usr/bin/timeâ command parser |
--timedatectl
âtimedatectl statusâ command parser
--timestamp
Unix Epoch Timestamp string parser
|
--toml |
TOML file parser |
|||
|
--top |
âtop -bâ command parser |
--top-s
âtop -bâ command streaming parser
--tracepath
âtracepathâ and âtracepath6â command parser
--traceroute
âtracerouteâ and âtraceroute6â command parser
--tune2fs
âtune2fs -lâ command parser
--udevadm
âudevadm infoâ command parser
|
--ufw |
âufw statusâ command parser |
--ufw-appinfo
âufw app info [application]â command parser
--uname
âuname -aâ command parser
--update-alt-gs
âupdate-alternatives --get-selectionsâ command parser
--update-alt-q
âupdate-alternatives --queryâ command parser
--upower
âupowerâ command parser
--uptime
âuptimeâ command parser
|
--url |
URL string parser |
|||
|
--ver |
Version string parser |
--veracrypt
âveracryptâ command parser
--vmstat
âvmstatâ command parser
--vmstat-s
âvmstatâ command streaming parser
|
--w |
âwâ command parser |
|||
|
--wc |
âwcâ command parser |
--wg-show
âwg showâ command parser
|
--who |
âwhoâ command parser |
--x509-cert
X.509 PEM and DER certificate file parser
--x509-csr
X.509 PEM and DER certificate request file parser
|
--xml |
XML file parser |
--xrandr
âxrandrâ command parser
|
--yaml |
YAML file parser |
--zipinfo
âzipinfoâ command parser
--zpool-iostat
âzpool iostatâ command parser
--zpool-status
âzpool statusâ command parser
Options:
-a, --about
About jc (JSON or YAML output)
-C, --force-color
Force color output even when using pipes (overrides -m and the NO_COLOR env variable)
-d, --debug
Debug - show traceback (use -dd for verbose traceback)
-h, --help
Help ( --help --parser_name for parser documentation). Use twice to show hidden parsers (e.g. -hh ). Use thrice to show parser categories (e.g. -hhh ).
-m, --monochrome
Monochrome output
-M, --meta-out
Add metadata to output including timestamp, parser name, magic command, magic command exit code, etc.
-p, --pretty
Pretty print output
-q, --quiet
Quiet mode. Suppresses parser warning messages (use -qq to ignore streaming parser errors)
-r, --raw
Raw output. Provides more literal output, typically with string values and no additional semantic processing
-s, --slurp
Slurp multiple lines into an array. (use -hhh to find compatible parsers)
-u, --unbuffer
Unbuffer output (useful for slow streaming data with streaming parsers)
-v, --version
Version information
-y, --yaml-out
YAML output
-B, --bash-comp
Generate Bash shell completion script
-Z, --zsh-comp
Generate Zsh shell completion script
Slice:
Line slicing is supported using the START:STOP syntax similar to Python slicing. This allows you to skip lines at the beginning and/or end of the STDIN input you would like jc to convert.
START and STOP can be positive or negative integers or blank and allow you to specify how many lines to skip and how many lines to process. Positive and blank slices are the most memory efficient. Any negative integers in the slice will use more memory.
For example, to skip the first and last line of the following text, you could express the slice in a couple ways:
$ cat table.txt
### We want to skip this header ###
col1 col2
foo 1
bar 2
### We want to skip this footer ###
$ cat table.txt | jc 1:-1 --asciitable
[{"col1":"foo","col2":"1"},{"col1":"bar","col2":"2"}]
$ cat table.txt | jc 1:4 --asciitable
[{"col1":"foo","col2":"1"},{"col1":"bar","col2":"2"}]
In this example 1:-1 and 1:4 line slices provide the same output.
When using positive integers the index location of STOP is non-inclusive. Positive slices count from the first line of the input toward the end starting at 0 as the first line. Negative slices count from the last line toward the beginning starting at -1 as the last line. This is also the way Pythonâs slicing feature works.
Here is a
breakdown of line slice options:
START:STOP
lines START through STOP - 1
|
START: |
lines START through the rest of the output |
|||
|
:STOP |
lines from the beginning through STOP - 1 |
-START:STOP
START lines from the end through STOP - 1
START:-STOP
lines START through STOP lines from the end
-START:-STOP
START lines from the end through STOP lines from the end
-START:
START lines from the end through the rest of the output
|
:-STOP |
lines from the beginning through STOP lines from the end |
|||
|
: |
all lines |
SLURP
Some parsers support multi-item input and can output an array of results in a single pass. Slurping works for string parsers that accept a single line of input. (e.g. url and ip-address ) To see a list of parsers that support the --slurp option, use jc -hhh .
For example, you can send a file with multiple IP addresses (one per line) to jc with the --slurp option and an array of results will output:
$ cat
ip-addresses.txt | jc --slurp --ip-address
[<multiple output objects>]
The magic syntax for /proc files automatically supports slurping of multiple files (no need to use the --slurp option). For example, you can convert many PID files at once:
$ jc
/proc/*/status
[<multiple output objects>]
When the /proc magic syntax is used and multiple files are selected, an additional _file field is inserted in the output so it is easier to tell what file each output object refers to.
Finally, the --meta-out option can be used in conjunction with slurped output. In this case, the slurped output is wrapped in an object with the following structure:
{
"result": [<multiple output objects>],
"_jc_meta": {
"parser": "url",
"timestamp": 1706235558.654576,
"slice_start": null,
"slice_end": null,
"input_list": [
"http://www.google.com",
"https://www.apple.com",
"https://www.microsoft.com"
]
}
}
With --meta-out , input_list contains a list of inputs (actual input strings or /proc filenames) so you can identify which output object relates to each input string or /proc filename.
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)
When using the "magic" syntax you can also retrieve the exit code of the called program by using the --meta-out or -M option. This will append a _jc_meta object to the output that will include the magic command information, including the exit code.
Here is an example with ping :
$ jc --meta-out -p ping -c2
192.168.1.252
{
"destination_ip": "192.168.1.252",
"data_bytes": 56,
"pattern": null,
"destination": "192.168.1.252",
"packets_transmitted": 2,
"packets_received": 0,
"packet_loss_percent": 100.0,
"duplicates": 0,
"responses": [
{
"type": "timeout",
"icmp_seq": 0,
"duplicate": false
}
],
"_jc_meta": {
"parser": "ping",
"timestamp": 1661357115.27949,
"magic_command": [
"ping",
"-c2",
"192.168.1.252"
],
"magic_command_exit": 2
}
}
$ echo $?
2
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 outputting 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.
PARSER PLUGINS
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
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. Any dependencies can be placed in the jc folder above jcparsers and can be imported in the parser code.
Parser 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 locale environment variables to C or en_US.UTF-8 by modifying the LC_ALL variable:
$ LC_ALL=C date | jc --date
You can also set the locale variables individually:
$ export LANG=C
$ export LC_NUMERIC=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 -p --dig
$ cat /proc/meminfo | jc --pretty --proc
Magic Syntax:
$ jc --pretty dig www.google.com
$ jc --pretty /proc/meminfo
Line Slicing:
$ cat file.csv | jc :101 --csv # parse first 100 lines
For parser documentation:
$ jc --help --dig
More Help:
$ jc -hh # show hidden parsers
$ jc -hhh # list parsers by category tags
AUTHOR
Kelly Brazil (kellyjonbrazil@gmail.com)
https://github.com/kellyjonbrazil/jc
COPYRIGHT
Copyright (c) 2019-2024 Kelly Brazil
License: MIT License