0

I have following JSON input

{
  "events": [
    {
      "cluster_id": "0717-035521-puny598",
      "timestamp": 1535540053785,
      "type": "TERMINATING",
      "details": {
        "reason": {
          "code": "INACTIVITY",
          "parameters": {
            "inactivity_duration_min": "15"
          }
        }
      }
    },
    {
      "cluster_id": "0717-035521-puny598",
      "timestamp": 1535537117300,
      "type": "EXPANDED_DISK",
      "details": {
        "previous_disk_size": 29454626816,
        "disk_size": 136828809216,
        "free_space": 17151311872,
        "instance_id": "6cea5c332af94d7f85aff23e5d8cea37"
      }
    }
  ]
}

I want to convert it into following.

1) add one static key:value in each object of "events" array. 2) remove one element "type" from each object of "events" array. 3) rest all values should be same in i/p and o/p. "details" is a object with no specific structure.

{
  "events": [
    {
      "new_key" : "new_value",
      "cluster_id": "0717-035521-puny598",
      "timestamp": 1535540053785,

      "details": {
        "reason": {
          "code": "INACTIVITY",
          "parameters": {
            "inactivity_duration_min": "15"
          }
        }
      }
    },
    {
      "new_key" : "new_value",
      "cluster_id": "0717-035521-puny598",
      "timestamp": 1535537117300,

      "details": {
        "previous_disk_size": 29454626816,
        "disk_size": 136828809216,
        "free_space": 17151311872,
        "instance_id": "6cea5c332af94d7f85aff23e5d8cea37"
      }
    }
  ]
}
Rakesh Prasad
  • 602
  • 1
  • 13
  • 32

1 Answers1

1

The following chain spec should work:

[
  {
    "operation": "default",
    "spec": {
      "events[]": {
        "*": {
          "new-key": "new-value"
        }
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "events": {
        "*": {
          "type": ""
        }
      }
    }
  }
]
mattyb
  • 11,693
  • 15
  • 20
  • 1
    perfect, it worked. I am not able to find good tutorial on JOLT, can you please direct me to a good one? thr seems to be only few ppl who answers most of JOLT queries. – Rakesh Prasad Aug 31 '18 at 01:15
  • I learned it from the Javadoc for various operations (like Shift: https://github.com/bazaarvoice/jolt/blob/master/jolt-core/src/main/java/com/bazaarvoice/jolt/Shiftr.java#L184) and with trial-and-error using the very helpful app here: http://jolt-demo.appspot.com/#inception – mattyb Aug 31 '18 at 18:55