create a new array using JOLT - json

I am trying to modify the JSON output using JOLT, I have been using it mostly to rename/remove fields until now.
I want to create a new object of existing 2 arrays as elements.
input.json
[
{
"features": [
{
"featureId": 1,
"feature": "feature"
}
],
"product": [
{
"id": 1,
"name": "name"
}
]
}
]
current spec json:
[{
"operation": "shift",
"spec": {
"*": {
"features": {
"*": {
"featureId": "[&3].&2.[&1].featureId",
"feature": "[&3].&2.[&1].feature"
}
},
"product": {
"*": {
"id": "[&3].&2.[&1].id",
"name": "[&3].&2.[&1].name"
}
}
}
}
}
]
expected output:
[
{
"newarray": {
"features": [
{
"featureId": 1,
"feature": "feature"
}
],
"product": [
{
"id": 1,
"name": "name"
}
]
}
}
]
Basically, I want to move both of my existing arrays inside a new object. Thanks in advance!

Spec
[
{
"operation": "shift",
"spec": {
"*": { // outer array index
// match features and product which are lists
// and copy those lists to the output
"*": "[0].newarray.&"
}
}
}
]

Related

Modify a JSON using Jolt Transform

I have an array of JSONs as listed below:
[
{
"id": 1
},
{
"id": 2
},
{
"id": 3
}
]
The following are the objectives:
(1) Modify the list above into:
{
"data": [
{
"id": 1
},
{
"id": 2
},
{
"id": 3
}
],
"idList": [ 1, 2, 3 ]
}
(2) Calculate the Minimum and Maximum of "idList" to finally obtain:
{
"data": [
{
"id": 1
},
{
"id": 2
},
{
"id": 3
}
],
"minID": 1,
"maxID": 3,
}
I think (2) is straightforward after getting (1), as I can simply use:
min(#(1,idList))
I have a problem in converting the original input into (1), here's my attempt:
[
{
"operation": "shift",
"spec": {
"*": "data"
}
},
{
"operation": "shift",
"spec": {
"data": { "*": { "id": "idList" } }
}
}
]
which yields:
{
"idList" : [ 1, 2, 5 ]
}
Can anyone help on this?
Also, am a newbie to this Jolt Transform technique, can anyone suggest a good source for mastering this ? (like a book)
You can consecutively use a shift, and modify transformation specs such as
[
{
// for the first objective
"operation": "shift",
"spec": {
"*": {
"#": "data",
"*": "idList"
}
}
},
{
// for the second one
"operation": "modify-overwrite-beta",
"spec": {
"minID": "=min(#(1,idList))",
"maxID": "=max(#(1,idList))"
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is

conversion array into array by jolt

I have following json data . I want to transform following data by jolt nifi processor into result data
{
"data": [
{
"source": "Environment Sensors",
"alert_count": "2",
"category": "envs",
"alert_array": {
"alerts": [
{
"name": "neeraj",
"id": "123"
},
{
"name": "arun",
"id": "897"
}
]
}
}
]
}
result data
{
"alert": "2",
"subcategory": "envs",
"alert_array": [
{
"category": "Environment Sensors",
"newName": "neeraj",
"newID": "123"
},
{
"category": "Environment Sensors",
"newName": "arun",
"newID": "897"
}
]
}
Here value of category is the value of source
You can use the following shift transformation spec
[
{
"operation": "shift",
"spec": {
"data": {
"*": {
"alert_count": "alert",
"category": "sub&",
"alert_array": {
"*": {
"*": {
"#(3,category)": "&3[&1].category",
"name": "&3[&1].newName",
"id": "&3[&1].newID"
}
}
}
}
}
}
}
]
first reach the elements of the innermost object, and determine the value of the category element by going 3 levels up along with the others which are alreay located there.

Jolt transform array to dissipate an outer attribute to each sub-object of an array

I want to apply a Jolt Transformation, but this is still cloudy on my side:
This is my input data:
{
"results": 1,
"data": [
{
"detail": [
{
"num": "140"
},
{
"num": "158"
},
{
"num": "180"
},
{
"num": "183"
},
{
"num": "213"
}
],
"code": "01007"
}
],
"response_code": 200
}
My desired output:
[
{
"code": "01007",
"num": "140"
},
{
"code": "01007",
"num": "158"
},
....
{
"code": "01007",
"num": "213"
}
]
And my trial for the JOLT specification so far, I do not understand how to add a custom field to all elements of the list:
[
{
"operation": "shift",
"spec": {
"data": {
"*": {
"detail": {
"*": {
"code": "[&1].code",
"#": "[&1]"
}
}
}
}
}
}
]
You can use the shift transformation like this
[
{
"operation": "shift",
"spec": {
"data": {
"*": {
"detail": {
"*": {
"#(2,code)": "[&1].code", // to go 2 levels up to reach the level of the attribute "code"
"*": "[&1].&" // to get the values of the existing attributes within the current object without object wrapper
}
}
}
}
}
}
]
applying just some slight changes.
the demo on the site http://jolt-demo.appspot.com/ is

Create array of simpler JSON objects from a nested JSON using jolt

My input JSON is like
{
"common": {
"name": "abc"
},
"details": {
"id": 4,
"node": [
{
"name": "node1",
"array2": []
},
{
"name": "node2",
"array2": [
{
"name": "node2_a2_1"
}
]
},
{
"name": "node3",
"array2": [
{
"name": "node3_a2_1"
},
{
"name": "node3_a2_2"
},
{
"name": "node3_a2_3"
}
]
}
]
}
}
What I want is for each leaf node in array2 (e.g. {"name": "node3_a2_1"}) I will traverse towards the root and add all common items and create an array of JSON objects without any nested object. So, the output I want is like
[
{
"common_name": "abc",
"id": 4,
"node_name": "node2",
"name": "node2_a2_1"
},
{
"common_name": "abc",
"id": 4,
"node_name": "node3",
"name": "node3_a2_1"
},
{
"common_name": "abc",
"id": 4,
"node_name": "node3",
"name": "node3_a2_2"
},
{
"common_name": "abc",
"id": 4,
"node_name": "node3",
"name": "node3_a2_3"
}
]
Could you please suggest how can I do that?
You can walk through the array2 while picking values of each element from their original location in the JSON value.
For example; traverse } four times to grab the value of id by using #(4,id), and use #(3,name)[&1] as the common distinguishing identifier for each attribute such as
[
{
"operation": "shift",
"spec": {
"details": {
"node": {
"*": {
"array2": {
"*": {
"#(5,common.name)": "#(3,name)[&1].common_name",
"#(4,id)": "#(3,name)[&1].id",
"#(2,name)": "#(3,name)[&1].node_name",
"name": "#(3,name)[&1].&"
}
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": ""
}
}
}
]

JOLT transformation for JSON originally from Protobuf format

I have json converted from protobuf protocol with following format:
{
"id": "6aa0734f-6d6a-4b95-8a2b-2dde346f9df7",
"measurements": [
{
"ts": "1590331111510000000",
"values": [
{},
{
"name": 1,
"value": -1.8093990087509155
},
{
"name": 2,
"value": 0.35456427931785583
}
],
"parameters": [
"Stat",
"VoltageAnglePhaseB"
]
}
]
}
expected output is:
{
"id" : "6aa0734f-6d6a-4b95-8a2b-2dde346f9df7",
"ts" : "1590331111510000000",
"Stat":-1.8093990087509155,
"VoltageAnglePhaseB":0.35456427931785583
}
I've started my Jolt spec like this:
[
{
"operation": "shift",
"spec": {
"id": "id",
"measurements": {
"*": {
"ts": "ts",
"parameters": {
"*": ""
},
"*": ""
}
}
}
}
]
But facing the problem to extract name and value from the JSON.
Does anyone have an idea?
It can be done with 2 shift operations. First, shift the values to the valuesArr, same way shift the parameters to the parametersArr. Then match the values with the index.
[
{
"operation": "shift",
"spec": {
"id": "id",
"measurements": {
"*": {
"ts": "ts",
"values": {
"*": {
"value": "valuesArr"
}
},
"parameters": "parametersArr"
}
}
}
},
{
"operation": "shift",
"spec": {
"id": "id",
"ts": "ts",
"valuesArr": {
"*": "#(2,parametersArr[&])"
}
}
}
]