Combine several JSON files into one document with a jq filter: append arrays, de-duplicate, deep-merge objects, or bundle by file name. It runs in your browser.
What it does
Add the files, in the order they should be merged, and choose how: the files are read in that order and put in one array, which is what the filter is given, the way jq -s does it. In the filter . is that array and $files is the array of the files' names. The result updates as you change the list or the filter.
Ready-made filters
- Append arrays:
add. Joins arrays end to end. Objects work too: their keys are combined and a later file wins a clash. - Append, sorted and de-duplicated:
add | unique. jq's unique also sorts. - Deep-merge objects:
reduce .[] as $file ({}; . * $file). Key by key, all the way down. - Bundle by file name: each file whole, labelled with its name.
A filter of your own
The filter is ordinary jq, so anything it can do is a merge. Some that come up:
add | group_by(.id) | map(add): records with the same id are combined, later files win.add | unique_by(.id): one record per id, the first seen.[range(0; length) as $i | .[$i][] | . + {from: $files[$i]}]: tag every record with its file.map(select(type == "array")) | add: skip the files that are not arrays.
A filter that gives several outputs gives them as one array.
Exact, and in your tab
Every digit of a number is kept, and the order of an object's keys. The files are read and merged by the same Rust engine as the desktop app, compiled to WebAssembly: nothing is uploaded. This page takes up to 48 MB in all, 10 MB a file; the desktop app merges up to 128 MB at a time. To query the result use Open in Query, to compare it with another document use Diff, to check it Validate.
Lessons
Samples to study and to try: ▶ Try it puts them in the page.
Append files
Merge reads the files of its list, in that order, into one array — . in the filter — and the filter makes one document of it, as jq -s does. The ready-made filters are in Merge as; add joins arrays end to end.
Two months of orders, one list
Filter: add
Samples: orders-jan.json, orders-feb.json
orders-jan.json
[
{ "id": 101, "customer": "Ada", "total": 25.5 },
{ "id": 102, "customer": "Linus", "total": 12 }
]orders-feb.json
[
{ "id": 103, "customer": "Grace", "total": 40.25 }
]What it gives: merged from 2 files
[
{
"id": 101,
"customer": "Ada",
"total": 25.5
},
{
"id": 102,
"customer": "Linus",
"total": 12
},
{
"id": 103,
"customer": "Grace",
"total": 40.25
}
]. is [jan, feb]: an array of the two files, in the order of the list.add joins them end to end: three orders, January's first.- Move a file up or down the list and the order of the result follows.
Tags of two files, each once
Filter: add | unique
Samples: tags-a.json, tags-b.json
tags-a.json
["rust", "sql", "go"]
tags-b.json
["sql", "python", "rust"]
What it gives: merged from 2 files
[
"go",
"python",
"rust",
"sql"
]
unique takes out what is there twice — and sorts what is left.
Deep-merge settings
Objects can be merged key by key, all the way down: settings for every place, then what one place changes, then what one machine changes. jq's * merges two objects; reduce lays each file over the ones before it.
Defaults, production, and a local change
Filter: reduce .[] as $file ({}; . * $file)
Samples: defaults.json, production.json, local.json
defaults.json
{
"server": { "host": "0.0.0.0", "port": 8080, "tls": false },
"log": { "level": "info", "format": "text" },
"features": ["search"]
}production.json
{
"server": { "tls": true },
"log": { "level": "warn" },
"features": ["search", "export"]
}local.json
{
"server": { "port": 3000 },
"log": { "format": "pretty" }
}What it gives: merged from 3 files
{
"server": {
"host": "0.0.0.0",
"port": 3000,
"tls": true
},
"log": {
"level": "warn",
"format": "pretty"
},
"features": [
"search",
"export"
]
}server keeps host from the defaults, takes tls from production and port from local: a later file wins a clash.- Arrays are not merged: production's
features takes the place of the defaults'. - A key no later file has keeps its value:
log.format is local's, log.level production's.
Good to know
- The order of the list is the order of the layers: put the general file first.
Your own filter
Any jq filter can merge: write it in the filter box. . is the array of the files and $files the array of their names.
Stock of two warehouses, summed by product
Filter: add | group_by(.sku) | map({sku: .[0].sku, qty: (map(.qty) | add)})
Samples: berlin.json, paris.json
berlin.json
[
{ "sku": "A-1", "qty": 5 },
{ "sku": "B-2", "qty": 1 }
]paris.json
[
{ "sku": "A-1", "qty": 2 },
{ "sku": "C-3", "qty": 7 }
]What it gives: merged from 2 files
[
{
"sku": "A-1",
"qty": 7
},
{
"sku": "B-2",
"qty": 1
},
{
"sku": "C-3",
"qty": 7
}
]add makes one list of both; group_by(.sku) puts the rows of a product together.map({…}) makes one row of each group, its qty the sum of the group's.
Which file each row came from
Filter: [range(0; length) as $i | .[$i][] | . + {from: $files[$i]}]
Samples: berlin.json, paris.json
berlin.json
[
{ "sku": "A-1", "qty": 5 },
{ "sku": "B-2", "qty": 1 }
]paris.json
[
{ "sku": "A-1", "qty": 2 },
{ "sku": "C-3", "qty": 7 }
]What it gives: merged from 2 files
[
{
"sku": "A-1",
"qty": 5,
"from": "berlin.json"
},
{
"sku": "B-2",
"qty": 1,
"from": "berlin.json"
},
{
"sku": "A-1",
"qty": 2,
"from": "paris.json"
},
{
"sku": "C-3",
"qty": 7,
"from": "paris.json"
}
]$files[$i] is the name of the file .[$i] is — so each row says where it came from.
Your own queries, schemas, patches, documents and web addresses, kept in folders, with a sample if you like. It is the desktop app's workspace: a folder of the app opens here. This needs JavaScript.