.TH jc 1 {{ today }} {{ jc.version}} "JSON Convert"
.SH NAME
\fBjc\fP \- JSON Convert JSONifies the output of many CLI tools, file-types,
and strings
.SH SYNOPSIS

Standard syntax:

.RS
COMMAND | \fBjc\fP [SLICE] [OPTIONS] PARSER

cat FILE | \fBjc\fP [SLICE] [OPTIONS] PARSER

echo STRING | \fBjc\fP [SLICE] [OPTIONS] PARSER
.RE

Magic syntax:

.RS
\fBjc\fP [SLICE] [OPTIONS] COMMAND

\fBjc\fP [SLICE] [OPTIONS] /proc/<path-to-procfile>
.RE

.SH DESCRIPTION
\fBjc\fP JSONifies the output of many CLI tools, file-types, and common strings
for easier parsing in scripts. It reads command output, a file, or a string from
\fBSTDIN\fP and writes JSON to \fBSTDOUT\fP. With the "magic" syntax, put
\fBjc\fP and its options in front of the command or \fB/proc\fP file path
instead (command aliases and shell builtins are not supported).

.SH OPTIONS
.B
Parsers:
.RS

{% for parser in jc.parsers %}
.TP
.B
\fB{{ parser.argument }}\fP
{{ parser.description }}
{% endfor %}

.RE
.PP
.B
Options:
.RS

.TP
.B
\fB-a\fP, \fB--about\fP
About \fBjc\fP (JSON or YAML output)
.TP
.B
\fB-C\fP, \fB--force-color\fP
Force color output even when using pipes (overrides \fB-m\fP and the
\fBNO_COLOR\fP env variable)
.TP
.B
\fB-d\fP, \fB--debug\fP
Debug - show traceback (use \fB-dd\fP for verbose traceback)
.TP
.B
\fB-h\fP, \fB--help\fP
Help (\fB--help --parser_name\fP for parser documentation). Use twice to show
hidden parsers (e.g. \fB-hh\fP). Use thrice to show parser categories (e.g. \fB-hhh\fP).
.TP
.B
\fB-m\fP, \fB--monochrome\fP
Monochrome output
.TP
.B
\fB-M\fP, \fB--meta-out\fP
Add metadata to output including timestamp, parser name, magic command, magic
command exit code, etc.
.TP
.B
\fB-p\fP, \fB--pretty\fP
Pretty print output
.TP
.B
\fB-q\fP, \fB--quiet\fP
Quiet mode. Suppresses parser warning messages (use \fB-qq\fP to ignore streaming
parser errors)
.TP
.B
\fB-r\fP, \fB--raw\fP
Raw output. Provides more literal output, typically with string values and no
additional semantic processing
.TP
.B
\fB-s\fP, \fB--slurp\fP
Slurp multiple lines into an array. (use \fB-hhh\fP to find compatible parsers)
.TP
.B
\fB-u\fP, \fB--unbuffer\fP
Unbuffer output (useful for slow streaming data with streaming parsers)
.TP
.B
\fB-v\fP, \fB--version\fP
Version information
.TP
.B
\fB-y\fP, \fB--yaml-out\fP
YAML output
.TP
.B
\fB-B\fP, \fB--bash-comp\fP
Generate Bash shell completion script
.TP
.B
\fB-Z\fP, \fB--zsh-comp\fP
Generate Zsh shell completion script
.TP
.B
\fB-X\fP, \fB--xonsh-comp\fP
Generate Xonsh shell completion script

.RE
.PP
.B
Slice:
.RS
A \fBSTART:STOP\fP line slice selects which lines of the input to convert, like
Python slicing. Lines are numbered from \fB0\fP at the start or from \fB-1\fP at
the end, and \fBSTOP\fP is not included. Lines are counted at LF and CRLF line
endings; a lone CR does not start a new line. Slices with negative numbers use
more memory.

For example, both of these slices skip the first and last lines:

.RS
.nf
$ 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"}]
.fi
.RE

Line slice options:

.TP
.B
\fBSTART:STOP\fP
lines \fBSTART\fP through \fBSTOP - 1\fP
.TP
.B
\fBSTART:\fP
lines \fBSTART\fP through the rest of the output
.TP
.B
\fB:STOP\fP
lines from the beginning through \fBSTOP - 1\fP
.TP
.B
\fB-START:STOP\fP
\fBSTART\fP lines from the end through \fBSTOP - 1\fP
.TP
.B
\fBSTART:-STOP\fP
lines \fBSTART\fP through \fBSTOP\fP lines from the end
.TP
.B
\fB-START:-STOP\fP
\fBSTART\fP lines from the end through \fBSTOP\fP lines from the end
.TP
.B
\fB-START:\fP
\fBSTART\fP lines from the end through the rest of the output
.TP
.B
\fB:-STOP\fP
lines from the beginning through \fBSTOP\fP lines from the end
.TP
.B
\fB:\fP
all lines

.SH SLURP
String parsers that read a single line (e.g. \fBurl\fP and \fBip-address\fP)
can read many lines with the \fB--slurp\fP option and output an array of
results. Use \fBjc -hhh\fP to see which parsers support it.

.RS
.nf
$ cat ip-addresses.txt | jc --slurp --ip-address
[<multiple output objects>]
.fi
.RE

The \fB/proc\fP magic syntax slurps multiple files automatically and adds a
\fB_file\fP field to each output object:

