1
votes

I've been trying to figure out how to get the first value out of an array in my events array in this JSON.

I think I must do something with Cardinality but I can't quite figure it out so any help would be appreciated.

This is what my JSON looks like

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

My Jolt Spec

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

The result 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" ],
  "e4" : [ "2021-05-29T08:08:19", "2021-05-29T08:08:19" ],
  "id" : "value",
  "new_id" : "value"
}

I would like the result to be more like this where only the first value is selected:

{
  "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",
  "e4" : "2021-05-29T08:08:19",
  "id" : "value",
  "new_id" : "value"
}

Edit: I've tried adding the cardinality into my spec, but I can't seem to get the result I want.

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

1 Answers

1
votes

Yes, you just can add a cardinality transformation such as

{
  "operation": "cardinality",
  "spec": {
    "*": "ONE"
  }
}

or optionally use e* wildcard just to restrict the result for the arrays with keys starting with the letter e such as

{
  "operation": "cardinality",
  "spec": {
    "e*": "ONE"
  }
}

Edit : just need to add to the current spec. So, you can use the following :

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