sort an array of json document - json

I'm wondering if it's possible to sort or bring the min value in case of an array of json. I read something about this issue but found nothing.
This is the Input:
{
"intData": [
{
"DATE": "2018",
"NOME": "raf"
},
{
"DATE": "2001",
"NOME": "fabio"
},
{
"DATE": "2002",
"NOME": "fabiola"
}
]
}
I would:
{
"intData": [
{
"DATE": "2001",
"NOME": "fabio"
},
{
"DATE": "2002",
"NOME": "fabiola"
},
{
"DATE": "2018",
"NOME": "raf"
}
]
}
or
{
"DATE": "2001",
"NOME": "fabio"
}
Is it possible?

Ordered Results
The steps are as follows:
Create object with structure: $.DATE.NOME.#
Sort it
Turn it back into an array
[
{
"operation": "shift",
"spec": {
"intData": {
"*": {
"#": "#(1,DATE).#(1,NOME)"
}
}
}
},
{
"operation": "sort"
},
{
"operation": "shift",
"spec": {
"*": {
"*": {
"#": "intData.[]"
}
}
}
}
]
First result
The steps are as follows:
Create object with structure: $.DATE.NOME.#
Sort it
Turn it back into an array
Get first result
[
{
"operation": "shift",
"spec": {
"intData": {
"*": {
"#": "#(1,DATE).#(1,NOME)"
}
}
}
},
{
"operation": "sort"
},
{
"operation": "shift",
"spec": {
"*": {
"*": {
"#": "[]"
}
}
}
},
{
"operation": "shift",
"spec": {
"0": {
"#": ""
}
}
}
]

Related

Masking cards usgins jolt

