jsonasobj2
raw JSON → 1.0.4 verified Mon Apr 27 auth: no python
Python library for converting JSON to Python objects and back, version 2. Current version 1.0.4, updated June 2023. Active development with infrequent releases.
pip install jsonasobj2 Common errors
error ModuleNotFoundError: No module named 'jsonasobj' ↓
cause Installed jsonasobj2 but tried to import 'jsonasobj' (v1).
fix
Use: from jsonasobj2 import JsonObj
error AttributeError: 'JsonObj' object has no attribute 'from_json' ↓
cause Calling from_json() on an instance instead of the class.
fix
Use JsonObj.from_json(json_string) as a class method.
Warnings
deprecated The old jsonasobj library is deprecated. Use jsonasobj2 for new projects. ↓
fix Replace 'import jsonasobj' with 'from jsonasobj2 import JsonObj'.
gotcha JsonObj does not support attribute names that are Python reserved words (e.g., 'class'). Use dict access instead. ↓
fix Use obj['class'] or setattr(obj, 'class', value) if needed.
gotcha JsonObj.from_json() is a class method that returns a new JsonObj. It does not modify the existing instance. ↓
fix Assign the result: obj = JsonObj.from_json(json_string).
Imports
- JsonObj wrong
from jsonasobj import JsonObjcorrectfrom jsonasobj2 import JsonObj
Quickstart
from jsonasobj2 import JsonObj
# Create a JsonObj from a dict
data = {'name': 'Alice', 'age': 30}
obj = JsonObj(**data)
print(obj.name) # Alice
print(obj.as_json()) # '{"name": "Alice", "age": 30}'
# Convert JSON string to JsonObj
json_str = '{"key": "value"}'
obj2 = JsonObj.from_json(json_str)
print(obj2.key) # value