How tracnform rest of json into one field value using jolt? - json

Here is the JSON input:
{
"myRootKey": {
"directMove": "directValue",
"marker": "THE_MARKER",
"someTextField": "someString",
"someObject": {
"someKey": "value"
}
}
}
the output should be:
{
"myRootKey": {
"subKey": {
"directMove": "directValue"
},
"THE_MARKER": {
"someTextField": "someString",
"someObject": {
"someKey": "value"
}
}
}
}
With direct moving it is clear, but how rest of the input to the marker object value?

You match down to "someTextField" and "someObject", but use the new "#" / look up the tree logic to find the "marker" to use as an ouput path.
Spec
[
{
"operation": "shift",
"spec": {
"myRootKey": {
"directMove": "myRootKey.subKey.directValue",
"someTextField": "#(1,marker).someTextField",
"someObject": "#(1,marker).someObject"
}
}
}
]

#(1,marker) allows you to retrieve the value of the marker field
&1 retrieves the value of the matching node
So the spec you are looking for looks like :
[
{
"operation": "shift",
"spec": {
"myRootKey": {
"directMove": "myRootKey.subKey.directValue",
"someTextField": "&1.#(1,marker).someTextField",
"someObject": "&1.#(1,marker).someObject"
}
}
}
]

You can use this spec fully dynamically:
[
{
"operation": "shift",
"spec": {
"*": {
"directMove": "&1.subKey.&",
"*TextField": "&1.#(1,marker).&",
"*Object": "&1.#(1,marker).&"
}
}
}
]

Related

Jolt transform specification input

