Merging two arrays by dynamically matching values using jolt - json

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].&"
}
}
}
}
}
}
}
}
]

Related

Conditional Jolt spec for nested array

I am trying to write Jolt spec for the following input. I need to populate the primaryEmail field based on the condition if primary field is true in the emails array
[
{
"uid": "1234mark",
"name": "mark",
"userName": "markw",
"displayName": "Mark W",
"emails": [
{
"primary": false,
"value": "mark#gmail.com"
},
{
"primary": true,
"value": "mark#hotmail.com"
}
]
},
{
"uid": "9876steve",
"name": "steve",
"userName": "stevew",
"displayName": "Steve W",
"emails": [
{
"primary": false,
"value": "steve#gmail.com"
},
{
"primary": true,
"value": "steve#hotmail.com"
}
]
}
]
The desired output is
[
{
"user": {
"externalId": "1234mark",
"name": "mark",
"userName": "markw",
"displayName": "Mark W",
"primaryEmail": "mark#hotmail.com"
}
},
{
"user": {
"externalId": "9876steve",
"name": "steve",
"userName": "stevew",
"displayName": "Steve W",
"primaryEmail": "steve#hotmail.com"
}
}
]
But I get the following incorrect output since I am not able to populate the primaryEmail field conditionally properly.
[
{
"user": {
"externalId": "1234mark",
"name": "mark",
"userName": "markw",
"displayName": "Mark W"
}
},
{
"user": {
"externalId": "9876steve",
"name": "steve",
"userName": "stevew",
"displayName": "Steve W"
}
}
]
The spec I have created is the following
[
{
"operation": "shift",
"spec": {
"*": {
"uid": "[&1].user.externalId",
"name": "[&1].user.name",
"userName": "[&1].user.userName",
"displayName": "[&1].user.displayName",
"title": "[&1].user.title",
"emails": {
"*": {
"primary": {
"true": {
"#(2,value)": "primaryEmail"
}
}
}
}
}
}
}
]
Could someone please help with this query. Thanks.
What you need is to go 5 levels the three up from the innermost object while adding an extra node called user such as
[
{
"operation": "shift",
"spec": {
"*": {
"uid": "[&1].user.externalId",
"*": "[&1].user.&", // the attributes except for "uid" and "emails" array
"emails": {
"*": {
"primary": {
"true": {
"#(2,value)": "[&5].user.&2Email" // replicate literal "primary" by using &2
}
}
}
}
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is

JOLT Transformation split objects from JSON with common values

I have an array of objects and I want to split each key from the object into a separate array in JSON.
Input JSON:
[
{
"Company": "Tesla",
"Age": 30.2
},
{
"Company": "Facebook",
"Age": 40.5
},
{
"Company": "Amazon",
"Age": 60
}
]
Expected Output
[
{
"value": "Tesla",
"variable": "Company",
"model": "device"
},
{
"value": "Facebook",
"variable": "Company",
"model": "device"
},
{
"value": "Amazon",
"variable": "Company",
"model": "device"
},
{
"value": 30.2,
"variable": "Age",
"model": "device"
},
{
"value": 40.5,
"variable": "Age",
"model": "device"
},
{
"value": 60,
"variable": "Age",
"model": "device"
}
]
Here the value of the Model is fixed. I have tried with some jolt spec. It's not working.
Any help would be much appreciated!
You can use shift transformation spec such as
[
{
// distinguish the attributes by indexes
"operation": "shift",
"spec": {
"*": {
"*": {
"#": "&1[&2].value", // # represents values of the keys inherited from one upper level, &1 represents keys "Company" or "Age", [&2] represents going tree two levels up to grab the outermost indexes in an array fashion
"$": "&1[&2].variable", // $ represents values of the keys inherited from one upper level
"#device": "&1[&2].model"
}
}
}
},
{
// get rid of object/array labels
"operation": "shift",
"spec": {
"*": {
"*": ""
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is
Edit : If the input was
[
{
"Company": "Tesla",
"Age": 30.2,
"Time": "27/10/1992"
},
{
"Company": "Facebook",
"Age": 40.5,
"Time": "27/10/1982"
}
]
as mentioned in the comment, then you could convert the spec to
[
{
"operation": "shift",
"spec": {
"*": {
"Company|Age": {
"#": "&1[&2].value",
"$": "&1[&2].variable",
"#(1,Time)": "&1[&2].DateofBirth",
"#device": "&1[&2].model"
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": ""
}
}
}
]
in order to get the following desired output :
[
{
"value": "Tesla",
"variable": "Company",
"DateofBirth": "27/10/1992",
"model": "device"
},
{
"value": "Facebook",
"variable": "Company",
"DateofBirth": "27/10/1982",
"model": "device"
},
{
"value": 30.2,
"variable": "Age",
"DateofBirth": "27/10/1992",
"model": "device"
},
{
"value": 40.5,
"variable": "Age",
"DateofBirth": "27/10/1982",
"model": "device"
}
]

How to change a name of more fields in Json array using Jolt

I am trying to convert the input JSON:
for example , incoming json data structure is
{
"12346565": [
{
"type": "13",
"value": "3",
"score": "5"
},
{
"type": "45",
"value": "1",
"score": "5"
}
],
"12346777": [
{
"type": "41",
"value": "10",
"score": "3"
}
]
}
The expected output is:
{
"12346565": [
{
"model": "13",
"v": "3"
},
{
"model": "45",
"v": "1"
}
],
"12346777": [
{
"model": "41",
"v": "10"
}
]
}
change:
type -> model;
value -> v
remove:
score
Any ideas?
You can use modify and remove transformation specs consecutively such as
[
{
// generate new pairs with values copied from the former attributes
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"*": {
"model": "#(1,type)",
"v": "#(1,value)"
}
}
}
},
{
// get rid of undesired attributes
"operation": "remove",
"spec": {
"*": {
"*": {
"type": "",
"value": "",
"score": ""
}
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is

Jolt Conversion - iterate list within a list and form single list

I am trying to iterate lists inside a list and form a single list with multiple objects. Iterating lists, I am able to achieve. But applying tags before the list iterating to each object is not happening if there are more objects in single list.
My input request is like below:
[
{
"success": [
{
"id": "4",
"Offers": [
{
"name": "Optional",
"type": {
"id": "1",
"name": "Optional"
},
"productOfferings": [
{
"id": "3",
"name": "Test1"
}
]
},
{
"name": "Default",
"type": {
"id": "2",
"name": "Default"
},
"productOfferings": [
{
"id": "1",
"name": "Test2"
},
{
"id": "2",
"name": "Test3"
}
]
}
]
}
]
}
]
My spec is like below:
[
{
"operation": "shift",
"spec": {
"*": {
"success": {
"*": {
"Offers": {
"*": {
"name": "[&1].[&3].typeName",
"type": {
"id": "[&2].[&4].typeNameId",
"name": "[&2].[&4].typeNameValue"
},
"productOfferings": {
"*": {
"id": "[&3].[&1].id",
"name": "[&3].[&1].name"
}
}
}
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": "[]"
}
}
}
]
Output Received from spec:
[
{
"typeName": "Optional",
"typeNameId": "1",
"typeNameValue": "Optional",
"id": "3",
"name": "Test1"
},
{
"typeName": "Default",
"typeNameId": "2",
"typeNameValue": "Default",
"id": "1",
"name": "Test2"
},
{
"id": "2",
"name": "Test3"
}
]
But Expected output is like below:
[
{
"typeName": "Optional",
"typeNameId": "1",
"typeNameValue": "Optional",
"id": "3",
"name": "Test1"
},
{
"typeName": "Default",
"typeNameId": "2",
"typeNameValue": "Default",
"id": "1",
"name": "Test2"
},
{
"typeName": "Default",
"typeNameId": "2",
"typeNameValue": "Default",
"id": "2",
"name": "Test3"
}
]
If there are more objects inside productOfferings object, I am not able to add typeName,typeNameId, typeNameValue to the actual object. Please help to fix this issue.
You seem just needing to collect all into the productOfferings array while prepending each key by the common identifier [&3].[&1] such as
[
{
"operation": "shift",
"spec": {
"*": {
"success": {
"*": {
"Offers": {
"*": {
"productOfferings": {
"*": {
"#(2,name)": "[&3].[&1].typeName",
"#(2,type.id)": "[&3].[&1].typeNameId",
"#(2,type.name)": "[&3].[&1].typeNameValue",
"*": "[&3].[&1].&"
}
}
}
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": "[]"
}
}
}
]

jolt - copy or move a key from nested object to the top level

I'm looking for a way to copy or move a key from nested object to the top level
Input:
{
"id": "123",
"name": "foo",
"details": {
"orderNumber": "456789",
"addr": "N st 124",
"date": "2021-01-01"
}
}
desired output:
{
"id": "123",
"name": "foo",
"orderNumber": "456789",
"details": {
"orderNumber": "456789",
"addr": "N st 124",
"date": "2021-01-01"
}
}
or ideally
{
"id": "123",
"name": "foo",
"orderNumber": "456789",
"details": {
"addr": "N st 124",
"date": "2021-01-01"
}
}
the closest I could get is below transformation, but it converts object to value array
[
{
"operation": "shift",
"spec": {
"id": "id",
"name": "name",
"details": {
"orderNumber": "orderNumber",
"*": "details"
}
}
}
]
You're so close to the result, just a slight change(adding an ampersand) is needed such as
[
{
"operation": "shift",
"spec": {
"id": "id",
"name": "name",
"details": {
"orderNumber": "orderNumber",
"*": "&1.&"
}
}
}
]
in this case the keys keeps on appearing.