This module is built to provide straightforward way to build and modify json objects
Project description
json_builder
Build or modify json objects incrementally using json paths
Installation
The quick way:
pip install json_builder
Usage
Before start, check the json path syntax: https://goessner.net/articles/JsonPath/
Add new key
root_object = {"a": 10}
JsonBuilder.add(root_object, "$.b", 20)
JsonBuilder.add(root_object, "$.c.d.e", 30)
print(root_object)
Output:
{
'a': 10,
'b': 20,
'c': {
'd': {
'e': 30
}
}
}
Modify existing key value
root_object = {
'a': 10,
'b': 20,
'c': {
'd': {
'e': 30
}
}
}
JsonBuilder.add(root_object, "$.c.d.e", "test")
print(root_object)
Output:
{
'a': 10,
'b': 20,
'c': {
'd': {
'e': 'test'
}
}
}
Create a list
root_object = {
'a': 10,
}
JsonBuilder.add(root_object, "$.b[0]", "1")
JsonBuilder.add(root_object, "$.b[1]", "2")
JsonBuilder.add(root_object, "$.b[2]", {"c": 20})
print(root_object)
Output:
{
'a': 10,
'b': [
'1',
'2',
{
'c': 20
}
]
}
Modify existing list index
root_object = {
'a': 10,
'b': [
'1',
'2',
{
'c': 20
}
]
}
JsonBuilder.add(root_object, "$.b[2]", '3')
print(root_object)
Output:
{
'a': 10,
'b': [
'1',
'2',
'3'
]
}
Exceptions
Object is not json serializable
Values that are not json serializable are not allowed
root_object = object()
JsonBuilder.add(root_object, "$.b[4]", '3')
print(root_object)
root_object = {}
JsonBuilder.add(root_object, "$.b[4]", object())
print(root_object)
Output:
TypeError: Object <object object at 0x00000295240A4D20> is not JSON serializable.
Invalid json path
Check the json path syntax: https://goessner.net/articles/JsonPath/
root_object = {}
JsonBuilder.add(root_object, ".b[4]", 3)
print(root_object)
Output:
ValueError: Invalid json path ".b[4]". Check https://goessner.net/articles/JsonPath/ for more details.
Object is not a dictionary
root_object = {
'a': []
}
JsonBuilder.add(root_object, "$.a.b", 1)
print(root_object)
Output:
Exception: Can't insert key "b", object is not a dictionary. Root: {'a': []}. Json path: $.a.b
Object is not a list
root_object = {
'a': {}
}
JsonBuilder.add(root_object, "$.a[0]", 1)
print(root_object)
Output:
Exception: Can't insert on position "0", object is not a list. Root: {'a': {}}. Json path: $.a[0]
Previous list index not defined
List items must be added consecutively
root_object = {
'a': []
}
JsonBuilder.add(root_object, "$.a[0]", 1)
JsonBuilder.add(root_object, "$.a[2]", 2)
print(root_object)
Output:
Exception: Index to big "2", previous list indexes not defined. Root: {'a': [1]}. Json path: $.a[2]
License
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
json_builder-0.4.tar.gz
(3.3 kB
view details)
File details
Details for the file json_builder-0.4.tar.gz.
File metadata
- Download URL: json_builder-0.4.tar.gz
- Upload date:
- Size: 3.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.8.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a73b61a36a5c9809bac2ba0c6295466644c84b91532b0bfcb38deef05db68bbf
|
|
| MD5 |
ff9b170b8dd52ca0e520920a0b61325f
|
|
| BLAKE2b-256 |
d5699d1a6c3fbd9b6529dd761bb0686002bc27aa04291fb287e86fcd07a3c549
|