Add Array Index using JOLT - jolt

I want to add a kind of line number or identifier with jolt for each array element.
Given Array:
[
{
"key1": "value1",
"key2": "value2",
"neyN": "valueN"
},
{
"key1": "value1",
"key2": "value2",
"neyN": "valueN"
}
]
Expected Result:
[
{
"key1": "value1",
"key2": "value2",
"neyN": "valueN",
"id": 0
},
{
"key1": "value1",
"key2": "value2",
"neyN": "valueN",
"id": 1
}
]
I tried now default, shift and more, but was not able to find a right solution.
Can someone help me?
Thanks in advance
Marcus

Spec 1 : Group the keys along with id field using index number.
Spec 2 : Remove the index number key from the result array.
[
{
"operation": "shift",
"spec": {
"*": {
"#": "&1",
"$": "&1.id"
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"#": "[]"
}
}
}
]

Related

Extract certain fields in a Json array based on a filter using JOLT

Please help. I am trying to extract certain fields from the below input based on some condition ( list of name and match whose key value is "key2"). But not getting the proper result.
json Input:
{"schemas": [
{
"name": "myschema",
"tables": [
{
"name": "myname",
"alias": "temp_alias",
"keys": [
{
"name": "value1",
"key": "key1",
"match": "match_val"
},
{
"name": "value21",
"key": "key2",
"match": "match_val2"
},
{
"name": "value22",
"key": "key2",
"match": "match_val2"
},
{
"name": "value3",
"key": "key3"
},
{
"name": "value4",
"key": "key4"
}
]
}
]
}
]
}
Expected Output: list of name and match whose key value is "key2".
{"key2": [
{
"name": "value21",
"match": "match_val2"
},
{
"name": "value22",
"match": "match_val2"
}
]
}
I have used the below spec but it is giving all the keys in output.
[{
"operation": "shift",
"spec": {
"schemas": {
"*": {
"tables": {
"*": {
"keys": {
"*": {
"key": {
"key2": {
"#2": "&"
}
}
}
}
}
}
}
}
}
}
]
Check this spec,
[
{
"operation": "shift",
"spec": {
"schemas": {
"*": {
"tables": {
"*": {
"keys": {
"*": {
"key": {
"key2": {
"#2": "#2[]"
}
}
}
}
}
}
}
}
}
}
]

JOLT Transformation Merge Array of Objects

I am trying to create a jolt transformation for the below input;
{
"group1": [
{
"schema": "schemaA"
},
{
"key1": "val1",
"key2": "val2"
}
],
"group2": [
{
"schema": "schemaA"
},
{
"key1": "val1",
"key2": "val2"
}
]}
With the desired output of;
{
"group1": {
"schema": "schemaA",
"key1": "val1",
"key2": "val2"
},
"group2": {
"schema": "schemaA",
"key1": "val1",
"key2": "val2"
}}
The key 'schema' will always be present but I won't know what the key1,key2,etc values are. So I can't explicitly map them. Any help would be much appreciated!
Spec,
[
{
"operation": "shift",
"spec": {
"group*": {
"*": {
"key*": "&2.&",
"schema": "&2.&"
}
}
}
}
]

Add array inside object with same key in jolt spec

I would like to move the recommendations array by row_id key inside the investors with the same row_id
Original Json
{
"investors": [
{
"row_id": 1,
"name": "AAAA"
},
{
"row_id": 2,
"name": "BBBB"
}
],
"recommendations": [
{
"row_id": "1",
"title": "ABC"
},
{
"row_id": "2",
"title": "CDE"
}
]
}
I've tried a lot of specs at https://jolt-demo.appspot.com with no success
Specs tried...
[{
"operation": "shift",
"spec": {
"investors": {
"*": "investors[]"
},
"recommendations": {
"#": "recommendations[]"
}
}
}]
Desired Json
{
"investors": [
{
"row_id": 1,
"name": "AAAA",
"recommendations":[{
"row_id": "1",
"title": "ABC"
}]
},
{
"row_id": 2,
"name": "BBBB",
"recommendations":[{
"row_id": "2",
"title": "CDE"
}]
}
]
}
This can be done in two stage shift
First shift groups everything based on row_id.
(I'd suggest running the first shift of its own to see what the output is)
Second shift uses that grouped output and formats results.
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"row_id": {
"*": {
"#2": "&.&4"
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"investors": "investors.[#2]",
"recommendations": "investors.[#2].recommendations[]"
}
}
}
]

How to change a name of one field in large Json using Jolt

I have a big Json document:
{ "field1": "value1",
"field2": "value2",
"field3": "value3",
...
"field1000": "value1000"
}
I want to change the name of one field (field3) to third_field
How to do it without writing specification like that:
[
{
"operation": "shift",
"spec": {
"field1": "field1",
"field2": "field2",
"field3": "third_field",
...
"field1000": "field1000"
}
}
]
This should work and essentially does an if then else
[
{
"operation": "shift",
"spec": {
//if
"field3": {
//$ - current value
"$": "third_field"
},
//else
"*": {
//$ - current value
//& - current key
"$": "&"
}
}
}
]

Jolt JSON Conversion from String value to Long

I am using Jolt to convert one json to another json. Everything is working fine except I want to convert String value to Long. Below is my Specs and input. I have use modify-overwrite-beta but no luck.
Specs -
[
{
"operation": "modify-overwrite-beta",
"spec": {
"timestamp": "=toLong(#(1,time))"
}
},
{
"operation": "shift",
"spec": {
"key1": "outputText1",
"key2": "outputText2",
"key3": "outputText3",
"time": "timestamp"
}
}
]
Input Json
{
"key1": "test1",
"time": "1499967627",
"key2": "test2",
"key3": "test3",
}
So in above input json how I can convert time value to Long
Expected Json :
{
"outputText1": "test1",
"timestamp": 1499967627,
"outputText2": "test2",
"outputText3": "test3",
}
Spec
[
{
"operation": "modify-overwrite-beta",
"spec": {
"timestamp": "=toLong(#(1,time))"
}
},
{
"operation": "shift",
"spec": {
"key1": "outputText1",
"key2": "outputText2",
"key3": "outputText3",
// pass timestamp thru
"timestamp": "timestamp"
}
}
]
In the first operation (modify) it was making "timestamp" be a long. But then in the 2nd operation, you were copying the String value from "time" to timestamp, instead of passing timestamp thru.