jsontransformationjolt

Extract key from object and match that key in array of object and extract value using jolt transform


{
  "reason": {
    "id": 1234,
    "value": "Date changed",
    "key": "2124"
  },
  "updatedEntity": [
    {
      "key": "2124",
      "value": "US california"
    },
    {
      "key": "2123",
      "value": "Dallas"
    },
    {
      "key": "2122",
      "value": "Texas"
    }
  ]
}

i have input data as stated above. I want to apply jolt transformation in such way the key in reason object matches key in updatedEntity array and in output i ll get value..

{
  "payload" :"US california"
}

i have written jolt spec but seems it is not working

[
  {
    "operation": "shift",
    "spec": {
      "reason": {
        "key": "reasonKey"
      },
      "updatedEntity": {
        "*": {
          "key": {
            "reasonKey": {
              "@(2,value)": "payload"
            }
          }
        }
      }
    }
  }
]

Solution

  • You can use the following transformation spec :

    [
      {
        "operation": "shift",
        "spec": {
          "updatedEntity": {
            "*": {
              "@value": "@key"
            }
          },
          "@reason.key": "payload"
        }
      },
      {
        "operation": "shift",
        "spec": {
          "payload": {
            "*": {
              "@2,&": "&2"//match payload's value @2,& with the key &2
            }
          }
        }
      }
    ]
    

    the demo on the site Jolt Transform Demo Using v0.1.1 is :

    Jolt Transform Demo Using v0.1.1