I want to rename fields in an array nested in an another array using JOLT transformation library.
1. One field to rename is a top level field in an array
2. Two fields to rename are inside a nested array
I have tried using wildcards but they are not giving me expected output. I am using JOLT 0.0.22 version.
Input JSON:
{
"country": "usa",
"state": [
{
"stateName": "TX",
"location": "south",
"cities": [
{
"name": "Austin",
"pop": "1M"
},
{
"name": "Dallas",
"pop": "2M"
}
]
},
{
"stateName": "CA",
"location": "west",
"cities": [
{
"name": "SanFran",
"pop": "3M"
},
{
"name": "LosAngeles",
"pop": "4M"
}
]
}
]
}
Expected Output :
{
"country": "usa",
"state": [
{
"stateName": "TX",
"locatedIn": "south", // name change here
"cities": [
{
"cityname": "Austin", // name change here
"citypopulation": "1M" // name change here
},
{
"cityname": "Dallas",
"citypopulation": "2M"
}
]
},
{
"stateName": "CA",
"locatedIn": "west",
"cities": [
{
"cityname": "SanFran",
"pop": "3M"
},
{
"cityname": "LosAngeles",
"citypopulation": "4M"
}
]
}
]
}
Spec
[
{
"operation": "shift",
"spec": {
"country": "country",
"state": {
"*": { // state array index
"stateName": "state[&1].stateName",
"location": "state[&1].location",
"cities": {
"*": { // city array index
"name": "state[&3].cities[&1].cityname",
"pop": "state[&3].cities[&1].citypopualtion"
}
}
}
}
}
}
]
For comparison, this is the solution in JSLT.
{
"state" : [for (.state) . | {
"locatedIn" : .location,
"cities" : [for (.cities) {
"cityname" : .name,
"citypopulation" : .pop
}],
* : .
}],
* : .
}
Related
I have an array of JSON Objects that I need to transform into a new array of JSON Objects that align with my schema. I am new to Jolt and my output is not what I need. I've attempted using the [&1] and # operators within but I have yet to get my desired output.
My input:
{
"totalCount": 2,
"entities": {
"people": [
{
"uniqueIdentifier": "0105",
"common": {
"firstName": "Bob",
"lastName": "Smith",
"geoLocation": {
"geometry": {
"point": {
"coordinates": [
39.93479016,
47.21850072
]
}
}
},
"timeOfReporting": "2019-11-18T18:36:10Z"
},
"type": "PERSON",
"parent": {
"firstName": "Jane",
"lastName": "Smith"
}
},
{
"uniqueIdentifier": "0106",
"common": {
"firstName": "Joe",
"lastName": "Green",
"geoLocation": {
"geometry": {
"point": {
"coordinates": [
39.93479016,
47.21850072
]
}
}
},
"timeOfReporting": "2019-11-18T18:36:10Z"
},
"Type": "PERSON",
"parent": {
"firstName": "John",
"lastName": "Green"
}
}
]
}
}
My Jolt Spec:
[
{
"operation": "shift",
"spec": {
"entities": {
"people": {
"*": {
"uniqueIdentifier": "Person.Id",
"type": "Person.Type",
"parent": {
"firstName": "Person.Parent.FirstName",
"lastName": "Person.Parent.LastName"
},
"common": {
"firstName": "Person.FirstName",
"lastName": "Person.LastName",
"timeOfReporting": "Person.DateCreated",
"geoLocation": {
"geometry": {
"point": {
"coordinates": {
"0": "Person.Locations.Location.Longitude",
"1": "Person.Locations.Location.Latitude"
}
}
}
}
}
}
}
}
}
},
{
"operation": "default",
"spec": {
"Person": {
"Processed": "false"
}
}
}
]
Current Output:
{
"Person" : {
"DateCreated" : [ "2019-11-18T18:36:10Z", "2019-11-18T18:36:10Z" ],
"FirstName" : [ "Bob", "Joe" ],
"Id" : [ "0105", "0106" ],
"LastName" : [ "Smith", "Green" ],
"Locations" : {
"Location" : {
"Latitude" : [ 47.21850072, 47.21850072 ],
"Longitude" : [ 39.93479016, 39.93479016 ]
}
},
"Parent" : {
"FirstName" : [ "Jane", "John" ],
"LastName" : [ "Smith", "Green" ]
},
"Processed" : "false",
"Type" : "PERSON"
}
}
Desired Output:
{
"Person":{
"DateCreated": "2019-11-18T18:36:10Z",
"FirstName": "Bob",
"LastName" "Smith"
"Id": "0105",
"Locations": {
"Location": {
"Latitude": 47.2184,
"Longitude": 39.7854
}
},
"Parent": {
"FirstName": "Jane",
"LastName": "Smith"
},
"Processed": "false",
"Type": "PERSON"
},
"Person": {
"DateCreated": "2019-11-18T18:36:10Z",
"FirstName": "Joe",
"LastName": "Green"
"Id": "0106",
"Locations": {
"Location": {
"Latitude": 42.2184,
"Longitude": 31.7854
}
},
"Parent": {
"FirstName": "John",
"LastName": "Green"
},
"Processed": "false",
"Type": "PERSON"
},
}
You can't have two Person keys in the desired output. But, You can use one of these jolt specs according to your desired output:
1. Save all person objects in an array
[
{
"operation": "shift",
"spec": {
"*": {
"people": {
"*": {
"uniqueIdentifier": "Person[&1].Id",
"type": "Person[&1].Type",
"parent": {
"firstName": "Person[&2].Parent.FirstName",
"lastName": "Person[&2].Parent.LastName"
},
"common": {
"firstName": "Person[&2].FirstName",
"lastName": "Person[&2].LastName",
"timeOfReporting": "Person[&2].DateCreated",
"geoLocation": {
"geometry": {
"point": {
"coordinates": {
"0": "Person[&6].Locations.Location.Longitude",
"1": "Person[&6].Locations.Location.Latitude"
}
}
}
}
}
}
}
}
}
},
{
"operation": "modify-default-beta",
"spec": {
"*": {
"*": {
"Processed": "false"
}
}
}
}
]
2. Save each person's object in a new object:
[
{
"operation": "shift",
"spec": {
"*": {
"people": {
"*": {
"uniqueIdentifier": "Person.&1.Id",
"type": "Person.&1.Type",
"parent": {
"firstName": "Person.&2.Parent.FirstName",
"lastName": "Person.&2.Parent.LastName"
},
"common": {
"firstName": "Person.&2.FirstName",
"lastName": "Person.&2.LastName",
"timeOfReporting": "Person.&2.DateCreated",
"geoLocation": {
"geometry": {
"point": {
"coordinates": {
"0": "Person.&6.Locations.Location.Longitude",
"1": "Person.&6.Locations.Location.Latitude"
}
}
}
}
}
}
}
}
}
},
{
"operation": "modify-default-beta",
"spec": {
"*": {
"*": {
"Processed": "false"
}
}
}
}
]
I am new to jolt and having tough time to get the desired output
Input JSON
[
{
"getPatientDemographicDetailsOutput": [
{
"Message": "SUCCESS",
"Results": [
[
{
"APS_Age__c": "7 Years",
"Id": "1234",
"LastName": "LName0915-6",
"APS_DOB__c": "2014-11-25",
"Contacts__r": {
"totalSize": 2,
"records": [
{
"Alternate_Phone_1__c": "1458296321",
"Alternate_Phone_2__c": "(732) 318-3232",
"Correspondence_City__c": "Charlotte",
"Email_Address__c": "sohel#abc.com",
"Correspondence_State__c": "NC",
"State__c": "NC",
"Patient__c": "1234",
"Correspondence_Time_Zone__c": "EST",
"Correspondence_ZipCode_Name__c": "28222",
"Primary__c": true,
"Primary_Phone__c": "Mobile",
"Correspondence_Country__c": "USA",
"Country__c": "USA",
"Address_Line_2__c": "City",
"City__c": "Charlotte",
"Type__c": "Self",
"Zip_Code_Name__c": "28222",
"Address_Line_1__c": "Bangalore",
"Id": "11111",
"Correspond_Address_Line_1__c": "Bangalore",
"Correspond_Address_Line_2__c": "City",
"Mobile_Phone_for_Campaigns__c": "+1(732) 318-1232"
},
{
"Correspondence_City__c": "INTERNAL REVENUE SERVICE",
"Correspondence_State__c": "NY",
"Type__c": "Caregiver",
"Patient__c": "1234",
"Id": "22222",
"Correspond_Address_Line_1__c": "test address",
"Correspondence_Time_Zone__c": "EST",
"Correspondence_ZipCode_Name__c": "00501",
"Primary__c": false,
"Correspondence_Country__c": "USA"
}
],
"done": true
}
}
],
[
{
"Contact__c": "11111",
"Text_Consent_Date__c": "2019-11-23",
"Text_Messaging__c": "Yes",
"Id": "54545454"
}
]
]
}
]
}
]
Expected Output
[
{
"APS_Age__c": "7 Years",
"Id": "1234",
"LastName": "LName0915-6",
"APS_DOB__c": "2014-11-25",
"Alternate_Phone_1__c": "1458296321",
"Alternate_Phone_2__c": "(732) 318-3232",
"Correspondence_City__c": "Charlotte",
"Correspond_Address_Line_1__c": "Bangalore",
"Correspond_Address_Line_2__c": "City",
"Mobile_Phone_for_Campaigns__c": "+1(732) 318-1232",
"Primary__c": true,
"Text_Consent_Date__c": "2019-11-23",
"Text_Messaging__c": "Yes"
}
]
Logic to transform last two fields Text_Consent_Date__c and Text_Messaging__c
There are two arrays in "Results" in input JSON. I want to select Text_Consent_Date__c and Text_Messaging__c from second array if Primary__c = true(select only primary contact from primary contact list) and Id (from Contacts__r) matches with Contact__c(field in second json array)
This shift operation will help you generate the required json :
[
{
"operation": "shift",
"spec": {
"*": {
"getPatientDemographicDetailsOutput": {
"*": {
"Results": {
"*": {
"*": {
"*": "[&2].&",
"Contacts__r": {
"records": {
"*": {
"Alternate_Phone_1__c": "[&1].&",
"Alternate_Phone_2__c": "[&1].&",
"Correspondence_City__c": "[&1].&",
"Correspond_Address_Line_1__c": "[&1].&",
"Correspond_Address_Line_2__c": "[&1].&",
"Mobile_Phone_for_Campaigns__c": "[&1].&",
"Primary__c": "[&1].&"
}
}
},
"Text_Consent_Date__c": "[&1].&",
"Text_Messaging__c": "[&1].&"
}
}
}
}
}
}
}
}
]
This is my first time to use JoltTransformationJson, so I have limited knowledge and experience on that. Please help me with this complicated project.
Request:
when the payment.code <> "paid", I have to do the following two things for the file.
to change the payment.code ="denied" and payment.text ="denied"
to add a JSON object to item.ADJ
When the payment.code =="paid", don't need to change anything.
Input :
{
"resourceType": "E",
"id": "11",
"identifier": [
{
"type": {
"coding": [
{
"system": "sys1",
"code": "aaa"
}
]
},
"value": "212"
},
{
"type": {
"coding": [
{
"system": "sys2",
"code": "RRR"
}
]
},
"value": "367"
}
],
"status": "active",
"created": "2021-08-05T02:43:48+00:00",
"outcome": "complete",
"item": [
{
"sequence": 1,
"product": {
"coding": [
{
"system": "example",
"code": "abc",
"display": "ABC"
}
],
"text": "ABC"
},
"servicedDate": "2021-08-04",
"quantity": {
"value": 60
},
"ADJ": [
{
"category": {
"coding": [
{
"system": "code1",
"code": "code1",
"display": "CODE1"
}
],
"text": "CODE1"
},
"amount": {
"value": 46.45,
"currency": "USD"
}
},
{
"category": {
"coding": [
{
"system": "code2",
"code": "code2",
"display": "CODE2"
}
],
"text": "CODE2"
},
"amount": {
"value": 12.04,
"currency": "USD"
}
}
]
}
],
"payment": {
"type": {
"coding": [
{
"system": "http://payment.com",
"code": "reversed/cancelled"
}
],
"text": "cancelled"
}
}
}
My Expected Output :
{
"resourceType": "E",
"id": "11",
"identifier": [
{
"type": {
"coding": [
{
"system": "sys1",
"code": "aaa"
}
]
},
"value": "212"
},
{
"type": {
"coding": [
{
"system": "sys2",
"code": "RRR"
}
]
},
"value": "367"
}
],
"status": "active",
"created": "2021-08-05T02:43:48+00:00",
"outcome": "complete",
"item": [
{
"sequence": 1,
"product": {
"coding": [
{
"system": "example",
"code": "abc",
"display": "ABC"
}
],
"text": "ABC"
},
"servicedDate": "2021-08-04",
"quantity": {
"value": 60
},
"ADJ": [
{
"category": {
"coding": [
{
"system": "code1",
"code": "code1",
"display": "CODE1"
}
],
"text": "CODE1"
},
"amount": {
"value": 46.45,
"currency": "USD"
}
},
{
"category": {
"coding": [
{
"system": "code2",
"code": "code2",
"display": "CODE2"
}
],
"text": "CODE2"
},
"amount": {
"value": 12.04,
"currency": "USD"
}
},
{// new object I want to insert into
"category": {
"coding": [
{
"system": "sys_denail",
"code": "denialreason"
}
],
"reason": {
"coding": [
{
"system": "https://example.com",
"code": "A1"
}
],
"text": "unknown"
}}
}
]
}
],
"payment": {
"type": {
"coding": [
{
"system": "http://payment.com",
"code": "denied" //change the value to denied
}
],
"text": "denied" //change the value to denied
}
}
}
Edit : I've tried to answer the second case by myself to be evaluated after the first case is answered
Welcome to SO, please ask minimal and reproducible questions, and show your effort tried for the future.
What you need is to use a conditional logic along with placeholder values with ampersand symbols depending on the levels of each key name within the tree.
I have partially answered, which will handle the bottom part of your question. Indeed the logic for the rest(inserting an object to the array will be similiar)
So, consider having a look at the following solution
[
{
"operation": "shift",
"spec": {
"*": "&",
"payment": {
"type": {
"coding": {
"*": {
"*": "&4.&3.&2[&1].&",
"code": {
"paid": {
"#1": "&6.&5.&4[&3].&2",
"#(4,text)": "&6.text"
},
"*": {
"#denied": "&6.&5.&4[&3].code",
"#(4,text)": {
"#denied": "&6.text"
}
}
}
}
}
}
}
}
}
]
Edit(for your own answer related to adding an object):
your current idea of using shift after default transformation spec is pretty good, you can rephrase like
[
{
"operation": "default",
"spec": {
"temp_deny": {
"denialreason": {
"category": {
"coding": [
{
"system": "sys_denail",
"code": "denialreason"
}
],
"reason": {
"coding": [
{
"system": "https://example.com",
"code": "A1"
}
],
"text": "unknown"
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": "&",
"item": {
"*": {
"*": "&2[&1].&",
"ADJ": {
"#": "&3[&2].&",
"#(4,temp_deny)": "&3[&2].&"
}
}
}
}
}
]
I just started learning jolt transformations.I found this scenario. The spec also is posted, but I am trying to understand the spec, and confused with the levels of the fields using "&".
From the below spec:
I am confused with this syntax "name": "state[&3].cities[&1].cityname".
Why "name": "state[&2].cities[&1].cityname" doesn't give any output? As per my understanding the level of state for city name is &2. Can someone please try to explain why &3 is used.
Input Json:
{
"country": "usa",
"state": [
{
"stateName": "TX",
"location": "south",
"cities": [
{
"name": "Austin",
"pop": "1M"
},
{
"name": "Dallas",
"pop": "2M"
}
]
},
{
"stateName": "CA",
"location": "west",
"cities": [
{
"name": "SanFran",
"pop": "3M"
},
{
"name": "LosAngeles",
"pop": "4M"
}
]
}
]
}
Expected Output:
{
"country": "usa",
"state": [
{
"stateName": "TX",
"locatedIn": "south", // name change here
"cities": [
{
"cityname": "Austin", // name change here
"citypopulation": "1M" // name change here
},
{
"cityname": "Dallas",
"citypopulation": "2M"
}
]
},
{
"stateName": "CA",
"locatedIn": "west",
"cities": [
{
"cityname": "SanFran",
"pop": "3M"
},
{
"cityname": "LosAngeles",
"citypopulation": "4M"
}
]
}
]
}
Spec:
[
{
"operation": "shift",
"spec": {
"country": "country",
"state": {
"*": { // state array index
"stateName": "state[&1].stateName",
"location": "state[&1].location",
"cities": {
"*": { // city array index
"name": "state[&3].cities[&1].cityname",
"pop": "state[&3].cities[&1].citypopualtion"
}
}
}
}
}
}
]
{
"operation": "shift",
"spec": {
"country": "country",
"state": {
"*": { // state array index <- level 3
"stateName": "state[&1].stateName",
"location": "state[&1].location",
"cities": { <- level 2
"*": { // city array index <- level 1
"name": "state[&3].cities[&1].cityname", <- level 0
"pop": "state[&3].cities[&1].citypopualtion"
}
}
}
}
}
}
You are actually retrieving the index, that is represented by the "*". If you use &2 you are getting the cities and siblings level but you dont want to access those attributes. So during transformation it will actually end up being something like state[state_index].cities[city_index].cityname
Original JSON message:
[
{
"correlationId": "12345",
"payloadFormat": "Money",
"payload": {
"stateName": "TX",
"location": "south",
"name": "Dallas",
"pop": "2M"
}
},
{
"correlationId": "ed1e3",
"payloadFormat": "Cash",
"payload": {
"stateName": "CA",
"location": "west",
"name": "LosAngeles",
"pop": "4M"
}
}
]
Output should be in below format:
[
{
"correlationId": "12345",
"payloadFormat": "Money",
"payload": {
"California": "TX",
"MontGomery": "south",
"City": "Dallas",
"ID": "2M"
}
},
{
"correlationId": "ed1e3",
"payloadFormat": "Cash",
"payload": {
"California": "CA",
"MontGomery": "west",
"City": "LosAngeles",
"ID": "4M"
}
}
]
Check this spec,
[
{
"operation": "shift",
"spec": {
"*": {
"correlationId": "[&1].correlationId",
"payloadFormat": "[&1].payloadFormat",
"payload": {
"stateName": "[&2].payload.California",
"location": "[&2].payload.MontGomery",
"name": "[&2].payload.City",
"pop": "[&2].payload.ID"
}
}
}
}
]