I have following JSON data . I want to transform following data by Jolt nifi processor into result data
{
"alert_array": {
"alerts": [
{
"alertId": "alt001",
"severity": "High",
"alert_type": "Alert"
},
{
"alertId": "alt002",
"severity": "High",
"alert_type": "Alert"
}
]
}
}
result data
{
"alert_array": [
{
"Id": "alt001",
"speed": "High",
"type": "Alert"
},
{
"Id": "alt002",
"speed": "High",
"type": "Alert"
}
]
}
please help to write spac of jolt transformation
If you wouldn't rename the attributes, then using
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": "&2[&]" // &2 represents going two levels up to get the literal "alert_array"
}
}
}
}
]
would suffice.
the demo on the site http://jolt-demo.appspot.com/ is
But in your case, you need to qualify each one individually such as
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": {
"alert*": "&3[&1].&(0,1)",
"severity": "&3[&1].speed",
"alert_*": "&3[&1].&(0,1)"
}
}
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is
Related
I need to merge array data from two different arrays from the Input JSON and make a flat list of items in the Output JSON. The first array contains the key required for the output and the second array contains the value.
So far, all my attempts at a spec haven't even come close, which is why I haven't listed one. Please see the Input and Desired Output below. Thanks for any help!!
Input JSON :
{
"data": [
{
"2": {
"value": "DC1"
},
"3": {
"value": 5
}
},
{
"2": {
"value": "DC2"
},
"3": {
"value": 10
}
}
],
"fields": [
{
"id": 2,
"label": "DataCenter",
"type": "text"
},
{
"id": 3,
"label": "CCount",
"type": "numeric"
}
],
"metadata": {
"numFields": 2,
"numRecords": 2,
"skip": 0,
"totalRecords": 2
}
}
Desired Output JSON:
[
{
"DataCenter": "DC1",
"CCount": "5"
},
{
"DataCenter": "DC2",
"CCount": "10"
}
]
You can use this spec:
[
{
"operation": "shift",
"spec": {
"data": "&",
"fields": {
"*": {
"label": "fields.#(1,id)"
}
}
}
},
{
"operation": "shift",
"spec": {
"data": {
"*": {
"*": {
"*": "[&2].#(4,fields.&1)"
}
}
}
}
}
]
In your desired output CCount value is a string, You can add the below spec to change an integer to a string.
,
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"CCount": "=toString"
}
}
}
You might reciprocally match id value of fields array vs. the object keys of the data array within a common object within the first shift transformation spec in order to prepare for the second one in which we tile the attributes into their individual objects such as
[
{
"operation": "shift",
"spec": {
"*": { // represents the node for both "data" and "fields" arrays
"*": {
"*": {
"value": "&1.&",
"#1,label": "#2,id.&"
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"value": {
"*": "[&].#2,label"
}
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is
The Jolt transform should modify the input as shown, I need to push the data aggregated like that in to the DB based on the Key value as identifier.
Input Json :
[
{
"Key": "991641500~2167767723",
"itemNm": "2067875000"
},
{
"Key": "991641500~2167767724",
"itemNm": "2067875085"
},
{
"Key": "991641500~2167767723",
"itemNm": "2067875063"
},
{
"Key": "991641500~2167767724",
"itemNm": "2067875004"
}
]
Output JSON:
The output JSON should be merged as follows, I need to have the key and the contents of those as elements of a JSon Array.
[
{
"Key": "991641500~2167767723",
"Items": [
{
"itemNm": "2067875004"
},
{
"itemNm": "2067875085"
}
]
},
{
"Key": "991641500~2167767724",
"Items": [
{
"itemNm": "2067875000"
},
{
"itemNm": "2067875063"
}
]
}
]
You can use the following transformation spec :
[
{
// group attributes under common objects by their Key(#(1,Key))
"operation": "shift",
"spec": {
"*": {
"Key": "#(1,Key).&",
"itemNm": "#(1,Key).items[&1].&"
}
}
},
{
// get rid of object labels
"operation": "shift",
"spec": {
"*": ""
}
},
{
// get rid of redundant null components of the arrays
"operation": "modify-overwrite-beta",
"spec": {
"*": "=recursivelySquashNulls"
}
},
{
// pick only single one from identical components of each "Key" array
"operation": "cardinality",
"spec": {
"*": {
"Key": "ONE"
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is :
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
I am trying to transform a Json Using Jolt transformation looking for some input here.
I am trying to filter on a Key which is a value of another attribute. here is my input and expected output
{
"pid": "p1#gmail.com",
"eventType": "new",
"details": {
"participants": {
"automationqaus#gmail.com": {
"id": "automationqaus#gmail.com",
"type": "Customer"
},
"p1#gmail.com": {
"id": "p1#gmail.com",
"name": "participantAgent1",
"joinTime": 1617634021103,
"type":"Agent"
},
"p2#gmail.com": {
"id": "p2#gmail.com",
"name": "participantAgent1",
"joinTime": 1617634022303,
"type":"Agent"
}
}
},
"interactionId": "c5804d49-961d-11eb-9650-7fb32f44d8a5"
}
And the Output i am looking at is
{
"pid": "p1#gmail.com",
"name": "participantAgent1",
"joinTime": 1617634021103,
"type":"Agent"
}
and the spec that i tried with is
[
{
"operation": "shift",
"spec": {
"participants": {
"#(2,pid)": {
"#(3,pid)": "pid",
"pType": "type",
"name": "name",
"joinTime":"joinTime"
}
}
}
}
]
But i am not getting expected output. I tried with few other combination too but failed to get correct output.Can some one please help in this?
[ // converting the matching value as the key to the value of the details.
{
"operation": "shift",
"spec": {
"details": "#(1,pid)"
}
},
{
"operation": "shift",
"spec": {
"*": {
"participants": {
"&1": { // matching with the target converted key
"id": "pid",
"type": "type",
"name": "name",
"joinTime": "joinTime"
}
}
}
}
}
]
Jolt is new to me and I have been struggling with this issue till the point where I created this post.
I want to turn this:
{
"Properties": [{
"Id": "property1",
"Values": ["randomValue1", "randomValue2"]
}, {
"Id": "property2",
"Values": "randomValue3"
}, {
"Id": "property3",
"Values": "randomValue4"
}]
}
into this
{
"Properties": [{
"Id": "property1",
"Values": "randomValue1"
},{
"Id": "property1",
"Values": "randomValue2"
}, {
"Id": "property2",
"Values": "randomValue3"
}, {
"Id": "property3",
"Values": "randomValue4"
}]
}
The values for each property can be 1 value or an array of an unknown amount of values.
I changed the following json into what is seen in the first json already:
{
"Properties": {
"property1": ["randomValue1", "randomValue1"],
"property2": ["randomValue3"],
"property3": ["randomValue4"]
}
}
Spec:
[{
"operation": "shift",
"spec": {
"Properties": {
"*": {
"*": "Properties[#2].Values",
"$": "Properties[#2].Id"
}
}
}
}]
Property names on RHS are generic and the number of values for a property can differ as well.
Thank you in advanced for taking the time to assist me.
check if this helps:
[
{
"operation": "cardinality",
"spec": {
"Properties": {
"*": {
// normalize values to always be a list
"Values": "MANY"
}
}
}
},
{
"operation": "shift",
"spec": {
"Properties": {
"*": {
"Values": {
"*": {
// create arrays with values and ids
"#": "Values",
"#(2,Id)": "Id"
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"Values": {
"*": {
// create the final list joining ids and values at the indexes you want
"#": "Properties[&1].Values",
"#(2,Id[#1])": "Properties[&1].Id"
}
}
}
}
]