JOLT Specification to Transform field value to field name

Viewed 38

I am trying to convert below json payload into a json payload which has field name as the value of the field:

Can you please help to provide jold specification for this ?

Input JSON Payload :

{
  "action": {
    "allowPartialSuccess": false,
    "records": [
      {
        "recordid": "a4c6364e-4446-47d0-b014-c20ca014fdb3",
        "ShipToCustomerTextualAddress__c": "TestAddress1",
        "ResellerPO__c": "TestAddress1",
        "InvoiceCDBID__c": "TestAddress1"
      },
      {
        "recordid": "73781a94-9660-4f69-9bde-f2bf1991317d",
        "ShipToCustomerTextualAddress__c": "TestAddress2",
        "ResellerPO__c": "TestAddress2",
        "InvoiceCDBID__c": "TestAddress2"
      }
    ],
    "type": "update"
  }
}

Desired Output Payload :

{
  "action": {
    "allowPartialSuccess": false,
    "records": {
      "a4c6364e-4446-47d0-b014-c20ca014fdb3": {
        "ShipToCustomerTextualAddress__c": "TestAddress1",
        "ResellerPO__c": "TestAddress1",
        "InvoiceCDBID__c": "TestAddress1"
      },
      "73781a94-9660-4f69-9bde-f2bf1991317d": {
        "ShipToCustomerTextualAddress__c": "TestAddress2",
        "ResellerPO__c": "TestAddress2",
        "InvoiceCDBID__c": "TestAddress2"
      }
    },
    "type": "update"
  }
}
3 Answers

This will help you resolve it. @(level,attribute) -->does the work.

[
  {
    "operation": "shift",
    "spec": {
      "action": {
        "*": "action.&",
        "records": {
          "*": {
            "ShipToCustomerTextualAddress__c": "action.records.@(1,recordid).&",
            "ResellerPO__c": "action.records.@(1,recordid).&",
            "InvoiceCDBID__c": "action.records.@(1,recordid).&"
          }
        }
      }
    }
  }
]

You can think symbolically through use of ampersands to replicate the respective values within a shift transformation, and a remove transformation in order to get rid of the undesired attribute(recordid) at the end such as

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "records": {
          "*": "&2.&1.@(0,recordid)" // &2(going two levels up) stands for replicating "action", &1 is for "records"
        },
        "*": "&1.&" // which represents the "else" case(eg. all but the "records" object); &1 is for "action", and & is for "records"
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "*": {
        "*": {
          "*": {
            "recordid": ""
          }
        }
      }
    }
  }
]

the demo on the site http://jolt-demo.appspot.com/ is

enter image description here

You would use this for solution this problem.

[
      {
        "operation": "shift",
        "spec": {
          "action": {
            "*": "action.&",
            "records": {
              "*": "action.records.@recordid"
            }
          }
        }
      }
    ]
Related