Home > other >  Jolt Transformation - Move object to array
Jolt Transformation - Move object to array

Time:11-25

I'm coming to the conclusion that Jolt is beyond me.

With this input data:-

{
  "cluster_id": "1",
  "data": {
    "id": 1,
    "types": [
      {
        "incident_id": 10,
        "incident_ref": "AAA",
        "incident_code": "123",
        "incident_date": "2010-11-15T00:01:00Z"
      },
      {
        "incident_id": 20,
        "incident_ref": "BBB",
        "incident_code": "456",
        "incident_date": "2020-11-15T00:01:00Z"
      }
    ]
  }
}

Spec:-

[
  {
    "operation": "shift",
    "spec": {
      "cluster_id": "id",
      "data": {
        "types": {
          "*": {
            "incident_id": "incidents",
            "incident_ref": "incidents"
          }
        }
      }
    }
  }
]

Gives:-

{
  "id" : "1",
  "incidents" : [ 10, "AAA", 20, "BBB" ]
}

How would I get the result of:-

{
  "id" : "1",
  "incidents" : [
    {"id": 10, "ref": "AAA", "code": "123", date: "2010-11-15T00:01:00Z"},
    {"id": 20, "ref": "BBB", "code": "456", date: "2020-11-15T00:01:00Z"},
  ]
}

Tried a bunch of permutations but getting nowhere!

CodePudding user response:

You can use this spec:

[
  {
    "operation": "shift",
    "spec": {
      "*": "id",
      "data": {
        "types": {
          "*": {
            "incident_*": "incidents[&1].&(0,1)"
          }
        }
      }
    }
  }
]

To prevent using incident text in the spec, you can use the below spec:

[
  {
    "operation": "shift",
    "spec": {
      "*": "id",
      "data": {
        "types": {
          "*": {
            "*_*": "&(0,1)[&1].&(0,2)"
          }
        }
      }
    }
  }
]

CodePudding user response:

You can use two level of shift transformations by extracting substring from tag names such as

[
  {
    "operation": "shift",
    "spec": {
      "*": "id",
      "data": {
        "types": {
          "*": "incidents"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "id": "&",
      "*": {
        "*": {
          "*_*": "&2[&1].&(0,2)"
        }
      }
    }
  }
]
  • Related