Skip to content

Instantly share code, notes, and snippets.

@sonnyksimon
Last active March 13, 2020 17:47
Show Gist options
  • Save sonnyksimon/3c01f5a4a6da047a045acdf6dabf1c40 to your computer and use it in GitHub Desktop.
Save sonnyksimon/3c01f5a4a6da047a045acdf6dabf1c40 to your computer and use it in GitHub Desktop.
content mapping in jq :: the filter maps each post to its slug, and the reverse filter reverts it

content mapping in jq :: the filter maps each post to its slug, and the reverse filter reverts it

{
"content": [
{
"title": "first post",
"slug": "one",
"body": "The quick brown fox jumps over the lazy dog.",
"author": "anon",
"updated": "NaN"
},
{
"title": "another one",
"slug": "two",
"body": "The second brown fox jumps over the lazy dog.",
"author": "anon",
"updated": "NaN"
}
]
}
reduce .content[] as {$title, $slug, $body, $author, $updated} ({}; (.[$slug] += {$title, $body, $author, $updated}))
to_entries | map(.value += {slug:.key}) | {content:[.[].value]}
{
"one": {
"title": "first post",
"body": "The quick brown fox jumps over the lazy dog.",
"author": "anon",
"updated": "NaN"
},
"two": {
"title": "another one",
"body": "The second brown fox jumps over the lazy dog.",
"author": "anon",
"updated": "NaN"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment