JoltTransform convert to string - jolt

I am strugling a bit with my JoltTransform. I just need to convert the decimal characters to string, but for the life of me I cannot get it to work, as per the example it would be the policy_revision_no and the policy_endorsement_no ..
My example json file I am using is:
{
"policy_unique_reference": "TST1",
"item_unique_reference": "TST2",
"item_parent_item_unique_reference": "",
"item_type": "Cover",
"item_parent_section": "Stuff",
"item_subsection_extension": "",
"policy_revision_no": 1,
"policy_endorsement_no": 2
}
My current Jolt Transform is:
[
{
"operation": "shift",
"spec": {
"*": "Items.&"
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"Items": {
"policy_revision_no": "=toString"
}
}
}
}
]
My current result that I am getting is:
{
"Items" : {
"policy_unique_reference" : "TST1",
"item_unique_reference" : "TST2",
"item_parent_item_unique_reference" : "",
"item_type" : "Cover",
"item_parent_section" : "Stuff",
"item_subsection_extension" : "",
"policy_revision_no" : 1,
"policy_endorsement_no" : 2,
"Items" : { }
}
}
Thank you.

You can directly apply modify transform such as
[
{
"operation": "modify-overwrite-beta",
"spec": {
"policy_*": "=toString"
}
}
]
presumingly you need to apply for the elements with keys starting with policy_

Related

Jolt - Conditional update LHS value

I basically have 2 different condition which can impact the value of a key in the output.
I have following input:
{
"state": "PRESENT",
"sample": {
"type": "OP"
}
}
I have following spec:
[
{
"operation": "shift",
"spec": {
"state": {
"PRESENT": {
"#ANSWER1": "answer"
},
"*": {
"$": "answer"
}
},
"sample":{
"type":{
"OP":{
"#ANSWER2": "answer"
}
}
}
}
}
]
Now if you see that both the conditions are true, the final output is as following. Both the values come in form of an array
{
"answer" : [ "ANSWER1", "ANSWER2" ]
}
But I am looking for an output based on the second condition. Basically override the existing value in "answer" attribute:
{
"answer" : "ANSWER2"
}
You can add one more shift transformation
{
"operation": "shift",
"spec": {
"*": { "1": { "#": "&2" } }
}
}
to the existing specification in order to pick the second(the element with the index 1) element.
If picking the left element of the array was the case, then cardinality transformation might be preferred to use
{
"operation": "cardinality",
"spec": {
"*": "ONE"
}
}

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
[
{
"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.

Array mapping with JOLT

First time JOLT user trying to restructure an array into a similar multi-level array. stackoverflow is asking for less code and more words, so I hope this extra sentence covers that.
Input JSON
{
"studentIdentifier": "453089029",
"studentRegistrationNumber": "753082022",
"parentIdentifier": "1001142760",
"parentFamilyName": "lastname1001142760",
"relationshipIdentifier": "1001142762",
"relationshipToStudent": "Mother"
},
{
"studentIdentifier": "453089193",
"studentRegistrationNumber": "753082123",
"parentIdentifier": "1001142760",
"parentFamilyName": "lastname1001142760",
"relationshipIdentifier": "1001159585",
"relationshipToStudent": "Mother"
}
]
Desired JSON output
{
"carer" : {
"relationships" : [ {
"relationshipIdentifier" : "1001142762",
"relationshipToStudent" : "Mother",
"student" : {
"studentIdentifier" : "453089029",
"studentRegistrationNumber" : "753082022"
}
}, {
"relationshipIdentifier" : "1001159585",
"relationshipToStudent" : "Mother",
"student" : {
"studentIdentifier" : "453089193",
"studentRegistrationNumber" : "753082123"
}
} ],
"parentIdentifier" : "1001142760",
"parentFamilyName" : "lastname1001142760"
}
}
JOLT spec so far
[
{
"operation": "shift",
"spec": {
"*": {
"#": "carer.relationships[].student",
"parent*": "carer.&"
}
}
},
{
"operation": "cardinality",
"spec": {
"*": {
"parent*": "ONE"
}
}
},
{
"operation": "remove",
"spec": {
"carer": {
"relationships": {
"*": {
"student": {
"parent*": "",
"relationship*": ""
}
}
}
}
}
}
]
Currently getting this result
{
"carer" : {
"relationships" : [ {
"student" : {
"studentIdentifier" : "453089029",
"studentRegistrationNumber" : "753082022"
}
}, {
"student" : {
"studentIdentifier" : "453089193",
"studentRegistrationNumber" : "753082123"
}
} ],
"parentIdentifier" : "1001142760",
"parentFamilyName" : "lastname1001142760"
}
}
How to map the missing relationship* fields at the right level or is there a better approach?
Spec:
[
{
"operation": "shift",
"spec": {
"*": {
"*": "&",
"student*": "carer.relationships[&1].student.&",
"relationship*": "carer.relationships[&1].&"
}
}
},
{
"operation": "cardinality",
"spec": {
"parent*": "ONE"
}
}
]

Jolt transformation json to json for two siftr operations

I am trying to map one a value of input json to a hashmap of the output json and also want to save the value to some another key using jolt json transformation
input json:
{
"metadata": "/a=value1/b=value2/c=value3"
}
spec:
[{
"operation": "shift",
"spec": {
"metadata": {
// match exactly sets of key value pairs
"/*/*/*": {
// pull each one off and accumulate them into a temp array
"$(0,1)": "temp[]",
"$(0,2)": "temp[]",
"$(0,3)": "temp[]"
}
}
}
},
{
"operation": "shift",
"spec": {
"temp": {
"*": {
// match each item by ":" into two captures
"*=*": {
"$(0,2)": "data.&(1,1)"
}
}
}
}
}
]
output
{
"data" : {
"a" : "value1",
"b" : "value2",
"c" : "value3"
}
}
whereas I also want to map the string metadata to originalData
Expected Output:
{
"data" : {
"a" : "value1",
"b" : "value2",
"c" : "value3"
},
"originalData":"/a=value1/b=value2/c=value3"
}
Spec
[
{
"operation": "shift",
"spec": {
"metadata": {
"#": "originalData",
// match exactly sets of key value pairs
"/*/*/*": {
// pull each one off and accumulate them into a temp array
"$(0,1)": "temp[]",
"$(0,2)": "temp[]",
"$(0,3)": "temp[]"
}
}
}
},
{
"operation": "shift",
"spec": {
"originalData": "originalData", // passthru
"temp": {
"*": {
// match each item by ":" into two captures
"*=*": {
"$(0,2)": "data.&(1,1)"
}
}
}
}
}
]
[
{
"operation": "modify-overwrite-beta",
"spec": {
"metadata": "=split('/',#(1,metadata))"
}
},
{
"operation": "shift",
"spec": {
"metadata": {
"*": {
"*=*": {
"$(0,2)": "data.&(1,1)"
}
}
}
}
}
]

Json array transformation using jolt

i want to transform a JSON array,
JSON:
{
"data1": {
"data2": [
{
"element1": "value1",
"entity": [
"array0",
{
"element2": [
"abc",
"pqr",
"xyz"
]
}
]
}
desired output:
"data2" : {
"element1":"value1",
"entity" : "array0",
"element2":[abc,pqr,xyz]
}
Please find find way to transform this JSON
i have tried with different approach to access the element1 . its not working
Spec
[
{
"operation": "shift",
"spec": {
"data1": {
"data2": {
"*": { // data2 array index
"element1": "data2[&1].element1",
"entity": {
// index input entity array explicilty with "0" and "1"
"0": "data2[&2].entity",
"1": {
"element2": "data2[&3].element2"
}
}
}
}
}
}
}
]