Jolt transform an object with an array to a single array - java

Hello everyone my json input is this:
{
"chequesCollateral": [
{
"accountNum": "0026.0002.62.0300162968",
"agreement": "0026.5501.90.0490520505",
"checkno": "229425941 ",
"amount": 20000,
"issueBank": "0026",
"branch": "0154",
"currency": "EUR",
"expDate": "2019-09-20"
},
{
"accountNum": "0026.0002.62.0300162968",
"agreement": "0026.5501.90.0490520505",
"checkno": "322108888 ",
"amount": 2500,
"issueBank": "0011",
"branch": "0335",
"currency": "EUR",
"expDate": "2019-10-26"
},
{
"accountNum": "0026.0002.62.0300162968",
"agreement": "0026.5501.90.0490520505",
"checkno": "321979826 ",
"amount": 3964.77,
"issueBank": "0011",
"branch": "0104",
"currency": "EUR",
"expDate": "2019-10-31"
}
]
}
I use this transform to produce this:
[
{
"operation": "shift",
"spec": {
"chequesCollateral": {
"*": {
"issueBank": "distinctBinData.&0"
}
}
}
}
]
and this is produced:
{
"distinctBinData" : {
"issueBank" : [ "0026", "0011", "0011" ]
}
}
I want to produce this :
{
"distinctBinData" : [ "0026", "0011", "0011" ]
}
What should i do?

You don't have to specify the &0 part in the destination key. &0 refers to the current level JSON key, which is issueBank in your case. So when you specified the destination key as distinctBinData.&0, it resolves to distinctBinData.issueBank. So just use distinctBinData as the destination key as follows.
[
{
"operation": "shift",
"spec": {
"chequesCollateral": {
"*": {
"issueBank": "distinctBinData"
}
}
}
}
]

Related

I have an issue in jolt transformation. While transforming I need as expected output but getting different one

