A Python module for working with JSON-like data using dot-separated keys and JSON path-like syntax.
You can install this module using pip:
pip install jsonpath-objectImport the JsonPathObject class from the module:
from jsonpath_object import JsonPathObjectYou can create a JsonPathObject instance with optional initial data, raise behavior, and default factory:
from jsonpath_object import JsonPathObject
# Create an empty JsonPathObject
obj = JsonPathObject()
# Create a JsonPathObject with initial data
data = {
'name': 'John',
'address': {
'city': 'New York',
'zip_code': '10001'
}
}
obj = JsonPathObject(data)
# You can also specify custom raise behavior and default factory
obj = JsonPathObject(data, raise_on_missing=False, default_factory=lambda: 'N/A')You can access data in the JsonPathObject using dot-separated keys or JSON path-like syntax:
# Access data using dot-separated keys
name = obj['name'] # 'John'
city = obj['address.city'] # 'New York'
# Access data using JSON path-like syntax
zip_code = obj['address["zip_code"]'] # '10001'You can modify data in the JsonPathObject by setting values:
# Set a new value
obj['age'] = 30
# Update an existing value
obj['address.city'] = 'Los Angeles'
# Note that this will create nested objects if they don't exist
obj['address.state'] = 'California'You can delete data from the JsonPathObject using dot-separated keys or JSON path-like syntax:
# Delete a key
del obj['age']
# Delete nested data
del obj['address.zip_code']You can convert the JsonPathObject to a regular Python dictionary or list using the to_object method:
data_dict = obj.to_object()This module is released under the MIT License.