Json array transformation using jolt - 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"
}
}
}
}
}
}
}
]

Related

JoltTransform convert to string

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_

Jolt Transformation to match on multiple key values

I'm struggling to find the correct jolt spec to evaluate the values of two keys prior to creating an output. Any ideas?
Input:
{
"ticketInformation": {
"area": "001",
"site": "ABC",
"ticketType": "TC"
}
}
Spec:
[
{
"operation": "shift",
"spec": {
"ticketInformation": {
"area": {
"001": {
"#Works": "OneMatch"
}
},
"ticketType": {
"TC": {
"#OnlyEvaluatingTicketType": "HowToEvalueValuesOfTwoKeys"
}
}
}
}
}
]
Output:
{
"OneMatch" : "Works",
"HowToEvalueValuesOfTwoKeys" : "OnlyEvaluatingTicketType"
}
The desired output is the same as the actual output but ONLY if area=001 AND troubleType=TC.
I guess this is the spec you are looking for
[
{
"operation": "shift",
"spec": {
"ticketInformation": {
"area": {
"001": {
"#(2,ticketType)": {
"TC": {
"#Yes": "BothMatching"
}
}
}
}
}
}
}
]

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

Jolt convert array keys

I want to convert keys in array json by using jolt
Input
[
{
"TestString": "AGC",
"TestNumber": "3"
},
{
"TestString": "DDD",
"TestNumber": "2"
}
]
Out put:
[
{
"test_string": "AGC",
"test_number": "3"
},
{
"test_string": "DDD",
"test_number": "2"
}
]
What is jolt spec will be?
[{
"operation": "shift",
"spec": {
"*": {
"TestString": "[&1].test_string",
"TestNumber": "[&1].test_number"
}
}
}]

Simplifying Google Sheet JSON using Jolt

I have gone through most of the test cases in Jolt that seem to serve as examples, but cannot find anything that does what I am looking for. For background, the source code is a stripped version of what comes from a google sheet. So rows and columns.
Source JSON:
[
{
"values": [
{
"formattedValue": "ACHME - Thailand"
},
{
"formattedValue": "5,368.11"
},
{
"formattedValue": "17.09%"
}
]
},
{
"values": [
{
"formattedValue": "ACHME-B2A"
},
{
"formattedValue": "101.47"
},
{
"formattedValue": "0.32%"
}
]
},
{
"values": [
{
"formattedValue": "ACHME-B2E"
},
{
"formattedValue": "83.79"
},
{
"formattedValue": "0.27%"
}
]
}
]
Desired Output:
[
{
"row": [
"ACHME - Thailand",
"5,368.11",
"17.09%"
]
},
{
"row": [
"ACHME-B2A",
"101.47",
"0.32%"
]
},
{
"row": [
"ACHME-B2E",
"83.79"
"0.27%"
]
}
]
I have tried this Jolt that gets me most of the way there, but I am still losing the row boundaries. It all comes out with one 'row' element and all of the values in an array.
[
{
"operation": "shift",
"spec": {
"*": {
"values": {
"*": {
"formattedValue": "row[]"
}
}
}
}
}
]
Spec
[
{
"operation": "shift",
"spec": {
"*": { // row index
"values": {
"*": { // column index
"formattedValue": "[&3].row[&1]"
}
}
}
}
}
]
Note, this assumes that all the incoming "values" arrays are the same length.