
Company News
Socket Named Top Sales Organization by RepVue
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.
json-merge-tree
Advanced tools
JSON Merge Tree (built on top of json-multi-merge) merges JSON objects in a hierarchy, allowing customization of how objects are merged.
JSON Merge Tree (built on top of json-multi-merge)
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.
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'
)
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.
You can append the annotation listed below to a key at any level to customize how its value affects the merged json.
-- UnsetUnset this key in the merged json. The value of this key does not matter - you can set it to null.
{
"colors": {
"content": "#000000",
"section--": null
}
}
merged into
{
"colors": {
"section": "#000001"
}
}
results in
{
"colors": {
"content": "#000000"
}
}
{
"styles": {
"h1--": null,
"h2": {
"fontSize": 3
}
}
}
merged into
{
"styles": {
"h1": {
"fontWeight": "heading"
}
}
}
results in
{
"styles": {
"h2": {
"fontSize": 3
}
}
}
! ReplaceReplace this key's value with this value.
{
"colors": {
"content": "#000000",
"section!": "#000002"
}
}
merged into
{
"colors": {
"section": "#000001"
}
}
results in
{
"colors": {
"content": "#000000",
"section": "#000002"
}
}
{
"styles": {
"h1!": {
"fontFamily": "heading",
"fontSize": 5
}
}
}
merged into
{
"styles": {
"h1": {
"fontWeight": "heading"
}
}
}
results in
{
"styles": {
"h1": {
"fontFamily": "heading",
"fontSize": 5
}
}
}
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.
slugs_onlyWhen slugs_only=True is passed to merge_tree, the resource JSON at every level of the hierarchy is skipped — only slug JSON objects are merged. This is useful when you only need the slug data without any of the base resource JSON mixed in.
merged_json = merge_tree(
table=widgets,
id='228135fb-6a3d-4551-93db-17ed1bbe466a',
type='brand',
json_field='widget',
parents=widget_service.parents,
slugs=['dark-theme'],
slugs_only=True,
)
Note that the underlying query still fetches all resource records (to check inherits and maintain correct cache invalidation), it just excludes them from the merge.
FAQs
JSON Merge Tree (built on top of json-multi-merge) merges JSON objects in a hierarchy, allowing customization of how objects are merged.
We found that json-merge-tree demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.

Security News
NIST will stop enriching most CVEs under a new risk-based model, narrowing the NVD's scope as vulnerability submissions continue to surge.

Company News
/Security News
Socket is an initial recipient of OpenAI's Cybersecurity Grant Program, which commits $10M in API credits to defenders securing open source software.