Reputation: 26640
How to transform this:
[
{
"author": {
"name": "Alice"
},
"message": "Text 1"
},
{
"author": {
"name": "Bob"
},
"message": "Text 2"
},
{
"author": {
"name": "Charlie"
},
"emotes": [
{
"images": [
{
"url": "https://www.youtube.com/s/gaming/emoji/828cb648/emoji_u1f600.svg"
},
{
"url": "https://www.youtube.com/s/gaming/emoji/828cb648/emoji_u1f600.svg"
},
{
"url": "https://some-host.com/some-url.svg"
}
],
"name": ":grinning_face:"
}
],
"message": "Text 3"
},
{
"author": {
"name": "Dave"
},
"message": "Text 4"
},
{
"author": {
"name": "Erin"
},
"message": "Text 5"
},
{
"author": {
"name": "Faythe"
},
"emotes": [
{
"images": [
{
"url": "https://www.youtube.com/s/gaming/emoji/828cb648/emoji_u1f44b.svg"
},
{
"url": "https://www.youtube.com/s/gaming/emoji/828cb648/emoji_u1f44b.svg"
},
{
"url": "https://some-host.com/some-other-url.svg"
}
],
"name": ":waving_hand:"
}
],
"message": "Text 6"
}
]
Into this:
[
{
"author": "Alice",
"message": "Text 1"
},
{
"author": "Bob",
"message": "Text 2"
},
{
"author": "Charlie",
"message": "Text 3",
"emotes": [
{
"name": ":grinning_face:",
"images": [
"https://www.youtube.com/s/gaming/emoji/828cb648/emoji_u1f600.svg",
"https://some-host.com/some-url.svg"
]
}
]
},
{
"author": "Dave",
"message": "Text 4"
},
{
"author": "Erin",
"message": "Text 5"
},
{
"author": "Faythe",
"message": "Text 6",
"emotes": [
{
"name": ":waving_hand:",
"images": [
"https://www.youtube.com/s/gaming/emoji/828cb648/emoji_u1f44b.svg",
"https://some-host.com/some-other-url.svg"
]
}
]
}
]
using jq?
I have no idea how to map nested arrays in "emotes" nodes.
Upvotes: 0
Views: 743
Reputation: 36033
Use (…)? // empty
to filter for in-depth existence, unique
to remove duplicates, and |=
to update:
jq 'map(.author |= .name | ((.emotes[].images)? // empty) |= (map(.url) | unique))'
[
{
"author": "Alice",
"message": "Text 1"
},
{
"author": "Bob",
"message": "Text 2"
},
{
"author": "Charlie",
"emotes": [
{
"images": [
"https://some-host.com/some-url.svg",
"https://www.youtube.com/s/gaming/emoji/828cb648/emoji_u1f600.svg"
],
"name": ":grinning_face:"
}
],
"message": "Text 3"
},
{
"author": "Dave",
"message": "Text 4"
},
{
"author": "Erin",
"message": "Text 5"
},
{
"author": "Faythe",
"emotes": [
{
"images": [
"https://some-host.com/some-other-url.svg",
"https://www.youtube.com/s/gaming/emoji/828cb648/emoji_u1f44b.svg"
],
"name": ":waving_hand:"
}
],
"message": "Text 6"
}
]
Upvotes: 1