i have the following input json:
{
"tags": {
"event": "observation",
"source": "hunter"
}
}
The output JSON should look like below:
{
"tags" : [ "event:observation", "source:hunter" ]
}
can anyone provide any guidance on how to build a proper jolt specification for the above?
thank you very much for the help ^_^
You can use this specification
[
{ // combine each key-value pair under within common arrays
"operation": "shift",
"spec": {
"tags": {
"*": {
"$": "&2_&1",
"#": "&2_&1"
}
}
}
},
{ // concatenate key-value pairs by colon characters
"operation": "modify-overwrite-beta",
"spec": {
"*": "=join(':',#(1,&))"
}
},
{
"operation": "shift",
"spec": { // make array key common("tags") for all arrays
// through use of _ seperator and * wildcard
"*_*": "&(0,1)"
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is

JOLT shift transformation to filter values in array

I want to use a JOLT transformation to do two things:
filter the elements in the array called myarray so that only elements remain which have a "v_518" attribute
filter out all attributes of the remaining elements except for "v_518" and "lfdn"
Input:
{
"isError": false,
"isValid": true,
"myarray": [
{
"p_0001": "1",
"p_0002": "1",
"p_0003": "1",
"p_0004": "1",
"v_518": "0,214506186",
"lfdn": 89709
},
{
"p_0001": "2",
"p_0002": "1",
"p_0003": "1",
"v_518": "0,3823236",
"lfdn": 89710
},
{
"p_0001": "3",
"p_0002": "1",
"p_0003": "1",
"lfdn": 89711
}
],
"errorMessage": null,
"exceptionMessage": null,
"innerExceptionMessage": null
}
Desired output:
{
"isError": false,
"isValid": true,
"myarray": [
{
"v_518": "0,214506186",
"lfdn": 89709
},
{
"v_518": "0,3823236",
"lfdn": 89710
}
],
"errorMessage": null,
"exceptionMessage": null,
"innerExceptionMessage": null
}
What I tried so far, but doesn't work as intended:
[
{
"operation": "shift",
"spec": {
"isError": "isError",
"isValid": "isValid",
"myarray": {
// loop thru all the elements in value array
"*": {
"v_518": {
// if the value "v_518" exists
// grab the whole object and write it out to
// a v_518_array array.
"#(1,v_518)": "v_518_array",
"#(1,lfdn)": "v_518_array"
}
}
},
"errorMessage": "errorMessage",
"exceptionMessage": "exceptionMessage",
"innerExceptionMessage": "innerExceptionMessage"
}
}
]
I tried working with the examples in http://jolt-demo.appspot.com/#andrewkcarter2 but I couldn't figure out how to do it.
I was able to solve my issue. This answer was the hint I needed to get the ball rolling: https://stackoverflow.com/a/38154541/1561441
The key is referencing the array you are currently transforming via "value" = "array[&1].value".
In my mind I spent way too much time on this issue. Does anyone know of a good documentation for the Jolt syntax? I couldn't find a satisfactory one by googling myself.
[
{
"operation": "shift",
"spec": {
"isError": "isError",
"isValid": "isValid",
"myarray": {
// loop thru all the elements in value array
"*": {
"v_518": {
// if the value "v_518" exists
// grab the whole object and write it out to
// a v_518_array array.
"#1": "v_518_array"
}
}
},
"errorMessage": "errorMessage",
"exceptionMessage": "exceptionMessage",
"innerExceptionMessage": "innerExceptionMessage"
}
},
{
"operation": "shift",
//Transform array: https://stackoverflow.com/questions/37865871/how-do-i-transform-an-array-using-jolt
"spec": {
"v_518_array": {
// loop thru all the elements in value array
"*": {
"v_518": "v_518_array[&1].v_518",
"lfdn": "v_518_array[&1].lfdn"
}
}
}
}
]
Here's a slightly better solution:
[
{
"operation": "shift",
"spec": {
"isError": "isError",
"isValid": "isValid",
"myarray": {
// loop thru all the elements in value array
"*": {
"v_518": {
// if the value "v_518" exists
// grab the whole object and write it out to
// a v_518_array array.
"#1": "v_518_array"
}
}
},
"errorMessage": "errorMessage",
"exceptionMessage": "exceptionMessage",
"innerExceptionMessage": "innerExceptionMessage"
}
},
{
"operation": "shift",
//Transform array: https://stackoverflow.com/questions/37865871/how-do-i-transform-an-array-using-jolt
"spec": {
"v_518_array": {
// loop thru all the elements in value array
"*": {
"v_518": "&2[&1].v_518", //notice the generic shorthand here
"lfdn": "&2[&1].lfdn"
}
}
}
}
]
Still there's a briefer, and more dynamic method to figure out your issue such as
[
{
"operation": "shift",
"spec": {
"myarray": {
"*": {
"v_518": {
"#(1,&)": "&3.[&2].&",
"#(1,lfdn)": "&3.[&2].lfdn"
}
}
},
"*": "&"
}
}
]

Converting List to Comma Separated String in JOLT

I have below scenario, where two operations need to be performed. One is parsing the list and create a comma separated string. Then, transform that into the output format json
Input -
{
"list": ["ABC","XYZ"]
}
Output -
{
"additionalAttributes" : {
"userContext" : [ {
"auths" : "ABC,XYZ"
} ]
}
}
Check this spec
[
{
"operation": "modify-overwrite-beta",
"spec": {
"list": "=join(',',#(1,list))"
}
}, {
"operation": "shift",
"spec": {
"list": "additionalAttributes.userContext[].auths"
}
}
]

Jolt transform so that data of an element is the key and the value is another elements data

I need to perform a Jolt transformation on the below example json:
[ {
"name" : "foo",
"dataSample" : "red"
}, {
"name" : "bar",
"dataSample" : "amber"
}]
I need the output to look like:
{
"foo": "red",
"bar": "amber"
}
so far i've managed to extract the name value as the key, but i'm lost as to how to get the dataSample value as the value for the transformed element. Here's the Jolt script I have so far:
[
{
"operation" : "shift",
"spec" : {
"*" : {
"name" : {
"*" : "&"
}
}
}
}
]
You need to go back up the tree to get the value of the "name" field, rather than using the current value (&). This should work:
[
{
"operation": "shift",
"spec": {
"*": {
"name": {
"#(1,dataSample)": "#(2,name)"
}
}
}
}
]
[
{
"operation": "shift",
"spec": {
"*": {
"dataSample": "#(1,name)"
}
}
}
]

Jolt Transformation

I am trying to write a jolt transformation for below input -
{
"restaurantId": "ZZ4ORJDY3E",
"chainId": "a-b"
}
expected output is -
{
"ZZ4ORJDY3E" : {
"key" : "ZZ4ORJDY3E",
"start" : "a",
"end" : "b"
}
}
My spec is -
[
{
"operation": "shift",
"spec": {
"#restaurantId": "#restaurantId.key",
"chainId": {
"*-*": {
"$(0,1)": "#restaurantId.start",
"$(0,2)": "#restaurantId.end"
}
}
}
}
]
The spec is not transforming as expected output. i want learn how to use attributes inside string parser.
Spec
[
{
"operation": "shift",
"spec": {
"restaurantId": {
// match any value of restaurantId
"*": {
// write the value to of the key $ to the output
// where the output is the "value of the key".key
// kinda hokey
"$": "&.key"
}
},
"chainId": {
"*-*": {
// write each part of the chainId to the output
// at the value of restaurantId from back up the tree
"$(0,1)": "#(3,restaurantId).start",
"$(0,2)": "#(3,restaurantId).end"
}
}
}
}
]