Converts the output of popular command-line tools and file-types to JSON.
—
High-level parsing functions and parser discovery utilities that provide the main interface for using jc programmatically. These functions handle parser loading, data parsing, and metadata retrieval.
The main high-level API function for parsing data using any available parser.
def parse(
parser_mod_name: Union[str, ModuleType],
data: Union[str, bytes, Iterable[str]],
quiet: bool = False,
raw: bool = False,
ignore_exceptions: Optional[bool] = None,
**kwargs
) -> Union[JSONDictType, List[JSONDictType], Iterator[JSONDictType]]:
"""
Parse data using the specified parser module.
Parameters:
- parser_mod_name: Name of parser module (accepts module_name, cli-name, --argument-name variants) or Module object
- data: Data to parse (string/bytes for standard parsers, iterable of strings for streaming parsers)
- quiet: Suppress warning messages if True
- raw: Output preprocessed JSON if True
- ignore_exceptions: Ignore parsing exceptions if True (streaming parsers only)
- **kwargs: Additional parser-specific arguments
Returns:
- Standard Parsers: Dictionary or List of Dictionaries
- Streaming Parsers: Generator Object containing Dictionaries
Raises:
- ModuleNotFoundError: If parser module is not found or invalid
"""Direct access to parser module objects for advanced usage patterns.
def get_parser(parser_mod_name: Union[str, ModuleType]) -> ModuleType:
"""
Return parser module object and validate it's a valid parser module.
Parameters:
- parser_mod_name: Name of parser module or Module object (accepts module_name, cli-name, --argument-name variants)
Returns:
- Module: The parser Module object
Raises:
- ModuleNotFoundError: If Module is not found or is not a valid parser Module
"""Functions to discover available parsers and their capabilities.
def parser_mod_list(
show_hidden: bool = False,
show_deprecated: bool = False
) -> List[str]:
"""
Returns a list of all available parser module names.
Parameters:
- show_hidden: Include parsers marked as hidden
- show_deprecated: Include parsers marked as deprecated
Returns:
- List of parser module names
"""
def plugin_parser_mod_list(
show_hidden: bool = False,
show_deprecated: bool = False
) -> List[str]:
"""
Returns a list of plugin parser module names (subset of parser_mod_list).
Parameters:
- show_hidden: Include parsers marked as hidden
- show_deprecated: Include parsers marked as deprecated
Returns:
- List of plugin parser module names
"""
def standard_parser_mod_list(
show_hidden: bool = False,
show_deprecated: bool = False
) -> List[str]:
"""
Returns a list of standard (non-streaming) parser module names.
Parameters:
- show_hidden: Include parsers marked as hidden
- show_deprecated: Include parsers marked as deprecated
Returns:
- List of standard parser module names
"""
def streaming_parser_mod_list(
show_hidden: bool = False,
show_deprecated: bool = False
) -> List[str]:
"""
Returns a list of streaming parser module names.
Parameters:
- show_hidden: Include parsers marked as hidden
- show_deprecated: Include parsers marked as deprecated
Returns:
- List of streaming parser module names
"""
def slurpable_parser_mod_list(
show_hidden: bool = False,
show_deprecated: bool = False
) -> List[str]:
"""
Returns a list of slurpable parser module names (can use --slurp option).
Parameters:
- show_hidden: Include parsers marked as hidden
- show_deprecated: Include parsers marked as deprecated
Returns:
- List of slurpable parser module names
"""Functions to retrieve detailed information about parsers.
def parser_info(
parser_mod_name: Union[str, ModuleType],
documentation: bool = False
) -> ParserInfoType:
"""
Returns dictionary with parser module metadata.
Parameters:
- parser_mod_name: Parser module name or Module object (accepts module_name, cli-name, --argument-name variants)
- documentation: Include parser docstring if True
Returns:
- Dictionary containing parser metadata including name, version, description, compatibility, tags, etc.
"""
def all_parser_info(
documentation: bool = False,
show_hidden: bool = False,
show_deprecated: bool = False
) -> List[ParserInfoType]:
"""
Returns list of dictionaries with metadata for all parser modules.
Parameters:
- documentation: Include parser docstrings if True
- show_hidden: Include parsers marked as hidden
- show_deprecated: Include parsers marked as deprecated
Returns:
- List of parser metadata dictionaries
"""Interactive help functionality for parser modules.
def get_help(parser_mod_name: Union[str, ModuleType]) -> None:
"""
Show help screen for the selected parser.
Parameters:
- parser_mod_name: Parser module name or Module object (accepts module_name, cli-name, --argument-name variants)
Returns:
- None (displays help to stdout)
"""import jc
import subprocess
# Parse dig command output
cmd_output = subprocess.check_output(['dig', 'example.com'], text=True)
data = jc.parse('dig', cmd_output)
print(f"Query ID: {data[0]['id']}")
# Parse with error suppression
data = jc.parse('dig', cmd_output, quiet=True)
# Get raw (preprocessed) output
raw_data = jc.parse('dig', cmd_output, raw=True)import jc
# Get all available parsers
all_parsers = jc.parser_mod_list()
print(f"Available parsers: {len(all_parsers)}")
# Get only streaming parsers
streaming_parsers = jc.streaming_parser_mod_list()
print(f"Streaming parsers: {streaming_parsers}")
# Get parser information
dig_info = jc.parser_info('dig')
print(f"Parser: {dig_info['name']}, Version: {dig_info['version']}")import jc
# Get parser module
jc_dig = jc.get_parser('dig')
data = jc_dig.parse(cmd_output)
# Show parser help
jc.get_help('dig')Install with Tessl CLI
npx tessl i tessl/pypi-jc