2

I have JSON that looks like this:

    [
      {
        "fields": {
          "versions": [
            {
              "id": "36143",
              "name": "ST card"
            },
            {
              "id": "36144",
              "description": "Acceptance test card",
              "name": "AT card"
            }
          ],
          "severity": {
            "value": "B-Serious",
            "id": "14231"
          }
        }
      },
      {
        "fields": {
          "versions": [
            {
              "id": "36145",
              "name": "ST card"
            }
          ],
          "severity": {
            "value": "C-Limited",
            "id": "14235"
          }
        }
      }
    ]

I want to convert it with jq to this:

    [
      {
        "id": "36143",
        "name": "ST card"
        "value": "B-Serious"
      },
      {
        "id": "36144",
        "name": "AT card"
        "value": "B-Serious"
      },
      {
        "id": "36145",
        "name": "ST card"
        "value": "C-Limited"
      }
    ]

Note that the first object has 2 versions, and the same severity. I have tried jq's group_by and map functions but haven't been too successful. Please help :)

Somaiah Kumbera
  • 7,063
  • 4
  • 43
  • 44

1 Answers1

6

This should work. You wouldn't want to use a group_by here, you would do that if you were trying to go from more to less, we're going the other way.

You're combining the different versions with the corresponding severity. Here's how you could do that.

map(.fields | (.versions[] | { id, name }) + { value: .severity.value })
Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272