{"id":6585,"library":"demjson3","title":"demjson3","description":"demjson3 is a Python 3 specific library for encoding, decoding, and validating JSON data, fully compliant with RFC 7159. It is a fork of the `demjson` project, updated to address Python 3 compatibility issues and provide enhanced error handling and linting capabilities. It offers both strict JSON parsing and a non-strict mode for more JavaScript-like syntax. The library is currently at version 3.0.6, with its last release in October 2022.","status":"active","version":"3.0.6","language":"en","source_language":"en","source_url":"https://github.com/nielstron/demjson3","tags":["json","encoding","decoding","validation","lint","parser"],"install":[{"cmd":"pip install demjson3","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"note":"demjson3 is the Python 3 native fork; direct `demjson` imports might be problematic or require older `setuptools` versions.","wrong":"import demjson\njson_string = demjson.encode(python_object)","symbol":"encode","correct":"import demjson3\njson_string = demjson3.encode(python_object)"},{"note":"Use `demjson3` for Python 3 environments.","wrong":"import demjson\npython_object = demjson.decode(json_string)","symbol":"decode","correct":"import demjson3\npython_object = demjson3.decode(json_string)"}],"quickstart":{"code":"import demjson3\n\n# Encode Python data to JSON string\npython_data = {'name': 'Alice', 'age': 30, 'isStudent': False, 'courses': ['Math', 'Science'], 'extra': None}\njson_output = demjson3.encode(python_data, indent=2, strict=True)\nprint('Encoded JSON:')\nprint(json_output)\n\n# Decode JSON string to Python data\njson_input = '{\"product\": \"Laptop\", \"price\": 1200.50, \"inStock\": true}'\npython_decoded = demjson3.decode(json_input)\nprint('\\nDecoded Python data:')\nprint(python_decoded)\n\n# Example of non-strict decoding (allowing comments and unquoted keys)\nnon_strict_json = \"{ /* A comment */ myKey: 'value', another: undefined }\"\npython_non_strict = demjson3.decode(non_strict_json, strict=False)\nprint('\\nDecoded non-strict JSON:')\nprint(python_non_strict)\n","lang":"python","description":"This quickstart demonstrates basic encoding and decoding of JSON data using `demjson3`. It shows how to convert Python dictionaries and lists to JSON strings and vice-versa. It also includes an example of using the `strict=False` option to parse more lenient, JavaScript-like JSON with features not allowed by RFC 7159."},"warnings":[{"fix":"For Python 3 projects, replace `demjson` with `demjson3` in `requirements.txt` and use `import demjson3`. If legacy code expects the `demjson` name, use `import demjson3 as demjson`.","message":"Users migrating from the original `demjson` library to `demjson3` (especially in Python 3 environments) should update their `import` statements. `demjson3` is a dedicated Python 3 fork, resolving compatibility issues (`use_2to3` errors with newer `setuptools`) that plagued `demjson` in Python 3.","severity":"breaking","affected_versions":"demjson (all versions on Python 3); demjson3 (n/a, it's the solution)"},{"fix":"If parsing JavaScript-like input that is not strictly JSON, set `strict=False` in the `decode()` method (e.g., `demjson3.decode(json_string, strict=False)`).","message":"`demjson3` defaults to strict RFC 7159 compliance. This means non-standard JavaScript syntax (e.g., comments, unquoted keys, single-quoted strings, hexadecimal numbers, `undefined` keyword) will raise errors during decoding.","severity":"gotcha","affected_versions":"All versions of demjson3"},{"fix":"Ensure `json_equivalent()` returns Python data structures, not `str`.","message":"When implementing `json_equivalent()` on custom Python classes for encoding, this method must return standard Python objects (dictionaries, lists, primitives) that `demjson3` can then encode. It should *not* return an already JSON-formatted string, as this will lead to double-encoding or incorrect output.","severity":"gotcha","affected_versions":"All versions of demjson3"},{"fix":"Be aware that numerical data might be returned as `Decimal` objects. Adjust downstream processing to handle `decimal.Decimal` types, or explicitly convert them to `float` if precision loss is acceptable.","message":"`demjson3` automatically converts numbers that would lose precision as standard Python `float` types into `decimal.Decimal` objects during decoding to preserve accuracy.","severity":"gotcha","affected_versions":"All versions of demjson3"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[]}