I need to rearrange incoming structure and output multiple duplicate keys with different values, but jolt spec is giving me array instead of keys
Input data:
{
"TestData": {
"data": {
"Products": [
{
"Name": {
"key": "Name__c",
"value": "Example1"
}
},
{
"Name": {
"key": "Name__c",
"value": "Example2"
}
}
]
}
}
}
Desired output:
{
"TestData": {
"data": {
"Name__c": "Example1",
"Name__c": "Example2"
}
}
}
When i'm using following spec:
[
{
"operation": "shift",
"spec": {
"*": {
"data": {
"Products": {
"*": {
"Name": {
"value": "TestData.data.#(1,key)"
}
}
}
}
}
}
}
]
Its giving me
{
"TestData": {
"data": {
"Name__c": [
"Example1",
"Example2"
]
}
}
}
Does anyone know how to handle this in Jolt?
Thanks
Related
I have a JSON object like this:
{
"data":{
"list1":[
{
"id":"1",
"description":"abc..",
"title":"abc"
},
{
"id":"2",
"description":"hello..",
"title":"hello"
},
{
"id":"3",
"description":"hello..",
"title":"hello"
},
{
"id":"4",
"description":"hello..",
"title":"hello"
},
{
"id":"5",
"description":"hello..",
"title":"hello"
},
{
"id":"6",
"description":"hello..",
"title":"hello"
}
],
"list2":[
{
"info":"this is list2",
"idList":[
"1",
"3"
]
},
{
"info":"this is list2",
"idList":[
"2",
"4"
]
}
]
}
}
In the transformed JSON I want everything of list1 inside idList of list2 whenever the id matches. Like this:
{
"res":{
"data":[
{
"info":"this is list2",
"idList":[
{
"id":"1",
"description":"abc..",
"title":"abc"
},
{
"id":"3",
"description":"hello..",
"title":"hello"
}
]
},
{
"info":"this is list2",
"idList":[
{
"id":"2",
"description":"hello..",
"title":"hello"
},
{
"id":"4",
"description":"hello..",
"title":"hello"
}
]
}
]
}
}
How can I use Jolt Spec to achieve this transformation?
[
{
"operation": "shift",
"spec": {
"data": {
"list1": {
"*": {
"id": {
"#(3,list2[&].idList)": {
"#(4,list2[&].idList)": "res.data[&3].idList"
}
}
}
},
"list2": {
"*": {
"info": "res.data[&1].info"
}
}
}
}
}
]
I tried this spec to match those two lists however I don't get anything for the list1 spec. The result I get is only the info part:
{
"res" : {
"data" : [ {
"info" : "this is list2"
}, {
"info" : "this is list2"
} ]
}
}
You can use this spec:
[
{
"operation": "shift",
"spec": {
"*": {
"list1": {
"*": {
"*": "&3.&2.#(1,id).&"
}
},
"*": "&1.&"
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"list2": {
"*": {
"*": "res.&3[&1].&",
"idList": {
"*": {
"*": {
"#(5,list1.&)": "res.&6.[&4].&3"
}
}
}
}
}
}
}
}
]
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": ""
}
}
}
}
}
}
]
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].&"
}
}
}
}
}
}
}
}
]
I have a requirement for transforming JSON and I am trying to use the same value multiple times. Is there a way to use value multiple times previously I did use in array but this time I have to go through the level. Any help is appreciated and thank you.
Note I want to filter product configurations based on the name.
How to use same field value at multiple places in Jolt
Input:
{
"sample": {
"Result": {
"value": {
"ImplProductConfigurationsOptionsEcomResponse": {
"transactionId": "12345678",
"timeStamp": 1646087177771,
"orderCorrelationId": "11039714",
"implValidateMultiProductConfigurationsResponse": {
"productConfigurations": [
{
"productConfiguration": {
"productSpecification": {
"name": "Private",
"id": "247541"
}
}
},
{
"productConfiguration": {
"productSpecification": {
"name": " sample Miscellaneous",
"id": "22222"
}
}
}
]
}
}
}
}
}
}
Current output:
{
"productConfigurations": {
"productConfiguration": {
"productSpecification": {
"name": "Private",
"id": "247541"
}
}
}
}
Expected Output:
{
"productConfigurations": {
"productConfiguration": {
"productSpecification": {
"name": "Private",
"id": "247541"
}
}
},
"Details": {
"ImplProductConfigurationsOptionsEcomResponse": {
"transactionId": "12345678",
"timeStamp": 1646087177771,
"orderCorrelationId": "11039714",
"implValidateMultiProductConfigurationsResponse": {
"productConfigurations": [
{
"productConfiguration": {
"productSpecification": {
"name": "Private",
"id": "247541"
}
}
},
{
"productConfiguration": {
"productSpecification": {
"name": " sample Miscellaneous",
"id": "22222"
}
}
}
]
}
}
}
}
My Spec:
[
{
"operation": "shift",
"spec": {
"sample": {
"Result": {
"value": {
"ImplProductConfigurationsOptionsEcomResponse": {
"implValidateMultiProductConfigurationsResponse": {
"productConfigurations": {
"*": {
"productConfiguration": {
"productSpecification": {
"name": {
"*Miscellaneous": null,
"*": {
"#4": "productConfigurations"
}
}
}
}
}
}
}
}
}
}
}
}
}
]
You can reference the object twice along with a shift transformation spec such as
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": {
"*": {
"*": "Details.&1.&",
"implValidateMultiProductConfigurationsResponse": {
"*": {
"*": {
"*": {
"*": {
"name": {
"*Miscellaneous": {
"#3": "Details.&8.&7.&6[].&4"
},
"*": {
"#4": "&6",
"#3": "Details.&8.&7.&6[].&4"
}
}
}
}
}
}
}
}
}
}
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/
I need to split the data list into two separate idExists and idNotExists lists, based on whether id exists or not.
Can someone help me with Jolt Specification to achieve the below results?
Input:
{
"data": [
{
"name": "a",
"id": "100"
},
{
"name": "b",
"id": "101"
},
{
"name": "c"
}
]
}
Desired Output:
{
"IdExists": [
{
"name": "a",
"id": "100"
},
{
"name": "b",
"id": "101"
}
],
"IdNotExists": [
{
"name": "c"
}
]
}
I have tried with the below spec but it is not working as expected
[
{
"operation": "modify-default-beta",
"spec": {
"data": {
"": {
"id": "NotExist"
}
}
}
},
{
"operation": "shift",
"spec": {
"data": {
"": {
"id": {
"10*": {
"#2": "IdExists[]"
},
"*": {
"#2": "IdNotExists[]"
}
}
}
}
}
}
]
I had some modification on your spec, added * as it was missing in the selectors.
Modified shifting condition to NotExist and finally shift only name to the IdNotExists array.
[
{
"operation": "modify-default-beta",
"spec": {
"data": {
"*": {
"id": "NotExist"
}
}
}
},
{
"operation": "shift",
"spec": {
"data": {
"*": {
"id": {
"NotExist": {
"#(2,name)": "IdNotExists[].name"
},
"*": {
"#2": "IdExists[]"
}
}
}
}
}
}
]