Danish M
Danish M

Reputation: 105

Selectively get certain arrays from inside an array in JSON using JOLT

I've been trying to get some fields out of a very long and complicated json format but not getting the output I want.

MY current spec obtains all events from the event array and lists them in the output. I'm unsure how to select specific events and only output those. Am not quite sure of the syntax

My JSON:

{
  "rootid": "19718",
  "clloadm": "2021-06-01T22:40:02",
  "clload": "2021-06-01T21:21:39",
  "date": "2021-05-25T21:52:30",
  "events": [
    {
      "done": {
        "id": "e0",
        "value": "2021-05-29T08:08:19"
      },
      "id": "e0_event",
      "started": {
        "id": "e0",
        "value": "2021-05-29T08:08:19"
      },
      "status": "complete"
    },
    {
      "done": {
        "id": "e1",
        "value": "2021-05-27T02:20:25"
      },
      "id": "e1_event",
      "started": {
        "id": "e1",
        "value": "2021-05-27T02:20:25"
      },
      "status": "complete"
    },
    {
      "done": {
        "id": "e2",
        "value": "2021-05-29T08:08:19"
      },
      "id": "e2_event",
      "started": {
        "id": "e2",
        "value": "2021-05-29T08:08:19"
      },
      "status": "complete"
    },
    {
      "done": {
        "id": "e3",
        "value": "2021-05-29T08:08:19"
      },
      "id": "e3_event",
      "started": {
        "id": "e3",
        "value": "2021-05-29T08:08:19"
      },
      "status": "complete"
    },
    {
      "done": {
        "id": "e4",
        "value": "2021-05-29T08:08:19"
      },
      "id": "e4_event",
      "started": {
        "id": "e4",
        "value": "2021-05-29T08:08:19"
      },
      "status": "complete"
    }
  ],
  "ids": [
    {
      "id": "id",
      "source": "source",
      "value": "value"
    },
    {
      "id": "new_id",
      "source": "new_source",
      "value": "value"
    }
  ]
}

My Jolt Spec that gets all events for now:

[
  {
    "operation": "shift",
    "spec": {
      "rootid": "rootid",
      "clloadm": "clloadm",
      "clload": "clload",
      "date": "date",
      "events": {
        "*": {
          "*": {
            "@value": "@id"
          }
        }
      },
      "ids": {
        "*": {
          "@value": "@id"
        }
      }
    }
  }
]

The output I get:

{
  "rootid" : "19718",
  "clloadm" : "2021-06-01T22:40:02",
  "clload" : "2021-06-01T21:21:39",
  "date" : "2021-05-25T21:52:30",
  "e0" : [ "2021-05-29T08:08:19", "2021-05-29T08:08:19" ],
  "e1" : [ "2021-05-27T02:20:25", "2021-05-27T02:20:25" ],
  "e2" : [ "2021-05-29T08:08:19", "2021-05-29T08:08:19" ],
  "e3" : [ "2021-05-29T08:08:19", "2021-05-29T08:08:19" ],
  "e4" : [ "2021-05-29T08:08:19", "2021-05-29T08:08:19" ],
  "id" : "value",
  "new_id" : "value"
}

The output I would like

{
  "rootid" : "19718",
  "clloadm" : "2021-06-01T22:40:02",
  "clload" : "2021-06-01T21:21:39",
  "date" : "2021-05-25T21:52:30",
  "e0" : [ "2021-05-29T08:08:19", "2021-05-29T08:08:19" ],
  "e4" : [ "2021-05-29T08:08:19", "2021-05-29T08:08:19" ],
  "id" : "value",
  "new_id" : "value"
}

Upvotes: 1

Views: 293

Answers (1)

Barbaros Özhan
Barbaros Özhan

Reputation: 65228

You can write the individual keys e0 and e4 as conditional cases for @id key while rewriting the rest of the key-value pairs through "*":"&" representation such as

[
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "events": {
        "*": {
          "*": {
            "@id": {
              "e0": { "@(2,value)": "&" },
              "e4": { "@(2,value)": "&" }
            }
          }
        }
      },
      "ids": {
        "*": {
          "@value": "@id"
        }
      }
    }
  }
]

Upvotes: 2

Related Questions