.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. \fBjc\fP 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 \fBjc\fP to the
command to be converted. Options can be passed to \fBjc\fP immediately before
the command is given. (Note: "Magic" syntax does not support shell builtins or
command aliases)

.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)
.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 -qq 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-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

.RE
.PP
.B
Slice:
.RS
Line slicing is supported using the \fBSTART:STOP\fP syntax similar to Python
slicing. This allows you to skip lines at the beginning and/or end of the
\fBSTDIN\fP input you would like \fBjc\fP to convert.

\fBSTART\fP and \fBSTOP\fP 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:

.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

In this example \fB1:-1\fP and \fB1:4\fP line slices provide the same output.

When using positive integers the index location of \fBSTOP\fP is non-inclusive.
Positive slices count from the first line of the input toward the end
starting at \fB0\fP as the first line. Negative slices count from the last line
toward the beginning starting at \fB-1\fP as the last line. This is also the way
Python's slicing feature works.

Here is a breakdown of 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 EXIT CODES
Any fatal errors within \fBjc\fP 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), \fBjc\fP will store
the exit code of the program being parsed and add it to the \fBjc\fP exit code.
This way it is easier to determine if an error was from the parsed program or
\fBjc\fP.

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

When using the "magic" syntax you can also retrieve the exit code of the called
program by using the \fB--meta-out\fP or \fB-M\fP option. This will append a
\fB_jc_meta\fP object to the output that will include the magic command
information, including the exit code.

Here is an example with \fBping\fP:
.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

\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=<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, 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\fP 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, \fBping-s\fP, etc.) that immediately start processing and outputting
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
.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

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
.nf
$ ping 1.1.1.1 | jc \fB--ping-s\fP | jq
<slow output>
.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
.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 may be placed in a \fBjc/jcparsers\fP folder in 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

Parser plugins are standard python module files. Use the
\fBjc/parsers/foo.py\fP or \fBjc/parsers/foo_s.py\fP (streaming) parser as a
template and simply place a \fB.py\fP file in the \fBjcparsers\fP subfolder.
Any dependencies can be placed in the \fBjc\fP folder above \fBjcparsers\fP
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 \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.

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