{"id":2695,"library":"pyjson5","title":"pyjson5","description":"PyJSON5 is a high-performance JSON5 serializer and parser library for Python 3, implemented in Cython. It extends the standard JSON format with features like comments, unquoted keys, and trailing commas, making it ideal for human-editable configuration files. This library is actively maintained, with frequent updates and a current stable version of 2.0.0.","status":"active","version":"2.0.0","language":"en","source_language":"en","source_url":"https://github.com/Kijewski/pyjson5","tags":["json","json5","parser","serializer","cython","configuration"],"install":[{"cmd":"pip install pyjson5","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"note":"The PyPI package `json5` is a different, pure-Python implementation. This library (pyjson5, the Cython one) exposes its functions directly under the `pyjson5` module. Using `import json5` will import the other library if installed.","wrong":"import json5","symbol":"loads, dumps, load, dump","correct":"import pyjson5\n# Use pyjson5.loads, pyjson5.dumps, etc."}],"quickstart":{"code":"import pyjson5\nimport io\n\njson5_data = \"\"\"\n{\n  // This is a comment\n  name: 'Project Alpha',\n  version: 1.0.0,\n  features: [\n    'comments',\n    'trailing commas',\n  ],\n  config: { key: 'value', }, // Trailing comma\n}\n\"\"\"\n\n# Load from a string\ndata = pyjson5.loads(json5_data)\nprint(f\"Loaded data: {data}\")\n\n# Dump to a string\noutput_json5 = pyjson5.dumps(data, indent=2)\nprint(f\"\\nDumped JSON5:\\n{output_json5}\")\n\n# Load from a file-like object (StringIO acts like a file)\nfile_like_obj_read = io.StringIO(json5_data)\nfile_data = pyjson5.load(file_like_obj_read)\nprint(f\"\\nLoaded from file-like object: {file_data}\")\n\n# Dump to a file-like object\nfile_like_obj_write = io.StringIO()\npyjson5.dump(data, file_like_obj_write, indent=2)\nprint(f\"\\nDumped to file-like object:\\n{file_like_obj_write.getvalue()}\")\n","lang":"python","description":"This quickstart demonstrates how to load JSON5 data from a string using `pyjson5.loads` and dump Python objects back to a JSON5 string using `pyjson5.dumps`. It also shows reading from and writing to file-like objects using `pyjson5.load` and `pyjson5.dump` respectively. Note the use of `io.StringIO` for in-memory file operations."},"warnings":[{"fix":"Ensure that file-like objects passed to `pyjson5.dump()` are opened in text write mode (e.g., `open('file.json5', 'w')`) rather than binary write mode (`'wb'`).","message":"In `pyjson5` v2.0.0, the `dump()` function now consistently writes `str` (Unicode) instead of `bytes` to file-like objects. This is a change from previous versions and can lead to `TypeError: a bytes-like object is required, not 'str'` if the target file object is opened in binary write mode (`'wb'`).","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Upgrade your Python environment to version 3.8 or newer to use `pyjson5` v2.0.0 and subsequent releases. If backward compatibility with older Python versions is required, use `pyjson5` v1.x.","message":"Starting with `pyjson5` v2.0.0, the library explicitly requires Python 3.8 or later. Versions prior to `v1.6.8` supported Python 3.5+, and `v1.6.8` raised the minimum to Python 3.7+.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Instead of `cls`, use the `default` keyword argument in `dumps` or `dump` for custom serialization of unsupported types. For custom deserialization, manual post-processing of the loaded Python object is often required.","message":"Unlike Python's standard `json` module, `pyjson5` does not support the `cls` keyword argument for custom `JSONDecoder` or `JSONEncoder` subclasses in its `load`, `loads`, `dump`, or `dumps` functions. This is due to its Cython-based parsing and serialization approach.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Consider the use case: if machine-to-machine interoperability is paramount, use Python's built-in `json` module. If human readability and maintainability of configuration files are the priority, `pyjson5` is a suitable choice.","message":"JSON5 is designed for human-editable configuration files, allowing features like comments and unquoted keys. It is generally not recommended for strict machine-to-machine communication, where the more rigid standard JSON format is often preferred due to broader tooling support and stricter validation.","severity":"gotcha","affected_versions":"All versions"},{"fix":"To enforce strict parsing and disallow duplicate keys, pass `allow_duplicate_keys=False` to the `load()` or `loads()` functions. For example: `pyjson5.loads(json5_string, allow_duplicate_keys=False)`.","message":"By default, `pyjson5.load()` and `pyjson5.loads()` allow duplicate keys within an object (dictionary) in the JSON5 input. When duplicate keys are present, the last encountered value for that key will override previous ones.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}