{"id":1992,"library":"dirtyjson","title":"DirtyJSON Python Decoder","description":"dirtyjson is a Python JSON decoder (version 1.0.8, last updated Nov 2022) designed to extract data from malformed or 'dirty' JSON, often found embedded in JavaScript files. It tolerates common non-standard JSON elements like single quotes, comments (line and block), dangling commas, unquoted keys, and hexadecimal/octal numbers. It focuses solely on decoding and provides line/column number contexts for parsed elements. While its release cadence is infrequent, it remains active for specialized parsing needs.","status":"active","version":"1.0.8","language":"en","source_language":"en","source_url":"https://github.com/codecobblers/dirtyjson","tags":["json","parser","malformed json","dirty json","decoder","javascript"],"install":[{"cmd":"pip install dirtyjson","lang":"bash","label":"Install dirtyjson"}],"dependencies":[],"imports":[{"note":"Standard json module cannot parse dirty JSON.","wrong":"import json\njson.loads(dirty_json_string)","symbol":"loads","correct":"import dirtyjson\ndirtyjson.loads(json_string)"},{"note":"Standard json module cannot parse dirty JSON from files.","wrong":"import json\nwith open('file.js') as f:\n    data = json.load(f)","symbol":"load","correct":"import dirtyjson\nwith open('file.js') as f:\n    data = dirtyjson.load(f)"}],"quickstart":{"code":"import dirtyjson\n\ndirty_json_string = \"\"\"\n{\n    name: 'John Doe', // Unquoted key, single quotes, comment\n    age: 30,\n    email: \"john@example.com\", /* Block comment */\n    'is_active': true,\n    data: [\n        1,\n        2, // Dangling comma\n    ]\n}\n\"\"\"\n\ntry:\n    data = dirtyjson.loads(dirty_json_string)\n    print(\"Parsed data:\", data)\n    \n    # Accessing position attributes\n    name_pos = data.attributes('name')\n    print(f\"'name' key starts at line {name_pos.key.line}, column {name_pos.key.column}\")\n\n    # Using standard dict/list operations\n    print(\"Name:\", data['name'])\n    print(\"First data element:\", data['data'][0])\n\nexcept dirtyjson.Error as e:\n    print(f\"Error parsing dirty JSON: {e}\")","lang":"python","description":"Demonstrates parsing a malformed JSON string with unquoted keys, single quotes, comments, and a dangling comma. It also shows how to access the positional attributes stored by dirtyjson and use the resulting AttributedDict as a standard dictionary."},"warnings":[{"fix":"Use `json.dumps()` or `json.dump()` for serialization.","message":"dirtyjson is a decoder only; it does not provide encoding capabilities (dump/dumps). For writing JSON, use Python's standard `json` library or `simplejson`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Only use `dirtyjson` when strictly necessary for malformed input. Validate the output carefully if the input quality is highly variable or unpredictable.","message":"Due to its flexibility in parsing malformed JSON, `dirtyjson` may not always produce the exact data structure or values that a user 'expects' from highly ambiguous input. Its primary goal is to extract data, not to strictly validate.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Be aware of these non-standard interpretations and types when integrating with systems that expect strict JSON adherence or standard Python `dict`/`list` types without additional attributes.","message":"Unlike the standard JSON specification, `dirtyjson` handles `NaN`, `Infinity`, `-Infinity`, and hexadecimal/octal integers (`0x...`, `0o...`) as valid numbers. Additionally, it uses `AttributedDict` and `AttributedList` subclasses (which are `dict`/`list` compatible) that store line/column number metadata, differing from standard Python types.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Benchmark performance if critical, and consider strict JSON parsing for inputs that are expected to be well-formed.","message":"Parsing 'dirty' or malformed JSON is inherently more complex and potentially less performant than parsing strict JSON. While `dirtyjson` is based on `simplejson`'s loader, it may not be suitable for high-performance or extremely high-reliability parsing environments compared to strict JSON parsers.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}