Skip to main content

JSON Merge Tree (built on top of jsonmerge) merges JSON objects in a hierarchy, allowing customization of how objects are merged.

Project description

json-merge-tree

JSON Merge Tree (built on top of jsonmerge) merges JSON objects in a hierarchy, allowing customization of how objects are merged.

The hierarchy is defined by the caller's parents module, which specifies a function for each resource type that returns the parent ID and another function to get its parents (unless it's the top level).

It relies on a SQLAlchemy Table containing the fields id (UUID), resource_id (UUID), resource_type (string), slug (string), and a field that stores the JSON object you want to merge.

Installation

Install this package, e.g., pip install json-merge-tree.


## merge_tree

Main entrypoint for merging JSON objects in a table up a hierarchy defined by `parents`.

### Example
```python
from json_merge_tree import merge_tree

import widget_service.parents
from widget_service.db import widgets

# Merge widgets of all the requested resource's ancestors and return the result
merged_json = merge_tree(
    table=widgets, 
    id='228135fb-6a3d-4551-93db-17ed1bbe466a', 
    type='brand', 
    json_field='widget', 
    parents=widget_service.parents, 
    slugs=None, 
    debug='annotate'
)

merge

Generic function to merge two JSON objects together. Largely the same as jsonmerge.Merger.merge but with the added ability to customize how objects are merged with the annotations below.

Merge key annotations

You can append the annotation listed below to a key at any level to customize how its value affects the merged json.

-- Unset

Unset this key in the merged json. The value of this key does not matter - you can set it to null.

  • E.g. 1
{
  "colors": {
    "content": "#000000", 
    "section--": null
  }
}

merged into

{
  "colors": {
    "section": "#000001"
  }
}

results in

{
  "colors": {
    "content": "#000000"
  }
}

  • E.g. 2
{
  "styles": {
    "h1--": null, 
    "h2": {
      "fontSize": 3
    }
  }
}

merged into

{
  "styles": {
    "h1": {
      "fontWeight": "heading"
    }
  }
}

results in

{
  "styles": {
    "h2": {
      "fontSize": 3
    }
  }
}

! Replace

Replace this key's value with this value.

  • E.g. 1
{
  "colors": {
      "content": "#000000", 
      "section!": "#000002"
  }
}

merged into

{
  "colors": {
    "section": "#000001"
  }
}

results in

{
  "colors": {
    "content": "#000000", 
    "section": "#000002"
  }
}

  • E.g. 2
{
  "styles": {
    "h1!": {
      "fontFamily": "heading",
      "fontSize": 5
    }
  }
}

merged into

{
  "styles": {
    "h1": {
      "fontWeight": "heading"
    }
  }
}

results in

{
  "styles": {
    "h1": {
      "fontFamily": "heading",
      "fontSize": 5
    }
  }
}

Slugs

Slugs are kind of a weird feature – and getting weirder as we use them to solve more use cases.

Originally, A slug was a named json object scoped under a resource in the hierarchy to be merged. In our case, a custom-page theme merged under one of the resources in our resource hierarchy.

The use of slugs has been extended to included named json object mixins not associated with any one resource at the bottom of the hierarchy, but scoped to a resource at any level. For example, a site wants to have a "dark" theme that could be applied to any page within the site.

When merging, the library first merges the json objects without slugs, then merges json objects with slugs at each level. Multiple slugs can be included when merging.

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_merge_tree-0.4.1.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

json_merge_tree-0.4.1-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file json_merge_tree-0.4.1.tar.gz.

File metadata

  • Download URL: json_merge_tree-0.4.1.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.9.19

File hashes

Hashes for json_merge_tree-0.4.1.tar.gz
Algorithm Hash digest
SHA256 5110ef1780a258df73cdf5fa24ccca8abd72a81aa462e3c01a7244df46a894a6
MD5 0e0d8fa232d878890478514f12e97070
BLAKE2b-256 4b3fe3796815771f6fa9977fdbe3686fc83a0e560445772c3e7b024ec5584737

See more details on using hashes here.

File details

Details for the file json_merge_tree-0.4.1-py3-none-any.whl.

File metadata

File hashes

Hashes for json_merge_tree-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 38bc64d78ef4a4452cf34afb5eb00d8e26c2870a6e4026d0f8b1e9c2d8237c2e
MD5 fb11511b1404a3c63b4b91cac88d3453
BLAKE2b-256 fede445d413ee16fd9d256f7e88b2b2ec0c2b9cd656a11b210155e505de00234

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