jc: JSON Convert CLI Tool and Python Library
jc is a CLI tool and Python library that converts the output of popular command-line tools and file-types to JSON, YAML, or Python dictionaries. This allows for easier parsing in scripts and seamless integration with tools like `jq`. The library is actively maintained with frequent releases, typically on a monthly or bi-monthly cadence.
Warnings
- breaking The `iso-datetime` parser was removed. Use `datetime-iso` instead.
- gotcha The `size` fields in `df` and `free` parsers were changed to integers and normalized to bytes, regardless of whether human-readable output (`-h`) was used in the original command. Previously, they might have been strings with units.
- gotcha Attempting to use parsers that rely on optional third-party libraries (e.g., `xmltodict` for XML parsing) without those libraries installed will result in an `ImportError` or a crash.
- gotcha The `--slurp` functionality (or `slurp=True` in `jc.parse()`) only works with parsers designed for single-line inputs, not with streaming parsers. Using it with unsupported parsers will not yield the expected results.
Install
-
pip install jc
Imports
- jc
import jc
- jc.parsers.<parser_name>
import jc.parsers.dig
Quickstart
import subprocess
import jc
# Example: Parse 'dig example.com' output
cmd_output = subprocess.check_output(['dig', 'example.com'], text=True)
data = jc.parse('dig', cmd_output)
# The result is a Python list of dictionaries
print(data[0]['answer'])
# Example: Parse 'df' output
df_output = subprocess.check_output(['df', '-h'], text=True)
df_data = jc.parse('df', df_output)
print(df_data[0]['filesystem'])