Skip to main content

Simple to use json/yaml modifier

Project description

json_modify

https://img.shields.io/pypi/v/json_modify https://img.shields.io/pypi/pyversions/json_modify.svg https://img.shields.io/github/license/Enacero/json_modify https://img.shields.io/badge/code%20style-black-000000.svg https://travis-ci.org/Enacero/json_modify.svg?branch=master https://codecov.io/gh/Enacero/json_modify/branch/master/graph/badge.svg

json_modify is simple library that helps apply modifications to json/yaml files and python representations of json.

Installation

pip install json_modify

Example

from json_modify import apply_actions

source = {
    "spec": {
        "name": "test",
        "metadata": [
            {"name": "test1", "value": "test1"},
            {"name": "test2", "value": "test2"}
        ],
        "values": {"value1": 10, "value2": 20}
    }
}

actions = [
    {"action": "add",
     "path": "spec/values",
     "value": {"value3": 30, "value4": 40}}
]

result = apply_actions(source, actions)

print(result)

{"spec": {
    "name": "test",
    "metadata": [
        {"name": "test1", "value": "test1"},
        {"name": "test2", "value": "test2"}
    ],
    "values": {
        "value1": 10, "value2": 20,
        "value3": 30, "value4": 40}
    }
}

Markers

json_modify uses markers to traverse lists. Markers should be used in path for example:

"path": "metadata/$project_name/name"

There are two types of markers:

  • Filter marker ($<marker_name>) - the kind of marker, that is used to select dictionary in list by list of section[key] == value. Filter marker should be described in action where it’s used. For example:

{
    "action": "delete",
    "path": "metadata/$project_name/name",
    "project_name": [{"key": "name", "value": "nginx"}]
}
  • Index marker ($<index>) - the kind of marker, that is used to select specific element in list, by it’s index. For example:

{
    "action": "replace",
    "path": ["users", "$0"],
    "value": { "name": "test_user", "id": 0}
}

It’s allowed to use any quantity of markers and mix both types of markers in single path.

Action schema

  • action (Required): Type of action, possible values are: add, replace, delete, rename(only for dictionaries).

  • path (Required): Path to the field that we want to change. path can be string, separated by delimiter (default is \) or list of strings.

  • value (Optional for delete, Required for other): Value that should be applied to specified path. The type of value is described for each action separately.

  • marker (Required for each non index marker in path): List of dictionaries, that should be applied to find value in list. Each dictionary consist of:

    • key (Required): Name of the key that should be used for search.

    • value (Required): Value that is used to find concrete dictionary in list of dictionaries.

Supported actions

  1. add: Insert values into section, specified by last key of path. The last key in section should lead to list or dict section.

    • For add action on list value should be of type list, so that it’ll be possible to extend current list.

    • For add action on dict value should be of type dict, so that we can update current dict.

  2. replace: Replace section, specified by last key of path with value.

  3. delete: Delete section, specified by last key of path.

  4. rename: Move content of section, specified by last key of path to section with name specified in value.

TODO

  1. Add documentation to ReadTheDocs

  2. Add creation of action’s by diff.

License

Copyright Oleksii Petrenko, 2020.

Distributed under the terms of the MIT license, json_modify is free and open source software.

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_modify-1.0.1.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

json_modify-1.0.1-py2.py3-none-any.whl (6.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file json_modify-1.0.1.tar.gz.

File metadata

  • Download URL: json_modify-1.0.1.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.1

File hashes

Hashes for json_modify-1.0.1.tar.gz
Algorithm Hash digest
SHA256 484506cc0ac72d8eb472b5d2454495794cd6c0fed435b4df96a6995cbb5869e0
MD5 abf91c9ccbdc32a18358a0aabaef26a0
BLAKE2b-256 29ae42564b430931886e47fb82dddc0370c77335ddd909df150a22acd7852bff

See more details on using hashes here.

File details

Details for the file json_modify-1.0.1-py2.py3-none-any.whl.

File metadata

  • Download URL: json_modify-1.0.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.1

File hashes

Hashes for json_modify-1.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f92996adfd256e19b399fb4f63fbe5af0270553dc944936286147b007822d323
MD5 543ebec54a717a857dc3b2c3cdfc086a
BLAKE2b-256 91816b274c8d120c67e9264efcad3b1d406ebe69973380bd7aab098dfe4bffb2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page