1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-02 22:58:01 +00:00

Add Zip Two JSON Files Together Based On Shared ID as a jq TIL

This commit is contained in:
jbranchaud
2023-11-28 16:16:50 -06:00
parent 4edc43e4bb
commit f03bf4cb70
2 changed files with 54 additions and 1 deletions

View File

@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
_1351 TILs and counting..._
_1352 TILs and counting..._
---
@@ -505,6 +505,7 @@ _1351 TILs and counting..._
- [Get The First Item For Every Top-Level Key](jq/get-the-first-item-for-every-top-level-key.md)
- [Reduce Object To Just Entries Of A Specific Type](jq/reduce-object-to-just-entries-of-a-specific-type.md)
- [Turn A List From A Command Into JSON](jq/turn-a-list-from-a-command-into-json.md)
- [Zip Two JSON Files Together Based On Shared ID](jq/zip-two-json-files-together-based-on-shared-id.md)
### Kitty

View File

@@ -0,0 +1,52 @@
# Zip Two JSON Files Together Based On Shared ID
Let's say we have JSON file (`list1.json`) that contains an array of objects.
Maybe they represent metadata about some books. Something like this:
```json
[
{ 'slug': 'the-subtle-knife', 'title': 'The Subtle Knife', ... },
{ 'slug': 'all-systems-red', 'title': 'All Systems Red', ... },
{ 'slug': 'piranesi-abc123', 'title': 'Piranesi', ... },
...
]
```
And then we have another JSON file (`list2.json`) in a similar format that
contains an additional piece of metadata tied to each slug:
```json
[
{ 'slug': 'the-subtle-knife', 'author': 'Philip Pullman' },
{ 'slug': 'all-systems-red', 'author': 'Martha Wells' },
{ 'slug': 'piranesi-abc123', 'author': 'Susanna Clarke' },
...
]
```
And we want to pull the details from the second file and combine them into the
first file based on that shared identifier, in this case, the `slug`.
Instead of copying over a ton of values manually or writing a full-fledged
script to do this, we can use a `jq` one-liner with the `--slurpfile` flag.
```
jq --slurpfile list1 list1.json --slurpfile list2 list2.json -n '
$list1[] as $item1
| $list2[] as $item2
| select($item1.slug == $item2.slug)
| $item1 + $item2
'
[
{ 'slug': 'the-subtle-knife', 'title': 'The Subtle Knife', 'author': 'Philip Pullman', ... },
{ 'slug': 'all-systems-red', 'title': 'All Systems Red', 'author': 'Martha Wells', ... },
{ 'slug': 'piranesi-abc123', 'title': 'Piranesi', 'author': 'Susanna Clarke', ... },
...
]
```
This reads in both files as lists into named variables, selects for the items
that have matching `slug` values, and then unions those objects together. The
result will go to standard out, but it could also be redirected into a new JSON
file.