Jolt Transformation - json

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

Related

JSON object to array with JOLT

I have the following JSON object
Input:
{
"17.39.108.85:80": [],
"10.204.32.9:443": [
{
"status": "DOWN"
}
]
}
and trying to transform it into a list/array as below :
Desired Output:
[
{
"17.39.108.85:80": []
},
{
"10.204.32.9:443": [
{
"status": "DOWN"
}
]
}
]
What's the best way to use Jolt.
You can use this spec:
Currently, your input is correct, but you need to put each key into an array.
You can get each key in the object and send it to an array with [#2].
Note: #2 is the index of each key. for example: 17.39.108.85:80 is 0 and 10.204.32.9:443 is 1.
[
{
"operation": "shift",
"spec": {
"*": {
"#": "[#2].&"
}
}
}
]

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

How can I combine two arrays to create a key value pair with Jolt?

I've already created a spec to convert my JSON input
{
"rows": [
{
"row": [
"row1",
"row2",
"row3"
],
"header": [
"header1",
"header2",
"header3"
]
},
{
"row": [
"row4",
"row5",
"row6"
],
"header": [
"header4",
"header5",
"header6"
]
}
]
}
to convert to key-value pairs as following object result :
{
"header1" : "row1",
"header2" : "row2",
"header3" : "row3",
"header4" : "row4",
"header5" : "row5",
"header6" : "row6"
}
Is this possible to do using Jolt?
Is there a copy/paste error in your input? Judging by your desired output, the second object's header array should be ["header4", "header5", "header6"]. If that's the case, this spec should work:
[
{
"operation": "shift",
"spec": {
"rows": {
"*": {
"header": {
"*": {
"*": {
"#(3,row[#2])": "&"
}
}
}
}
}
}
}
]
One option is to use the following shift transformation spec :
[
{
"operation": "shift",
"spec": {
"*s": { // rows array
"*": {
"&(1,1)": { // row array
"*": {
"#": "#(3,header[&1])"
}
}
}
}
}
}
]
where
"*s": { stands for rows
"&(1,1)": { -> not immediate(zeroth) level but one more level up by using &(1, and grab the value there the first asterisk exists by &(..,1)
"#": "#(3,header[&1])" -> 3 levels needed as stated at the right hand side traverse the colon
as well in order to reach the level of &(1,1) which is used to
represent the "row" array along with &1 representation to go one level up the tree to reach the indexes of the array "row" while matching with the values of "row" through use of # on the left hand side
the demo on the site http://jolt-demo.appspot.com/ is :

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

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

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