.RS
.nf
$ jc /proc/*/status
[<multiple output objects>]
.fi
.RE

With \fB--meta-out\fP, slurped output is wrapped in an object, and
\fBinput_list\fP holds the input strings or \fB/proc\fP filenames so you can
match each result to its input:

.RS
.nf
{
  "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"
    ]
  }
}
.fi
.RE

.SH EXIT CODES
\fBjc\fP exits with \fB100\fP on a fatal error, otherwise \fB0\fP. With the
"magic" syntax (e.g. \fBjc ifconfig eth0\fP), the exit code of the parsed
program is added to the \fBjc\fP exit code, so you can tell where an error came
from:

.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

The \fB-M\fP (\fB--meta-out\fP) option also adds the program's exit code to the
output in the \fB_jc_meta\fP object:
.RS
.nf
$ 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
.fi
.RE

.SH ENVIRONMENT

\fBJC_COLORS\fP

Set custom colors as four comma-separated values:

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

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, the default
colors are:

.RS
JC_COLORS=blue,brightblack,magenta,green
.RE

This is the same as \fBJC_COLORS=default,default,default,default\fP.

\fBNO_COLOR\fP

Set to any value to disable colors. The \fB-C\fP option forces color output
and overrides both \fBNO_COLOR\fP and \fB-m\fP.

.SH STREAMING PARSERS
Streaming parsers (e.g. \fBls-s\fP and \fBping-s\fP) output JSON Lines as the
input arrives instead of waiting for all of it. This uses much less memory on
large inputs (e.g. \fBls -lR /\fP).

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

\fBIgnoring Errors\fP

To keep a long-running pipeline going when a line can't be parsed, use the
\fB-qq\fP option. Each output object then gets a \fB_jc_meta\fP object with a
\fBsuccess\fP field. Lines that failed also get \fBerror\fP and \fBline\fP
fields.

.RS
Successfully parsed line with \fB-qq\fP option:
.RS
.nf
{
  "command_data": "data",
  "_jc_meta": {
    "success": true
  }
}
.fi
.RE

Unsuccessfully parsed line with \fB-qq\fP option:
.RS
.nf
{
  "_jc_meta": {
    "success": false,
    "error": "error message",
    "line": "original line data"
  }
}
.fi
.RE

.RE
\fBUnbuffering Output\fP

When \fBjc\fP output is piped to another program, the OS buffers it (usually
about 4KB), so slow input like \fBping\fP can look stuck. Use the \fB-u\fP
option to unbuffer the output:

.RS
.nf
$ ping 1.1.1.1 | jc \fB--ping-s\fP \fB-u\fP | 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 PARSER PLUGINS
Parser plugins go in the \fBjcparsers\fP folder of your local app data
directory:

.RS
.nf
- 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
.fi
.RE

A plugin is a standard Python module. Use \fBjc/parsers/foo.py\fP or
\fBjc/parsers/foo_s.py\fP (streaming) as a template. Plugin filenames must be
valid Python module names (starting with a letter, and only letters, digits,
and underscores). Dependencies can be placed in the \fBjc\fP folder above
\fBjcparsers\fP. Local plugins may override built-in parsers.

Note: The application data directory follows the \fBXDG Base Directory
Specification\fP

.SH CAVEATS
\fBLocale\fP

For best results set the locale environment variables to \fBC\fP or
\fBen_US.UTF-8\fP by modifying the \fBLC_ALL\fP variable:

.RS
$ LC_ALL=C date | jc \fB--date\fP
.RE

You can also set the locale variables individually:

.RS
$ export LANG=C

$ export LC_NUMERIC=C
.RE

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

\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.

\fBLine Endings\fP

\fBjc\fP passes input to the parser without translating line endings, whether
it comes from \fBSTDIN\fP, a "magic" command, or a \fB/proc\fP file. All
line-based parsers split lines at LF and CRLF line endings only, so other characters that
can appear inside a value, such as a lone CR, form feed, or Unicode line
separator, stay part of that line. Parsers for file formats that can use CR-only line endings
(\fBcsv\fP, \fBhosts\fP, \fBjar-manifest\fP, and \fBm3u\fP) also accept a
lone CR as a line ending.

Streaming parsers split \fBSTDIN\fP lines the same way (LF and CRLF only).
Bytes in \fBSTDIN\fP that are not valid UTF-8 are shown as backslash escapes
(e.g. \fB\\xff\fP) instead of stopping the parser.

.SH EXAMPLES
Standard Syntax:
.RS
$ dig www.google.com | jc \fB-p\fP \fB--dig\fP

$ cat /proc/meminfo | jc \fB--pretty\fP \fB--proc\fP
.RE

Magic Syntax:
.RS
$ jc \fB--pretty\fP dig www.google.com

$ jc \fB--pretty\fP /proc/meminfo
.RE

Line Slicing:
.RS
$ cat file.csv | jc \fB:101\fP \fB--csv\fP    # parse first 100 lines
.RE

For parser documentation:
.RS
$ jc \fB--help\fP \fB--dig\fP
.RE

More Help:
.RS
$ jc \fB-hh\fP          # show hidden parsers

$ jc \fB-hhh\fP         # list parsers by category tags
.RE

.SH AUTHOR
{{ jc.author }} ({{ jc.author_email }})

{{ jc.website }}

.SH COPYRIGHT
Copyright (c) {{ jc.copyright[2:] }}

License:  {{ jc.license }}
