could anyone help to convert below mention input json to desired output json using jolt ?
Here in treefield list i want to fetch "paramid" and "paramvalue" to first level for only matched "paramid", rest of the list items should be intact in that treefield list.
e.g i want to take paramid "k1" with its value to first level as mentioned in the output.
Input
{
"A": "value1",
"B": "value2",
"C": {
"D": "x1",
"E": {
"treefield": [
{
"paramid": "k1",
"paramvalue": {
"string": "value1"
}
},
{
"paramid": "k2",
"paramvalue": {
"string": "value2"
}
},
{
"paramid": "k3",
"paramvalue": {
"string": "value3"
}
}
]
},
"F": {
"a": "x1",
"x": {
"y": 1
}
},
"H": "x4"
}
}
]```
**Output**
```[
{
"A": "value1",
"B": "value2",
"C": {
"D": "x1",
"E": {
"treefield": [
{
"paramid": "k1",
"paramvalue": {
"string": "value1"
}
},
{
"paramid": "k3",
"paramvalue": {
"string": "value3"
}
}
]
},
"F": {
"a": "x1",
"x": {
"y": 1
}
},
"H": "x4"
},
"k2": "value2"
}
]```
If I understand what you're trying to do (pull out key/value pairs and put them at the top level), this spec should do it:
[
{
"operation": "shift",
"spec": {
"C": {
"E": {
"treefield": {
"*": {
"paramvalue": {
"string": "#(2,paramid)"
}
}
}
},
"*": "&"
},
"*": "&"
}
}
]
That works on individual JSON objects. In your sample input you don't have a leading array bracket but you do have a trailing one, so if you expect multiple objects in an array and want them output in an array, this spec should work:
[
{
"operation": "shift",
"spec": {
"*": {
"C": {
"E": {
"treefield": {
"*": {
"paramvalue": {
"string": "[&6].#(2,paramid)"
}
}
}
},
"*": "[&2].&"
},
"*": "[&1].&"
}
}
}
]
Related
I am trying to achieve the following transformation. However, my solution adds undesired null values into the final array.
The transformation needs to shift names in child array for all root elements. I have created 3 cases to illustrate the problem.
Case 1
Input
{
"root": [
{
"child": [
{
"name": "John"
},
{
"name": "Frazer"
}
]
},
{
"child": [
{
"name": "Brandon"
},
{
"name": "Josef"
}
]
}
]
}
Desired Output
{
"NAMES": ["John,Frazer","Brandon,Josef"]
}
Case 2: One child is empty
Input
{
"root": [
{
"child": []
},
{
"child": [
{
"name": "Brandon"
},
{
"name": "Josef"
}
]
}
]
}
Desired Output
{
"NAMES": ["","Brandon,Josef"]
}
Case 3: All childs are empty
Input
{
"root": [
{
"child": []
},
{
"child": []
}
]
}
Desired Output
{
"NAMES": ["",""]
}
EDIT: root array will always have at least 1 element.
Current JOLT spec works fine except for cases where child is an empty array. It generates null values and I'm trying to specify an empty string instead (or any hardcoded string value such as "NO_NAMES")
[
{
"operation": "shift",
"spec": {
"root": {
"*": {
"child": {
"*": {
"name": "NAMES[&3]"
}
}
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"NAMES": {
"*": "=trim"
}
}
},
{
"operation": "cardinality",
"spec": {
"NAMES": "MANY"
}
},
{
"operation": "default",
"spec": {
"NAMES": []
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"NAMES": {
"*": "=join(',',#0)"
}
}
}
]
You can apply consecutive transformations modify-overwrite-beta and then shift in order to determine comma-seperated elements of the list(unless they have zero size,this case only double quotes will appear), and then concatenate them within a list such as
[
{
"operation": "modify-overwrite-beta",
"spec": {
"root": {
"*": {
"child": { "*": "#(0,name)" },
"NAMES": "=join(',',#(1,child))"
}
}
}
},
{
"operation": "shift",
"spec": {
"root": {
"*": {
"NAMES": "&"
}
}
}
}
]
I have the following input json, which has an array of json:
{
"ArrayList": [
{
"a": "value1",
"b": "value2",
"c": "value3"
},
{
"a": "value4",
"b": "value5",
"c": "value6"
},
{
"a": "value7",
"b": "value8",
"c": "value9"
}
]
}
And the desired output is :
{
"ArrayList": [
{
"a": "value1",
"b": "value2"
},
{
"a": "value4",
"b": "value5"
},
{
"a": "value7",
"b": "value8"
}
]
}
What will be the Jolt transform spec expression for this?
Seems you want to remove the attribute c of objects of the ArrayList array, then a remove transformation might be used such as
[
{
"operation": "remove",
"spec": {
"ArrayList": {
"*": {
"c": ""
}
}
}
}
]
I am trying to give as input a JSON that contains a fixed variable (in my example group) that has as value an imbrication of objects and my goal is to transform it to sub objects.
My Input :
[
{
"key": "name",
"value": "marc",
"group": "office.people"
}
]
My Spec :
[
{
"operation": "shift",
"spec": {
"*": {
"value": "#(1,key).#(1,group)"
}
}
}
]
Expected:
{
"name": {
"office": {
"people": "marc"
}
}
}
Actual:
{
"name" : {
"office.people" : "marc"
}
}
You could split the group first:
[
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"group": "=split('\\.',#(1,group))"
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"value": "#(1,key).#(1,group[0]).#(1,group[1])"
}
}
}
]
In JOLT Based on the jsonarray values the output key and value should be added.
In the output CO2_VAL,CO_VAL,O3_VAL shoould come based on param value in env_values. So, how to apply the above filter.
The input payload is:
{
"id":"abcd",
"env_values":[
{
"param":"CO2",
"values":"20.0"
},
{
"param":"CO",
"values":"21.0"
},
{
"param":"O3",
"values":"22.0"
}
]
}
The output is :
{
"sl":"abcd",
"CO2_VAL":"20.0",
"CO_VAL":"21.0",
"O3_VAL":"22.0"
}
Hope it is what you ment. First of all, we are adding '_VAL' to the key. In the second spec we are putting keys to the values. And at the end we are pairing each key with value.
[
{
"operation": "shift",
"spec": {
"id": "s1",
"env_values": {
"*": {
"param": {
"*": "param[&2].t.&_VAL"
},
"values": "param[&1].values"
}
}
}
},
{
"operation": "shift",
"spec": {
"s1": "s1",
"param": {
"*": {
"t": {
"*": {
"$": "param[&3].key"
}
},
"values": "param[&1].value"
}
}
}
},
{
"operation": "shift",
"spec": {
"s1": "s1",
"param": {
"*": {
"value": "#(1,key)"
}
}
}
}
]
I am trying to perform conditional mapping of values from the following JSON.
My input,
{
"rating": [
{
"id": 1,
"locations": [
{
"num": 1
},
{
"num": 2
}
]
}
]
}
Expected output:
{
"rating": [
{
"id": 1,
"locations": [
{
"num": 1
}
],
"new_locations": [
{
"num": 2
}
]
}
]
}
My spec,
[
{
"operation": "shift",
"spec": {
"rating": {
"*": {
"locations": {
"*": {
"num": "#(3,id)"
}
}
}
}
}
}
]
If the num value matches with id,then it should stay in location array else should be moved to new_locations.
Can anyone please suggest me help.Thanks.
There isn't a way to do that kind of conditional matching logic with the "out of the box" Jolt transforms.