Registry / serialization / dirtyjson

dirtyjson

JSON →
library1.0.8pypypi✓ verified 47d ago

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.

serialization
pip install dirtyjson
Install & Compatibility
Where this runs
tested against v1.0.8 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.925 runs
installs and imports cleanly · install 0.0s · import 0.006s · 18MB
glibc
py 3.103.925 runs
installs and imports cleanly · install 1.5s · import 0.006s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

loads
from dirtyjson import loads
import dirtyjson
DirtyJSONLoader
from dirtyjson import DirtyJSONLoader
load
from dirtyjson import load

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.

import dirtyjson dirty_json_string = """ { name: 'John Doe', // Unquoted key, single quotes, comment age: 30, email: "john@example.com", /* Block comment */ 'is_active': true, data: [ 1, 2, // Dangling comma ] } """ try: data = dirtyjson.loads(dirty_json_string) print("Parsed data:", data) # Accessing position attributes name_pos = data.attributes('name') print(f"'name' key starts at line {name_pos.key.line}, column {name_pos.key.column}") # Using standard dict/list operations print("Name:", data['name']) print("First data element:", data['data'][0]) except dirtyjson.Error as e: print(f"Error parsing dirty JSON: {e}")
Debug
Known issues
gotchadirtyjson is a decoder only; it does not provide encoding capabilities (dump/dumps). For writing JSON, use Python's standard `json` library or `simplejson`.
fix
Use `json.dumps()` or `json.dump()` for serialization.
affects: All versions
gotchaDue 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.
fix
Only use `dirtyjson` when strictly necessary for malformed input. Validate the output carefully if the input quality is highly variable or unpredictable.
affects: All versions
gotchaUnlike 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.
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.
affects: All versions
gotchaParsing '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.
fix
Benchmark performance if critical, and consider strict JSON parsing for inputs that are expected to be well-formed.
affects: All versions
Upgrade
Version history
1.0.8latest on PyPI
Audit
Dependencies

No dependency data recorded yet.

Agent activity
15 hits · last 30 days
node
4
seranking-bot
4
ahrefsbot
3
Amazon
1
amazonbot
1
bytedance
1
Resources