JStyleson

0.0.2 · maintenance · verified Wed Apr 15

JStyleson is a Python library designed to parse JSON data that includes JavaScript-style comments (single-line, multi-line, and inline) and trailing commas. It functions by first stripping these non-standard elements from the input string, then passing the sanitized string to Python's standard `json` module for parsing. The current version is 0.0.2, and it has not seen updates since 2016, indicating it is no longer actively maintained.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `jstyleson.loads()` to parse a JSON string containing JavaScript-style comments and trailing commas. It then shows how `jstyleson.dumps()` can be used to serialize a Python dictionary back to a JSON string, leveraging the standard `json` module's capabilities for formatting.

import jstyleson

# Example JSON string with JS-style comments and a trailing comma
invalid_json_str = """
{
    "name": "Alice", // User's name
    "age": 30, /* Age in years */
    "isStudent": false, // Trailing comment after an element
    "interests": ["coding", "reading", "hiking"],
    "contact": {
        "email": "alice@example.com",
        "phone": "123-456-7890", // Phone number
    },
}
"""

try:
    # Parse the JSON string with jstyleson
    result_dict = jstyleson.loads(invalid_json_str)
    print("Successfully parsed JSON:")
    print(result_dict)
    print(f"Type: {type(result_dict)}")

    # You can also dump a Python dictionary back to a JSON string
    json_output = jstyleson.dumps(result_dict, indent=2)
    print("\nDumped JSON:")
    print(json_output)

except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →