JOLT Specification to Transform field value to field name - json

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"
}
}

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

You would use this for solution this problem.
[
{
"operation": "shift",
"spec": {
"action": {
"*": "action.&",
"records": {
"*": "action.records.#recordid"
}
}
}
}
]

Related

JOLT Specification to Transform JSON field value to field name

I am trying to convert the below JSON payload into a JSON which has the field name as the value of the field:
The jolt file that I have is working if field values are different. But if field values are the same then it is giving an array in the response.
Can you please help to provide jolt specifications for this?
Input JSON Payload :
{
"action": {
"allowPartialSuccess": true,
"records": [
{
"ZuoraSubscriptionExtId": "962041ad-e673-492a-a071-5e0ab74ea001",
"CPQSubscriptionID__c": "5640029343"
},
{
"ZuoraSubscriptionExtId": "962041ad-e673-492a-a071-5e0ab74ea001",
"CPQSubscriptionID__c": "5640029343"
}
],
"type": "update"
}
}
`
Expected output JSON Payload :
{
"action" : {
"allowPartialSuccess" : true,
"records" : {
"962041ad-e673-492a-a071-5e0ab74ea001" : {
"CPQSubscriptionID__c" : "5640029343"
}
}, {
"962041ad-e673-492a-a071-5e0ab74ea001" : {
"CPQSubscriptionID__c" : "5640029343"
}
} ,
"type" : "update"
}
}
JOLT specification that I am using :
[
{
"operation": "shift",
"spec": {
"action": {
"allowPartialSuccess": "action.allowPartialSuccess",
"records": {
"*": {
"CPQSubscriptionID__c": "action.records.#(1,ZuoraSubscriptionExtId).CPQSubscriptionID__c"
}
},
"type": "action.type"
}
}
}
]
You can use this shift transformation spec
[
{
"operation": "shift",
"spec": {
"*": {
"*": "&1.&", // &1 replicates the literal "action"
"records": {
"*": {
"C*": "&3.&2.#(1,ZuoraSubscriptionExtId).&" // &3 replicates the literal "action" (by going three level up the tree), &2 for "records", & replicates the current level attribute
}
}
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is
Your expected output is wrong in the question.
If you want an object in your record, You can use this spec:
[
{
"operation": "shift",
"spec": {
"*": {
"&": "&1.&",
"records": {
"*": {
"CPQSubscriptionID__c": "&3.&2.#(1,ZuoraSubscriptionExtId).CPQSubscriptionID__c"
}
}
}
}
},
{
"operation": "cardinality",
"spec": {
"*": {
"records": {
"*": {
"*": "ONE"
}
}
}
}
}
]
And if you want an array in your output. You can use this spec:
[
{
"operation": "shift",
"spec": {
"*": {
"*": "&1.&",
"records": {
"*": {
"CPQSubscriptionID__c": "&3.&2[].#(1,ZuoraSubscriptionExtId).CPQSubscriptionID__c"
}
}
}
}
}
]

JOLT Specification for converting filed value to field name

I am trying to convert the below JSON payload into a JSON that has the field name as the value of the field:
The jolt file that I have is working if field values are different. But if field values are the same then it is giving an array in the response.
Can you please help to provide jolt specifications for this?
Input JSON Payload:
{
"action": {
"Success": true,
"records": [
{
"Id": "Test_abc",
"SubscriptionID": "ID_1"
},
{
"Id": "Test_abc",
"SubscriptionID": "ID_2"
},
{
"Id": "Test_xyz",
"SubscriptionID": "ID_3"
}
],
"type": "update"
}
}
Expected output:
{
"action": {
"Success": true,
"records": {
"Test_abc": {
"SubscriptionID": "ID_1"
},
"Test_abc": {
"SubscriptionID": "ID_2"
},
"Test_xyz": {
"SubscriptionID": "ID_3"
}
},
"type": "update"
}
}
Solution not found yet.
You can use the following shift transformation spec
[
{
"operation": "shift",
"spec": {
"*": {
"*": "&1.&", // &1 replicates the literal "action"
"records": {
"*": {
"S*": "&3.&2.#(1,Id).&" // &3 replicates the literal "action" (by going three level up the tree), &2 for "records", & replicates the current level attribute
}
}
}
}
}
]
presumingly converting one of the Id value from Test_abc to another one such as Test_def, since the desired output is wrong as a JSON value(There cannot be more than one object with the same tag at the same level)
Your output is wrong. You can't have multiple objects with the same keys.
If You want to support all objects of records array, You should bring them into an array like this spec:
[
{
"operation": "shift",
"spec": {
"*": {
"*": "&1.&",
"records": {
"*": {
"S*ID": "&3.&2[].#(1,Id).&"
}
}
}
}
}
]
But, if you want to have an object as your records in the output, You should remove the duplicate ID like this spec:
[
{
"operation": "shift",
"spec": {
"*": {
"*": "&1.&",
"records": {
"*": {
"S*ID": "&3.&2.#(1,Id).&"
}
}
}
}
},
{
"operation": "cardinality",
"spec": {
"*": {
"records": {
"*": {
"*": "ONE"
}
}
}
}
}
]

JOLT JSON transform values from one-to-many to one-to-one

I'm trying to map one key to each value in array to a new array by using JOLT.
Could someone please help give me a solution for this:
My JSON:
[
{
"person_id": "1",
"resources": ["asd", "zxc"]
},
{
"person_id": "2",
"resources": ["ghj", "asd"]
}
]
And my expected JSON:
[
{
"person_id": "1",
"resource": "asd"
},
{
"person_id": "1",
"resource": "zxc"
},
{
"person_id": "2",
"resource": "ghj"
},
{
"person_id": "2",
"resource": "asd"
}
]
I had tried this Jolt Specification
[
{
"operation": "shift",
"spec": {
"*": {
"resources": {
"*": {
"#(2,person_id)": "[&].person_id",
"#": "[&].resource"
}
}
}
}
}
]
But no luck it always maps all values at the same index to 1 array.
You can use two consecutive shift transformation specs by walking through the resources array within the first one such that
[
{
"operation": "shift",
"spec": {
"*": {
"*s": { // this tecnique stands for extracting by the replacement of the asterisk through use of &(2,1) below
"*": {
"#(2,person_id)": "[&3].&1.person_id", // to separate by indexes of the array to generate three level for three independent objects
"#": "[&3].&1.&(2,1)"
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": ""
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is
You can use this jolt spec:
[
{
"operation": "shift",
"spec": {
"*": {
"*s": {
"*": {
"#(2,person_id)": "#(1).person_id",
"#": "#(1).&(2,1)"
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": ""
}
}
]

Compare and map values between two array of objects in JOLT

I need to map the headers and row values based on the dataKey value.
Input JSON
{
"headers": [
{
"dataKey": "col-0",
"displayName": "Product"
},
{
"dataKey": "col-1",
"displayName": "Dimension"
},
{
"dataKey": "col-2",
"displayName": "Output type "
}
],
"rows": [{
"col-0": "Medium ",
"col-1": "300x250",
"col-2": "HTML [Animate]"
}]
}
Expected Output
{
"Data" : {
"1" : {
"Product":"Medium",
"Dimension":"300x250",
"Output type":"HTML [Animate]"
}
}
}
Spec:
This is the JOLT Spec i am using currently it is not producing the expected ouptut
[
{
"operation": "shift",
"spec": {
"headers": {
"*": {
"displayName": {
"*": {
"#(2,dataKey)": {
"$": "Data.1.&",
"*": {
"#(6,rows.&)": "Data.1.&"
}
}
}
}
}
}
}
}
]
Please provide the jolt spec for above scanario. i tried but not able to get the expected result.
[
{
// segregate values of the same key and form respective arrays.
"operation": "shift",
"spec": {
"headers": {
"*": {
"displayName": "#(1,dataKey)"
}
},
"rows": {
"*": {
"*": "&"
}
}
}
},
{
// put every value array into temp array
"operation": "shift",
"spec": {
"*": "temp[]"
}
},
{
// map first index element as key and second index element as a value into the output
"operation": "shift",
"spec": {
"temp": {
"*": {
"1": "Data.1.#(1,[0])"
}
}
}
}
]

Json array elements duplication using jolt

I want to know if jolt can handle the following transformation:
{
"interface": [
{
"field": "A",
"ip": [
"1.1.1.1",
"1.1.1.2"
]
},
{
"field": "B",
"ip": [
"1.1.1.3"
]
}
]
}
to
{
"interface": [
{
"field": "A",
"ip": "1.1.1.1"
},
{
"field": "A",
"ip": "1.1.1.2"
},
{
"field": "B",
"ip": "1.1.1.3"
}
]
}
i.e for a JSON array containing a child array, create one version of parent item for each of its child array item.
Can jolt do that?
This can be achieved by doing a two stage shift:
Create an array of arrays
Then flatten the array of arrays
[
{
"operation": "shift",
"spec": {
"interface": {
"*": {
"ip": {
"*": {
"#": "[&3].[&1].ip",
"#(2,field)": "[&3].[&1].field"
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": "interface.[]"
}
}
}
]
It is not complete answer, but maybe that helps you somehow or someone else could help?
[
{
"operation": "shift",
"spec": {
"interface": {
"*": {
"ip": {
"*": {
"#": "ip",
"#(2,field)": "field"
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"ip": {
"*": {
"#": "interface[&1].ip",
"#(2,field)": {
"???": "interface[&2].field" // How to take proper index of an array?
}
}
}
}
}
]