A Next Generation Library to update any json document
Project description
JsonUpdate-NG
Introduction
jsonupdate-ng provides easy and scalable way to update any json document using following ways:
- json : a head json which you can use to add/update/delete nodes from base json
- jsonpath : a jsonpath as node name along with the value to add/update/delete multiple nodes from the base json in one shot
- a mixup of json and jsonpaths
PS - jsonpath is a language like xpath to select single/multiple nodes from a given json. There are multiple tutorials on web to learn this.
Here are few resources to learn jsonpath:
- jsonpath tutorial : https://goessner.net/articles/JsonPath/index.html#e2
- jsonpath evaluator : https://jsonpath.com/
Prerequisites
jsonupdate-ng has following prerequisites:
jsonpath-ng>=1.4.3
setuptools>=39.1.0
you can install all the prerequisites either one by one or using the requirements.txt provided with source code:
pip install -r requirements.txt
Installation
RESTLibrary can be easily installed using pip with following command:
pip install jsonupdate-ng
Alternatively it can also be installed from the sourcecode:
python setup.py install
Major features
updateJson
this python function takes two arguments, base json and head json, and updates the base json based on the details provided in head.
Example
from jsonupdate_ng import jsonupdate_ng
updatedJson = jsonupdate_ng.updateJson(baseJson, headJson)
contents of base json
{
"page": 1,
"per_page": 6,
"total": 12,
"total_pages": 2,
"data": [
{
"id": 1,
"email": "george.bluth@reqres.in",
"first_name": "George",
"last_name": "Bluth",
"avatar": "https://reqres.in/img/faces/1-image.jpg"
},
{
"id": 2,
"email": "janet.weaver@reqres.in",
"first_name": "Janet",
"last_name": "Weaver",
"avatar": "https://reqres.in/img/faces/2-image.jpg"
},
{
"id": 3,
"email": "emma.wong@reqres.in",
"first_name": "Emma",
"last_name": "Wong",
"avatar": "https://reqres.in/img/faces/3-image.jpg"
},
{
"id": 4,
"email": "eve.holt@reqres.in",
"first_name": "Eve",
"last_name": "Holt",
"avatar": "https://reqres.in/img/faces/4-image.jpg"
},
{
"id": 5,
"email": "charles.morris@reqres.in",
"first_name": "Charles",
"last_name": "Morris",
"avatar": "https://reqres.in/img/faces/5-image.jpg"
},
{
"id": 6,
"email": "tracey.ramos@reqres.in",
"first_name": "Tracey",
"last_name": "Ramos",
"avatar": "https://reqres.in/img/faces/6-image.jpg"
}
]
}
contents of head json (comments are just to clarify the example, please remove them before trying it in your code)
{
"name" : "users", // this will add a name node at the root
"per_page" : 12, // this will update the per_page value
"company" : { "name" : "anything", "office" : "anywhere" }, // this will add a new object at the root
"total_pages" : "<<<DELETE>>>", // this will delete total_pages node from the root
"data" : [
{
"id": 100, // this will update id of the first item in the data list
"full_name": "deepak chourasia", // this will add a full_name node in the first item of the data list
}
],
"$.limit" : 6, // this will add a limit node at the root
"$.data[*].designation" : "developer", // this will add a designation node in all the objects inside data list
"$.data[?(@.id > 3)].salary" : "$100,000", // this will add a salary node in all the objects inside data list which have id > 3
"$.data[100]" : { // this can be used to add/update an object at provided index in the data list, if index is greater than current length of the list then item will be added to the end of the list
"id": 101,
"email": "deepak.chourasia@gmail.com",
"first_name": "Deepak",
"last_name": "Chourasia"
},
"$.data[?(@.id > 4 & @.id < 10)].avatar" : "<<<DELETE>>>" // this will delete avtar node from the object inside data list which has id > 4
}
checkIfNodeExistsAtGivenJsonPath
this function checks if a node exists at given jsonpath in the provided json
Example
from jsonupdate_ng import jsonupdate_ng
exists = jsonupdate_ng.checkIfNodeExistsAtGivenJsonPath(baseJson, "$.data")
Add_Update_Delete_Node_AtGivenJsonPath
this function can perform multiple tasks based on the situation:
- will add a node if it doesnt exist in base json
- will update the node value if node already exists in base json
- will delete the node if value is provided as <<<DELETE>>>
from jsonupdate_ng import jsonupdate_ng
updatedJson = jsonupdate_ng.Add_Update_Delete_Node_AtGivenJsonPath(baseJson, "$.name", "Deepak Chourasia")
Contributions:
Creator and maintainer : Deepak Chourasia
License
JsonUpdate-NG is open source software provided under the Apache License 2.0
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
Built Distribution
File details
Details for the file jsonupdate_ng-1.0.3.tar.gz
.
File metadata
- Download URL: jsonupdate_ng-1.0.3.tar.gz
- Upload date:
- Size: 11.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a8f1bf346de8c9c308a83627e88d5443a01554cba5849fd008fcf2bbafa33fea |
|
MD5 | 202fdd3a49a3e8bb36aeaaa44cd37fa9 |
|
BLAKE2b-256 | 9420e46c06de80119dbbb09da281be3dd1d95ab56c4a5e9a1f89ddc7b5a58968 |
File details
Details for the file jsonupdate_ng-1.0.3-py3-none-any.whl
.
File metadata
- Download URL: jsonupdate_ng-1.0.3-py3-none-any.whl
- Upload date:
- Size: 10.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f77ea275c542184181eb7aa7536f322622ee2e01559aaef3bf8eb728cb260ec1 |
|
MD5 | 07d72f5057019289fc35a607d61a897d |
|
BLAKE2b-256 | 113f3359c18473ad359b8fb5304e8e3f36374df0930f1fe7a5bf0e2de0b35f61 |