Jolt transformation with key also in the output

Viewed 56

Below is the input JSON:

{
  "statusCode": 200,
  "statusMessage": {
    "Acked-by": {
      "checksum": 3103748203,
      "offset": 0,
      "partition": 3,
      "timestamp": 1648884230874,
      "topic": "ix_ingress_topic"
    }
  }
}

Expected output is :

{
  "$set": {
    "crmKafkaResponse": {
      "statusMessage": {
        "Acked-by": {
          "checksum": 3103748203,
          "offset": 0,
          "partition": 3,
          "timestamp": 1648884230874,
          "topic": "ix_ingress_topic"
        }
      }
    },
    "ingressKafkaResponse": "success"
  }
}

I tried using the below jolt spec:

[
  {
    "operation": "shift",
    "spec": {
      "statusMessage": "\\$set.crmKafkaResponse"
    }
  },
  {
    "operation": "default",
    "spec": {
      "$set": {
        "ingressKafkaResponse": "success"
      }
    }
  }
]

I'm getting the below output:


{
  "$set" : {
    "crmKafkaResponse" : {
      "Acked-by" : {
        "checksum" : 3103748203,
        "offset" : 0,
        "partition" : 3,
        "timestamp" : 1648884230874,
        "topic" : "ix_ingress_topic"
      }
    },
    "ingressKafkaResponse" : "success"
  }
}

Need help to arrive at the expected output. I went through documentation but wanted to get the key also in the output. Have been trying different things here:

https://jolt-demo.appspot.com/#listKeys

1 Answers

Just add an ampersand to represent statusMessage to the line which starts with it in order to nest inner object(Acked-by) by statusMessage also such as

[
  {
    "operation": "shift",
    "spec": {
      "statusMessage": "\\$set.crmKafkaResponse.&"
    }
  },
  {
    "operation": "default",
    "spec": {
      "$set": {
        "ingressKafkaResponse": "success"
      }
    }
  }
]

alternatively, you can directly use only one shift transformation such as

[
  {
    "operation": "shift",
    "spec": {
      "statusMessage": "\\$set.crmKafkaResponse.&",
      "#success": "\\$set.ingressKafkaResponse"
    }
  }
]

in order to get the identical result

Related