Skip to main content

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 hashes)

Uploaded Source

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