JSONC Parser

1.1.5 · active · verified Thu Apr 16

jsonc-parser is a lightweight, zero-dependency Python module designed for parsing `.jsonc` files, which are JSON files extended to support JavaScript-style comments. It allows developers to easily deserialize JSONC content into Python dictionaries, stripping out comments in the process. The library is currently at version 1.1.5 and is actively maintained with updates released on an as-needed basis rather than a strict schedule.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to parse a `.jsonc` file and a JSONC string using the `JsoncParser` class. It creates a temporary `.jsonc` file, reads its content (including comments), and then parses a direct string, showcasing how comments are stripped to yield a standard Python dictionary.

import os
from jsonc_parser.parser import JsoncParser

# Create a dummy JSONC file for demonstration
jsonc_content = '''
{
  "name": "Test Config", // Application name
  "version": "1.0.0", /* Current version */
  "settings": {
    "debugMode": true,
    "logLevel": "INFO"
  }
}
'''

file_path = "./config.jsonc"
with open(file_path, "w") as f:
    f.write(jsonc_content)

try:
    # Parse the .jsonc file
    data = JsoncParser.parse_file(file_path)
    print("Parsed data:", data)

    # Parse a JSONC string
    jsonc_string = '{ "message": "Hello, /* commented */ World!" }'
    string_data = JsoncParser.parse_str(jsonc_string)
    print("Parsed string data:", string_data)

finally:
    # Clean up the dummy file
    if os.path.exists(file_path):
        os.remove(file_path)

view raw JSON →