Compare and map values between two array of objects in JOLT - json

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

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": {
"*": ""
}
}
]

Jolt to check non-null value and then add a new field

Below is the json input file.
{
"data": {
"no1": 12345,
"cp1": {
"cp1_sub1": "sub1",
"cp2_sub2": "sub2"
},
"cp2": null
}
}
the expectation is like adding a "no1" field inside the "cp1" and "cp2" if it is non-null object.
I have tried with following 2 ways but non of it is working. Is there any way to check null or add a null value with a new key in jolt?
Try 1:
[
{
"operation": "shift",
"spec": {
"data": {
"*": "data.&",
"cp1": {
"#(1,no1)": "data.cp1.no1",
"*": "data.cp1.&"
},
"cp2": {
"#(1,no1)": "data.cp2.no1",
"*": "data.cp2.&"
}
}
}
}
]
Here the problem is it adds "no1" in "cp2".
Try 2:
[
{
"operation": "shift",
"spec": {
"data": {
"*": "data.&",
"cp1": {
"#(1,no1)": "data.cp1.no1",
"*": "data.cp1.&"
},
"cp2": {
"*": {
"#(1,no1)": "data.cp2.no1",
"*": "data.cp2.&"
}
}
}
}
}
]
Here the problem is "cp2" it self removed. If this is correct then how we can add back "cp2": null
If the input is an array like this.
{
"abc": "def",
"data": [
{
"no1": 12345,
"cp1": {
"cp1_sub1": "sub1",
"cp2_sub2": "sub2"
},
"cp2": null
},
{
"no1": 56789,
"cp1": null,
"cp2": {
"cp3_sub1": "sub1",
"cp3_sub2": "sub2"
}
}
]
}
I tried with the same way with #Barbaros answer. but the last step is not working for the array part. Considering cp1 and cp2 as the fixed key and not a dynamic.
[
{
// multiplex the current JSON value in order to use one for logical comparisons later
"operation": "shift",
"spec": {
"data": {
"*": {
"*": "data.[&1].&",
"#(0,cp1)": "data.[&1].temp-cp1",
"#(0,cp2)": "data.[&1].temp-cp2"
}
}
}
},
{
// determine whether the value of the attribute/object is "null"
"operation": "modify-overwrite-beta",
"spec": {
"data": {
"*": {
"temp-cp1": ["=toString", "NuLllLL"],
"temp-cp2": ["=toString", "NuLllLL"]
}
}
}
}
]
We should generate a comparison identifier in order to be prepared for upcoming conditional which will be held within a consequent transformation
[
{
// multiplex the current JSON value in order to use one for logical comparisons later
"operation": "shift",
"spec": {
"data": {
"*": "&1.&",
"#": "New_&1.&"
}
}
},
{
// determine whether the value of the attribute/object is "null"
"operation": "modify-overwrite-beta",
"spec": {
"New_data": {
"data": {
"cp*": ["=toString", "NuLllLL"]
}
}
}
},
{
// use conditional logic based on the results determined within the previous transformation
"operation": "shift",
"spec": {
"New_data": {
"data": {
"cp*": {
"NuLllLL": {
"#(4,data.&1)": "&3.&2"
},
"*": {
"#1": "&3.&2.new_&2",
"#(4,data.no1)": "&3.&2.no1"
},
"*}": { // check out if it's an object
"#(4,data.&1)": "&3.&2",
"#(4,data.no1)": "&3.&2.no1"
}
},
"*": "&1.&"
}
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is

JOLT Specification to Transform field value to field name

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