0

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.

1 Answer 1

1

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"
  }
]

Demo

Sign up to request clarification or add additional context in comments.

1 Comment

One alternative to ... // empty would be: ... | [type filter]. So in this case, .emotes[].images | arrays. Reads nicer IMO.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.