How will the JOLT spec look to achieve desired result with the following input? - jolt

I am having troubles transforming JSON payload into the desired document.
I have following input:
{
"events": [
{
"recipientId": "r0001"
},
{
"recipientId": "r0002"
}
],
"networkResponseTime": 1234
}
Desired output:
{
"events": [
{
"recipientIds": "r0001",
"networkResponseTime": 1234"
},
{
"recipientIds": "r0002",
"networkResponseTime": 1234"
}
]
}
How will the JOLT spec look like for this example?
So far I have smth like this:
[{
"operation": "shift",
"spec": {
"events": {
"*": {
"recipientId": "events[&1].recipientIds"
}
}
}
}]

Spec
[{
"operation": "shift",
"spec": {
"events": {
"*": {
"recipientId": "events[&1].recipientIds",
//
// go back up to the root of the tree, and then
// come back down the key "networkResponseTime", and
// send it's value to "events[&1].networkResponseTime"
"#(2,networkResponseTime)": "events[&1].networkResponseTime"
}
}
}
}]

Related

Jolt spec file multiple array of element

I am trying to write a jolt transformation for the input below:
Input
[{
"factValues": [
{
uniqeid:"1",
"values":"1234567"
},
{
uniqeid:"1",
"teams":"abcde"
}]
},{
"factValues": [
{
uniqeid:"2",
"values":"6758595"
},
{
uniqeid:"2",
"teams":"medrgeh"
}]
}
]
Excepted Output.
{
"factValues": [{
uniqeid:"1",
"values":'1234567',
"teams":'76599876'
},
{
uniqeid:"2",
"values":'6758595',
"teams":'medrgeh'
}
]
}
Kindly help me to achieve the expected output.json in spec.json. The spec is not transforming as expected output. I want learn how to use attributes inside the string parser
Shifted all the values to the factValues. Then selected the first element from the uniqueid array,
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": {
"uniqeid": "factValues.[&3].uniqeid",
"values": "factValues.[&3].values",
"teams": "factValues.[&3].teams"
}
}
}
}
}, {
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"*": {
"uniqeid": "=firstElement(#(1,uniqeid))"
}
}
}
}
]

JsonTransformation using Jolt

I'm using jolt + java(https://github.com/bazaarvoice/jolt) to transform an external JSON in a format that I can understand.
My problem is the structure keeps changing and this is making my spec more and more complex.
I want to extract all the fields which are called "path" no matter the structure.
does someone have an idea how can I do that?
Example of structure:
{
"groups": {
"rows": {
"fieldSets": {
"fields": [{
"path": "example"
}]
}
}
}
}
or
{
"groups": {
"rows": {
"rowsets": {
"fieldSets": {
"fields": [{
"path": "example"
}]
}
}
}
}
}
or
{
"groups": {
"fieldSets": {
"fields": [{
"path": "example"
}]
}
}
}
in the end, I just want an array with plain "path" values.
I am also new to the JOLT. Anyway I tried with what I understood from your question. Just try the below spec:
[
{
"operation": "shift",
"spec": {
"groups": {
"rows": {
"fieldSets": {
"fields": {
"*": {
"path": "path"
}
}
}
}
}
}
}
]
If it is not what you are expecting, then please give an output json, so that i can understood what you want.

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.