Updates JSON according to a change spec
Project description
json-updater
Setup
To import this library to your environment:
pip install json-updater
To import this function in your python code:
from json_updater import update_json
Use
To invoke:
modified_input = update_json(input, changeset)
Where,
input
is either:- a json-serializable
str
, or - a json-serializable
dict
- a json-serializable
changeset
is a json-serializablestr
ordict
, or ajson_updater.ChangeSet
object.- a json-serializable
str
conforming to thejson_updater.ChangeSet
, - a json-serializable
dict
conforming to thejson_updater.ChangeSet
, or - a
json_updater.ChangeSet
object
- a json-serializable
ChangeSet Spec
The ChangeSet
definition can be found here; it consists of a set of Change
objects. Each Change
contains:
- an
op
(operation), - a
path
which conforms to jsonpath syntax, - an optional
value
parameter, used in certainop
types - an optional
index
parameter, used in certainop
types
Changes are applied in the order they are received.
Operation Types
The following operations are supported:
Delete: "del"
Deletes all json nodes specified in the change path
Init: "init"
For all json nodes specified in the change path
, if that node doesn't exist, its value is initialized to the change value
. Otherwise, ignores the existing node.
Primarily used to ensure an array exists in the object tree before invoking Array Insert commands.
Upsert: "ups"
For all json nodes specified in the change path
, creates or replaces that node, setting the value to the change value
Replace: "rpl"
For all json nodes specified in the change path
, replaces that node, setting the value to the change value
Array Insert: "arr_ins"
For all json nodes specified in the change path
, which must be of type array, insert the change value
at the provided index
.
If index
is null
, appends to the end of the array.
Project details
Release history Release notifications | RSS feed
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
Hashes for json_updater-0.0.6-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c377197873ed0829a02c51c7460d0eb6ed84d5193f037dff987845bbd3eecf2b |
|
MD5 | d4177743bc12fea723135fee63af6976 |
|
BLAKE2b-256 | 1d78891769e2832dbc87b7dbf2cfe201f866e2d2d6dcf9ac13e7e1dd72ec5c7f |