Skip to main content

Parsing for extended JSON with optional comments and trailing commas

Project description

JSONXOX: Extended JSON Parsing

Do you love the JSON serialization format but wish it included support for optional comments and trailing commas? Well if so, the JSONXOX library is for you.

The JSONXOX library provides support for parsing extended JSON files and strings – JSON files and strings which may contain optional C-style single-line and multi-line comments as well as trailing commas.

Requirements

The JSONXOX library has been tested with Python 3.10, 3.11, 3.12, 3.13 and 3.14. The library has not been tested with, nor is it likely compatible with Python 3.9 and earlier.

Installation

The JSONXOX library is available from PyPI, so may be added to a project's dependencies via its requirements.txt file or similar by referencing the JSONXOX library's name, jsonxox, or the library may be installed directly into the local runtime environment using pip install by running the following command:

$ pip install jsonxox

Usage Example

To use the JSONXOX library, simply import the library into your project as a drop-in replacement for the standard json library, and use it as you would to load a JSON file or JSON string. The library can be used to load standard JSON files and strings as well as extended JSON files and strings containing single- and multi-line comments as well as trailing commas. The library can also be used to save standard JSON files and strings.

See the Methods section for more information about the methods provided by the library.

import jsonxox

sample: str = """
/* this is a multi-line comment block
that can span multiple lines or just a single line
*/
{
  "a": 123, /* this is also a multi-line comment block (using a single line) */
  // this is a single-line comment
  "b": "456", // notice the trailing comma
}
"""

data = jsonxox.loads(sample)

assert isinstance(data, dict)

assert "a" in data
assert data["a"] == 123

assert "b" in data
assert data["b"] == "456"

Methods

The jsonxox library provides the following methods, mirroring the standard json library in its available methods and overall functionality:

  • load(handle, **kwargs) (object) – The load() method takes a file handle or file handle like object that is an instance of io.TextIOWrapper or one of its subclasses as input and removes any single and multi-line comments as well as any trailing commas from the input string contents read from the file handle, and then passes the cleaned string to the standard library's json.loads() method for quick deserialization to its corresponding Python data types. Any additional keyword arguments are passed to the json.loads() method, as such any additional keyword arguments must be compatible with the json.loads() method.

  • loads(handle, **kwargs) (object) – The loads() method takes a string as input and removes any single and multi-line comments as well as any trailing commas from the input string, and then passes the cleaned string to the standard library's json.loads() method for quick deserialization to its corresponding Python data types. Any additional keyword arguments are passed to the json.loads() method, as such any additional keyword arguments must be compatible with the json.loads() method

  • dump(*args, **kwargs) – The dump() method is a passthrough to the standard json library's dump() method and accepts the same arguments and has the same return values. Note that the dump() method cannot be used to re-save any comments or trailing commas that may have been present in the original extended JSON file or string.

  • dumps(*args, **kwargs) (str) – The dumps() method is a passthrough to the standard json library's dumps() method and accepts the same arguments and has the same return values. Note that the dumps() method cannot be used to re-save any comments or trailing commas that may have been present in the original extended JSON file or string.

Classes

The jsonxox library provides the following classes, mirroring the standard json library in its available classes:

  • JSONDecoder – The JSONDecoder class is a direct import of and reference to the standard json library's JSONDecoder class.

  • JSONEncoder – The JSONEncoder class is a direct import of and reference to the standard json library's JSONEncoder class.

Exceptions

The jsonxox library provides the following exception classes, mirroring the standard json library in its available exception classes:

  • JSONDecodeError – The JSONDecodeError exception class is a direct import of and reference to the standard json library's JSONDecodeError exception class.

  • JSONXOXDecodeError – The JSONXOXDecodeError exception class is a subclass of the standard json library's JSONDecodeError class and will be raised by the jsonxox library if an exception occurs while attempting to clean or load the JSON input.

Unit Tests

The JSONXOX library includes a suite of comprehensive unit tests which ensure that the library functionality operates as expected. The unit tests were developed with and are run via pytest.

To ensure that the unit tests are run within a predictable runtime environment where all of the necessary dependencies are available, a Docker image is created within which the tests are run. To run the unit tests, ensure Docker and Docker Compose is installed, and perform the following commands, which will build the Docker image via docker compose build and then run the tests via docker compose run – the output the tests will be displayed:

$ docker compose build
$ docker compose run tests

To run the unit tests with optional command line arguments being passed to pytest, append the relevant arguments to the docker compose run tests command, as follows, for example passing -v to enable verbose output and -s to print standard output:

$ docker compose run tests -v -s

See the documentation for PyTest regarding available optional command line arguments.

Copyright & License Information

Copyright © 2023–2026 Daniel Sissman; licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jsonxox-0.8.0.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jsonxox-0.8.0-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file jsonxox-0.8.0.tar.gz.

File metadata

  • Download URL: jsonxox-0.8.0.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for jsonxox-0.8.0.tar.gz
Algorithm Hash digest
SHA256 583ed7d23267df560c452ccfe0227419bfffb220745f9f4d64503a9320db925c
MD5 ea3a660f412091912de762160b455dfb
BLAKE2b-256 bb8dd36efff95bb95bc3bdbe92d8795e735642acff2a78e82e2961ecc8d8d5e3

See more details on using hashes here.

Provenance

The following attestation bundles were made for jsonxox-0.8.0.tar.gz:

Publisher: python-publish.yml on bluebinary/jsonxox

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file jsonxox-0.8.0-py3-none-any.whl.

File metadata

  • Download URL: jsonxox-0.8.0-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for jsonxox-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d2cb361938509e239ae3bed2b9dfa54b82bcd92cab5c438652a6664c32fe92e2
MD5 e32edd5f9eade06bd4ea74f225218071
BLAKE2b-256 9790ced5437f827049e8443cb10b105e0ffde84a0445e5bc55544488f425ba4c

See more details on using hashes here.

Provenance

The following attestation bundles were made for jsonxox-0.8.0-py3-none-any.whl:

Publisher: python-publish.yml on bluebinary/jsonxox

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page