ponnuru sarada
ponnuru sarada

Reputation: 11

JOLT Transformation with Arrays

Looking for help on Jolt transformation below

{
  "consents": [
    {
      "consent-data": [
        {
          "consent-key": "DOB",
          "consent-value": "02/2006"
        }
      ],
      "consent-id": "123",
      "consent-indicator": "true",
      "consent-type": "COPPA",
      "resource-id": "456"
    },
    {
      "consent-data": [
        {
          "consent-key": "DOB",
          "consent-value": "02/2006"
        }
      ],
      "consent-id": "567",
      "consent-indicator": "true",
      "consent-type": "COPPA",
      "resource-id": "678"
    }
  ]
}

Expected Output :

{
  "Consents": [
    {
      "consentDataResponse": [
        {
          "consentKey": "DOB",
          "consentValue": "02/2006"
        }
      ],
      "consentId": "123",
      "consentIndicator": "true",
      "consentType": "COPPA",
      "resourceId": "456"
    },
    {
      "consentDataResponse": [
        {
          "consentKey": "DOB",
          "consentValue": "02/2006"
        }
      ],
      "consentId": "567",
      "consentIndicator": "true",
      "consentType": "COPPA",
      "resourceId": "678"
    }
  ]
}

Trasformation iam using :

[
  {
    "operation": "shift",
    "spec": {
      "consents": {
        "*": {
          "consent-id": "consents[&1].retailerId",
          "consent-indicator": "consents[&1].consentIndicator",
          "consent-type": "consents[&1].consentType",
          "resource-id": "consents[&1].resoureId",
          "consent-data": {
            // loop thru the orderItems array
            "*": {
              "consent-key": "consentDataResponse[&1].consentKey",
              "consent-value": "consentDataResponse[&1].consentValue"
            }
          }
        }
      }
    }
  }

]

Current output :

{
  "consentDataResponse" : [ {
    "consentKey" : [ "DOB", "DOB1" ],
    "consentValue" : [ "02/2006", "03/2006" ]
  } ],
  "consents" : [ {
    "consentIndicator" : "true",
    "consentType" : "COPPA",
    "resoureId" : "22=",
    "retailerId" : "11="
  }, {
    "consentIndicator" : "false",
    "consentType" : "COPPA1",
    "resoureId" : "44=",
    "retailerId" : "33="
  } ]
}

Note - Here, consents and consentDataResponse will be repeated, currently I am getting consentDataResponse as a separate object with consolidated values but it should be per consents. Could you please help me here

Upvotes: 1

Views: 54

Answers (1)

Arun Sai
Arun Sai

Reputation: 1972

[
  {
    "operation": "shift",
    "spec": {
      "consents": {
        "*": {
          "consent-data": {
            "*": {
              "consent-key": "Consents[&3].consentDataResponse[&1].consentKey",
              "consent-value": "Consents[&3].consentDataResponse[&1].consentValue"
            }
          },
          "consent-id": "Consents[&1].consentId",
          "consent-indicator": "Consents[&1].consentIndicator",
          "consent-type": "Consents[&1].consentType",
          "resource-id": "Consents[&1].resourceId"
        }
      }
    }
  }
]

You did almost correct and just added indexing inside the consent-data loop.

Upvotes: 1

Related Questions