How can I iterate through list and replace some elements in it? I have this input
{
"clients": [
{
"clientId": "166734",
"info": {
"cards": [
"378282246310005",
"371449635398431"
]
}
}
]
}
I want mask cards like this "cards" : [ "3782*****0005", "3714*****8431" ]
You can use shift transformations to convert the cards array to indexed and 0,1,** labeled objects in order to prepare for modify transformation spec in which substring and concat functions are used such as
[
{
"operation": "shift",
"spec": {
"clients": {
"*": {
"*": "&2[#2].&",
"info": {
"cards": {
"*": {
"#": "&5[#5].&3.&2.&1.&"
}
}
}
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"clients": {
"*": {
"info": {
"cards": {
"*": {
"p1": "=substring(#(1,&1),0,4)",
"p2": "=substring(#(1,&1),11,15)",
"cs": "=concat(#(1,p1),'********',#(1,p2))"
}
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"clients": {
"*": {
"*": "&2[#2].&",
"info": {
"cards": {
"*": {
"cs": "&5[#5].&3.&2[]"
}
}
}
}
}
}
}
]

How to write JOLT Spec for nested arrays

I am trying to transform a JSON using JOLT. This JSON consists of nested arrays and I am not able to transform it correctly. Can someone please help. Thanks.
{
"root": [
{
"id": "1234",
"password": "key1234",
"devices": [
{
"details": {
"deviceType": "tv-iot",
"deviceId": "tv-iot-111"
}
},
{
"details": {
"deviceType": "machine-iot",
"deviceId": "machine-iot-999"
}
}
]
},
{
"id": "6789",
"password": "key6789",
"devices": [
{
"details": {
"deviceType": "phone-iot",
"deviceId": "phone-iot-111"
}
},
{
"details": {
"deviceType": "mobile-iot",
"deviceId": "mobile-iot-999"
}
}
]
}
]
}
This is the spec that I have written.
[
{
"operation": "shift",
"spec": {
"root": {
"*": {
"id": "[&1].userid",
"password": "[&1].pwd",
"devices": {
"*": {
"details": {
"deviceType": "[&2].deviceCategory",
"deviceId": "[&2].deviceUniqueValue"
}
}
}
}
}
}
}
]
The expected JSON that I am looking for is:
[
{
"userid": "1234",
"pwd": "key1234",
"devices": [
{
"details": {
"deviceCategory": "tv-iot",
"deviceUniqueValue": "tv-iot-111"
}
},
{
"details": {
"deviceCategory": "machine-iot",
"deviceUniqueValue": "machine-iot-999"
}
}
]
},
{
"userid": "6789",
"pwd": "key6789",
"devices": [
{
"details": {
"deviceCategory": "phone-iot",
"deviceUniqueValue": "phone-iot-111"
}
},
{
"details": {
"deviceCategory": "mobile-iot",
"deviceUniqueValue": "mobile-iot-999"
}
}
]
}
]
However, I get this wrong output. Somehow, my nested objects are getting transformed into list.
[
{
"userid" : "1234",
"pwd" : "key1234",
"deviceCategory" : [ "tv-iot", "phone-iot" ],
"deviceUniqueValue" : [ "tv-iot-111", "phone-iot-111" ]
},
{
"deviceCategory" : [ "machine-iot", "mobile-iot" ],
"deviceUniqueValue" : [ "machine-iot-999", "mobile-iot-999" ],
"userid" : "6789",
"pwd" : "key6789"
}
]
I am unable to figure out what is wrong. Can someone please help?
UPDATE(Solution): Was able to come up with a shorter spec that works as well !
[
{
"operation": "shift",
"spec": {
"root": {
"*": {
"id": "[&1].userId",
"password": "[&1].pwd",
"*": "[&1].&"
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"devices": {
"*": {
"details": {
"deviceType": "[&4].&3.[&2].&1.deviceCategory",
"deviceId": "[&4].&3.[&2].&1.deviceUniqueVal"
}
}
},
"*": "[&1].&"
}
}
}
]
You can start by deep diving into the innermost object while partitioning the sub-objects by id values through a shift transformation such as
[
{
"operation": "shift",
"spec": {
"root": {
"*": {
"devices": {
"*": {
"details": {
"*": {
"#(4,id)": "#(5,id).userid",
"#(4,password)": "#(5,id).pwd",
"#": "#(5,id).devicedetails[&3].&2.&1"
}
}
}
}
}
}
}
},
{
// get rid of top level object names
"operation": "shift",
"spec": {
"*": ""
}
},
{
// get rid of repeating components of each arrays respectively
"operation": "cardinality",
"spec": {
"*": {
"us*": "ONE",
"pwd": "ONE"
}
}
},
{
// determine new key names for attributes respectively
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"*": {
"*": {
"*": {
"deviceCategory": "=(#(1,deviceType))",
"deviceUniqueValue": "=(#(1,deviceId))"
}
}
}
}
}
},
{
// get rid of extra elements generated
"operation": "remove",
"spec": {
"*": {
"*": {
"*": {
"*": {
"deviceType": "",
"deviceId": ""
}
}
}
}
}
}
]

Conversion of if/else based JSON by using Jolt

I have two following json input and i want to convert it into expected output by jolt
input - 1
{
"alert_array": {
"alerts": [
{
"id": "1234",
"data": {
"parameter": [
{
"key": "level",
"value": "93.5"
}
]
}
}
]
}
}
input - 2
{
"alert_array": {
"alerts": [
{
"id": "1234",
"data": {}
}
]
}
}
expected output - 1
{
"alertArray": [
{
"id": "1234",
"properties": [
{
"key": "level",
"value": "93.5"
}
]
}
]
}
expected output - 2
{
"alertArray": [
{
"id": "1234",
"properties": []
}
]
}
In input-1 data contain some parameter but in input-2 data object is blank
You can use default transformation along with shift transformations
in order to add "properties": [] for the empty case of "data" object for the both of the input
JSON values such as
[
{
"operation": "shift",
"spec": {
"*": {
"alerts": {
"*": {
"id": "alertArray.&",
"*": {
"*": {
"*": {
"id": "alertArray.properties[&1].key",
"*": "alertArray.properties[&1].&"
}
}
}
}
}
}
}
},
{
"operation": "default",
"spec": {
"*": {
"properties": []
}
}
},
{
"operation": "shift",
"spec": {
"*": "&[]"
}
}
]
the demo for the first case :
the demo for the second case :
If there was no such need(to fill up for the "data"), then the following single spec would be sufficient :
[
{
"operation": "shift",
"spec": {
"*": {
"alerts": {
"*": {
"id": "alertArray[&1].&",
"*": {
"*": {
"*": {
"id": "alertArray[&4].properties[&1].key",
"*": "alertArray[&4].properties[&1].&"
}
}
}
}
}
}
}
}
]

Update json attribute with a condition in jolt

I have a problem, I don't understand how to update an attribute with a condition in jolt. For example, I have an Object with an inner array of Items. I need to update an Item attribute if another Item attribute equals to something and to return the Object.
Input:
{
"object": {
"id": "3cf1543e-be4d-11eb-84c0-87ba01ce01e0",
"a": "abc",
"del_sign": false,
"items": [
{
"id": "111",
"del_sign": false
},
{
"id": "222",
"del_sign": false
},
{
"id": "333",
"del_sign": false
}
],
"b": [],
"c": []
}
}
I need:
{
"object": {
"id": "3cf1543e-be4d-11eb-84c0-87ba01ce01e0",
"a": "abc",
"del_sign": false,
"items": [
{
"id": "111",
// here changes to true
"del_sign": true
},
{
"id": "222",
"del_sign": false
},
{
"id": "333",
"del_sign": false
}
],
"b": [],
"c": []
}
}
My current jolt spec:
[
{
"operation": "shift",
"spec": {
"object": {
"items": {
"*": {
"id": {
"111": {
"#2": {
"#true": "del_sign",
"$1": "&3"
}
}
}
}
}
}
}
}
]
You can use two step of shift transformations.
Determine the related del_sign value within a conditional as having two arrays for id and del_sign within the same object(items), and properly format them within the second step such as
[
{
"operation": "shift",
"spec": {
"*": {
"*": "&",
"items": {
"*": {
"id": {
"111": {
"#(2,&1)": "&4.&3.id",
"#true": "&4.&3.del_sign"
},
"*": {
"#1": "&4.&3.&2",
"#(2,del_sign)": "&4.&3.del_sign"
}
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": "&",
"items": {
"*": {
"*": "&2.[#2].&"
}
}
}
}
]
Edit : If there are more attributes other than the current ones(id,del_sign), then prefer using the following code in order not to individually repeat the each key such as
[
{
"operation": "shift",
"spec": {
"*": {
"*": "&",
"items": {
"*": {
"id": {
"111": {
"#2": {
"#true": "&5.&2.del_sign",
"*": "&5.&1.&"
}
},
"*": {
"#2": "&4.&3"
}
}
}
}
}
}
},
{
"operation": "cardinality",
"spec": {
"items": {
"*": {
"del_sign": "ONE"
}
}
}
},
{
"operation": "shift",
"spec": {
"*": "&",
"items": {
"*": {
"*": "&2.[#2].&"
}
}
}
}
]

With JOLT need to Transform JSON object with If Then Else Condition

Below is my Input Json
[
{
"correlationId": "12345",
"payloadFormat": "Money",
"payload": {
"DE35": "123654ABC54678",
"DE45": "898454PQR54678"
}
}
]
I need to Trnsaform this json in such a way if DE35 has value say of substring 6-9 then take DE35 value in Transform Json, if DE35 do not have value then Take DE45 value in transform Json.
So output will be if DE35 Available then
[ {
"COR_ID" : "12345",
"payloadFormat" : "Money",
"EXT_SERV_CD":"ABC"
} ]
if DE35 is not available then the output should be
[ {
"COR_ID" : "12345",
"payloadFormat" : "Money",
"EXT_SERV_CD":"PQR"
} ]
I am using Below JOLT spec to do Transformation but it is not working.
[
{
"operation": "shift",
"spec": {
"*": {
"#": "&",
"payload": {
"DE|DE35": "&2.payload.TMPDE35"
}
}
}
}, {
"operation": "modify-default-beta",
"spec": {
"*": {
"payload": {
"DE35Val": "=substring(#(1,TMPDE35), 6, 9)"
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"#": "&",
"payload": {
"DE|DE45": "&2.payload.TMPDE45"
}
}
}
}, {
"operation": "modify-default-beta",
"spec": {
"*": {
"payload": {
"DE45Val": "=substring(#(1,TMPDE45), 6, 9)"
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"correlationId": "[&1].COR_ID",
"payloadFormat": "[&1].payloadFormat",
"payload": {
"DE35Val": {
"ABC": {
"DE35Val": "[#3].payload.EXT_SERV_CD"
},
"false": {
"DE45Val": "[#3].payload.EXT_SERV_CD"
}
}
}
}
}
}
]
Please suggest where I am making mistake.
This spec should work for you
[
{
"operation": "modify-default-beta",
"spec": {
"*": {
"TmpDE35": "=substring(#(1,payload.DE35), 6, 9)",
"TmpDE45": "=substring(#(1,payload.DE45), 6, 9)"
}
}
},
{
"operation": "modify-default-beta",
"spec": {
"*": {
"Tmp35_45": "=concat(#(1,TmpDE35), #(1,TmpDE45))"
}
}
},
{
"operation": "modify-default-beta",
"spec": {
"*": {
"EXT_SERV_CD": "=substring(#(1,Tmp35_45), 0, 3)"
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": "[&1].&",
"Tmp*": null,
"payload": null
}
}
}
]
The solution idea:
Move the substrings of the DE35, DE45 fields to the TmpDE35, TmpDE45
Concatenate TmpDE35, TMPDE45 and store it in the Tmp35_45. If TmpE35 is empty then the first 3 characters will come from TmpDE45
Get the substring Tmp35_45
Clean up, shift