I am not able to transform the jolt properly using jolt spec. Thanks in photos array elements can be any like in above two are there, so it can be 3 or 4 or 5 any.
My jolt spec:
Input JSON :
{
"Entity": {
"card": {
"name": "RAM",
"lastName": "ABU"
},
"Photos": [
{
"Id": "327703",
"Caption": "TEST>> photo 1",
"Url": "http://bob.com/0001/327703/photo.jpg"
},
{
"Id": "327704",
"Caption": "TEST>> photo 2",
"Url": "http://bob.com/0001/327704/photo.jpg"
}
]
}
}
Jolt Spec :
[
{
"operation": "shift",
"spec": {
"Entity": {
"card": {
"name": "Photos[0].no",
"lastName": "Photos[0].caption2."
},
"Photos": {
"*": {
"Id": "Photos[&1].no",
"Caption": "Photos[&1].caption2"
}
}
}
}
}
]
Current output :
{
"Photos" : [ {
"no" : [ "RAM", "327703" ],
"caption2" : [ "ABU", "TEST>> photo 1" ]
}, {
"no" : "327704",
"caption2" : "TEST>> photo 2"
} ]
}
Expected output :
{
"Photos" : [
{
"no" : "Ram",
"caption2" : "ABU"
},
{
"no" : "327703" ,
"caption2" : "TEST>> photo 1"
},
{
"no" : "327704",
"caption2" : "TEST>> photo 2"
}
]
}
You can construct a shift transformation spec which will rearrange the desired elements to remain as two individual arrays nested within an object tagged Photos, and then put them into their respective (three) objects such as
[
{
"operation": "shift",
"spec": {
"Entity": {
"card": {
"name": "Photos.no",
"lastName": "Photos.caption2"
},
"Photos": {
"*": {
"Id": "Photos.no",
"Caption": "Photos.caption2"
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": {
"#": "&3[&1].&2" // grouped by "Photos" which is grabbed after going the tree 3 leves up(&3) and binded as array by their respective indexes([&1] which yields 0,1,2) and thier key names(&2)
}
}
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is

Jolt JSON Spec for Nested Object Transformation

I have a requirement to transform the Nested Object in a Json structure.
Here's the Input JSON
Input JSON
{
"data": {
"PRODUCTS": {
"ProductID": "1234-5678",
"ModelNumber": "B550",
"Price": "199",
"Quantity": "1",
"ATTRIBUTES": {
"ProductID": "1234-5678",
"Height": "25",
"Width": "75"
}
}
}
}
Required Output
{
"data": {
"products": [
{
"productId": "1234-5678",
"modelNumber": "B550",
"unitPrice": "199",
"quantity": "1",
"attributes": [
{
"productId": "1234-5678",
"height": "25",
"width": "75"
}
]
}
]
}
}
My JSON Spec:
[
{
"operation": "modify-overwrite-beta",
"spec": {
"data": {
"PRODUCTS": "=toList"
}
}
},
{
"operation": "shift",
"spec": {
"data": {
"PRODUCTS": {
"*": {
"ProductID": "data.products[&1].productId",
"ModelNumber": "data.products[&1].modelNumber",
"Price": "data.products[&1].unitPrice",
"Quantity": "data.products[&1].quantity",
"ATTRIBUTES": {
"ProductID": "data.products[&1].attributes[&1].productId",
"Height": "data.products[&1].attributes[&1].height",
"Width": "data.products[&1].attributes[&1].width"
}
}
}
}
}
},
{
"operation": "default",
"spec": {
"data": {
"*": {}
}
}
}
]
Current Output
{
"data" : {
"products" : [ {
"productId" : "1234-5678",
"modelNumber" : "B550",
"unitPrice" : "199",
"quantity" : "1"
} ]
}
}
I want to convert the ATTRIBUTES nested object to a list and also the nodes inside the ATTRIBUTES object as per the expected output. Can someone throw some light as to how can I achieve this?
You can apply three steps of shift transformations;
To rename all keys as desired
To create the innermost array (attributes)
To create the outermost array (products)
such as
[
{
"operation": "shift",
"spec": {
"data": {
"*": {
"ProductID": "&2.products.productID",
"ModelNumber": "&2.products.modelNumber",
"Price": "&2.products.unitPrice",
"Quantity": "&2.products.quantity",
"*": {
"ProductID": "&3.products.attributes.productId",
"Height": "&3.products.attributes.height",
"Width": "&3.products.attributes.width"
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": {
"attributes": "&2.&1.&[]",
"*": "&2.&1.&"
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": "&1.&[]"
}
}
}
]

unable to transform one json form to another using jolt parser

I have a data like below:
{
"resourceType": "Immunization",
"id": "example",
"protocolApplied": [
{
"series": "2-dose",
"authority": {
"reference": "Organization/org1",
"type": "Organization",
"display": "xyz organization"
},
"targetDisease": [
{
"coding": [
{
"system": "http://snomed.info/sct",
"code": "40468003"
}
]
}
],
"doseNumberPositiveInt": 1,
"seriesDosesPositiveInt": 10
},
{
"series": "3-dose",
"targetDisease": [
{
"coding": [
{
"system": "http://snomed.info/sct",
"code": "66071002"
}
]
}
],
"doseNumberString": "one",
"seriesDosesString": "ten"
}
]
}
I need to transform it to get the below output:
[ {
"resourceType" : "Immunization",
"series" : "2-dose",
"reference" : "Organization/org1",
"type" : "Organization",
"display" : "xyz organization",
"targetDiseaseCodingSystem":"http://snomed.info/sct"
"targetDiseaseCode":"40468003"
"doseNumberPositiveInt" : 1,
"seriesDosesPositiveInt" : 10
}, {
"resourceType" : "Immunization",
"series" : "3-dose",
"targetDiseaseCodingSystem":"http://snomed.info/sct"
"targetDiseaseCode": "66071002"
"doseNumberString" : "one",
"seriesDosesString" : "ten"
} ]
Below is my spec:
[
{
"operation": "shift",
"spec": {
"protocolApplied": {
"*": {
"#(2,resourceType)": "[#2].resourceType",
"authority": {
// "reference": "reference"
"*": "[#3].&"
},
"targetDisease": {
"*": {
"coding": {
"*": {
//"*": "[#2].&"
"#(2,system)": "[#2].targetDiseaseCodingSystem"
}
}
}
},
"*": "[#2].&"
}
}
}
}
]
I am getting below output after applying above spec:
[ {
"resourceType" : "Immunization",
"series" : "2-dose",
"reference" : "Organization/org1",
"type" : "Organization",
"display" : "xyz organization",
"doseNumberPositiveInt" : 1,
"seriesDosesPositiveInt" : 10
}, {
"resourceType" : "Immunization",
"series" : "3-dose",
"doseNumberString" : "one",
"seriesDosesString" : "ten"
} ]
Where targetcodingsystem and targetdiseasecode is not populated. Please help me in this.
Below is spec would help,
Use [&n], to shift the value n object above.
[
{
"operation": "shift",
"spec": {
"protocolApplied": {
"*": {
"#(2,resourceType)": "[&1].resourceType",
"authority": {
"*": "[&2].&"
},
"targetDisease": {
"*": {
"coding": {
"*": {
"system": "[&5].targetDiseaseCodingSystem",
"code": "[&5].targetDiseaseCode"
}
}
}
},
"*": "[&1].&"
}
}
}
}
]

Transforming Array of objects with nested array objects using JOLT

I want to transform following input JSON to output JSON format
INPUT JSON:
[
{
"orderNumber": "201904-000000001",
"items": [
{
"itemPrice": 40000,
"itemQuantity": 11,
"item": {
"external_id": "IPHONE"
}
},
{
"itemPrice": 25000,
"itemQuantity": 22,
"item": {
"external_id": "ONEPLUS"
}
},
{
"itemPrice": 35000,
"itemQuantity": 33,
"item": {
"external_id": "SAMSUNGS10"
}
}
]
}
]
OUTPUT JSON:
[{
"orderNumber" : "201904-000000001",
"items" : [ {
"itemQuantity" : 11,
"external" : "IPHONE"
} ]
},
{
"orderNumber" : "201904-000000001",
"items" : [ {
"itemQuantity" : 22,
"external" : "ONEPLUS"
} ]
},
{
"orderNumber" : "201904-000000001",
"items" : [ {
"itemQuantity" : 33,
"external" : "SAMSUNGS10"
} ]
}]
I have tried following spec which is not working...could someone guide me about spec I should use and explain each step if possible if nested arrays and objects are even deeper how to convert
SPEC I HAVE USED:
[
{
"operation": "shift",
"spec": {
"*": {
"orderNumber": "[&1].orderNumber",
"items": {
"*": {
"itemQuantity": "[&1].items[].itemQuantity",
"item": {
"external_id": "[&1].items[].external"
}
}
}
}
}
}
]
Thanks guys, Following spec did work for me after trying different combinations. If anyone comes across this question please explain to me the answer for not trying combinations next time
[
{
"operation": "shift",
"spec": {
"*": {
"orderNumber": "[&1].orderNumber",
"items": {
"*": {
"itemQuantity": "[&3].items[&1].itemQuantity",
"item": {
"external_id": "[&4].items[&2].external"
}
}
}
}
}
}
]

Jolt conditional spec

I want a conditional transformation where I need to add a property in output if the value of a specific field in input matches my condition. Below is my input and output required.
Input
{
"attr": [
{
"name": "first",
"validations": [
{
"type": "Required",
"value": true
}
]
},
{
"name": "last",
"validations": [
{
"type": "lenght",
"value": "10"
}
]
},
{
"name": "email",
"validations": [
{
"type": "min",
"value": 10
}
]
}
]
}
Output
{
"out": [
{
"name": "first",
"required": "yes"
},
{
"name": "last"
},
{
"name": "email"
}
]
}
So I am able to get till the condition, but inside condition, & and # are being respective to the input rather than to the output. Can anybody help me out with the transformation? Below is the spec I have written so far.
[
{
"operation": "shift",
"spec": {
"attr": {
"*": {
"name": "out.&1.name",
"validations": {
"*": {
"type": {
"Required": {
"#(2,value)": "out.&1.req"
}
}
}
}
}
}
}
}
]
This spec does the transform.
[
{
"operation": "shift",
"spec": {
"attr": {
"*": {
"name": "out[&1].name",
"validations": {
"*": {
"type": {
"Required": {
"#yes": "out[&5].required"
}
}
}
}
}
}
}
}
]
However, I think you meant to grab the "value" : true that is a sibling of the "Required" : true, rather than have the output be "yes".
If so swap in this bit.
"Required": {
"#(2,value)": "out[&5].required"
}

Categories

Resources