Validating and transform the array objects in JOLT - json

Would like to have the output JSON based on the active status in the input array.If active is true provide the value object.
INPUT :
{
"services": [
{
"active": true,
"value": "Clampable",
"key": "40300"
},
{
"active": false,
"value": "Mixed load",
"key": "40302"
}
]
}
SPECS:
[
{
"operation": "shift",
"spec": {
"services": {
"*": {
"key": {
"40302": {
"#mixed": "loading_method"
},
"40300": {
"#clampable": "loading_method"
}
}
}
}
}
}
]
OUTPUT :
"loading_method" : [ "clampable", "mixed"]
I do not want the mixed value as output as the status is false.
Any advise would be great..

This works,
If active is true then, shift the value node.
[
{
"operation": "shift",
"spec": {
"services": {
"*": {
"active": {
"true": {
"#(2,value)": "loading_method"
}
}
}
}
}
}
]

Related

Combine shifted values and default values into a same sub object using Jolt

Input:
{
"banking_account": {
"accounts": [
{
"accountId": "id1"
},
{
"accountId": "id2"
}
]
}
}
Expected output:
{
"Data": {
"Accounts": [
{
"Account": {
"Attribute1": "default",
"Identification": "id1"
}
},
{
"Account": {
"Attribute1": "default",
"Identification": "id2"
}
}
]
}
}
My current spec:
[
{
"operation": "shift",
"spec": {
"banking_account": {
"accounts": {
"*": {
"accountId": "Data.Accounts.[&1].Account.Identification"
}
}
}
}
},
{
"operation": "default",
"spec": {
"Attribute": "default"
}
},
{
"operation": "shift",
"spec": {
"Data": {
"Accounts": {
"*": {
"*": "Data.Accounts.[&1].&",
"#(3,Attribute)": "Data.Accounts.[&1].Account.Attribute1"
}
}
}
}
}
]
Current output:
{
"Data": {
"Accounts": [
{
"Account": [
{
"Attribute1": "default"
},
{
"Identification": "id1"
}
]
},
{
"Account": [
{
"Attribute1": "default"
},
{
"Identification": "id2"
}
]
}
]
}
}
It seems instead of inserting the key-value pair into the existing "Account" sub-object, it's making "Account" as a list, with one sub-object containing "Identification", and another object containing "Attribute1".
Could you help me understand why this happens, and how could I configure to avoid this?
Thanks a lot for your help in advance!
Just prefixing the desired fixed value(default) with a # symbol would handle along with using a single shift transformation spec as in the following
[
{
"operation": "shift",
"spec": {
"banking_account": {
"a*s": {
"*": {
"#default": "Data.A&(2,1)s[&1].A&(2,1).Attribute1", // "&(2,1)" represents going tree two levels up and grabbing the second piece(with index 1 which's after 0) from the literal(`accounts`) splitted by asterisk symbols
"accountId": "Data.A&(2,1)s[&1].A&(2,1).Identification"
}
}
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is

JOLT Specification to Transform field value to field name

I am trying to convert below json payload into a json payload which has field name as the value of the field:
Can you please help to provide jold specification for this ?
Input JSON Payload :
{
"action": {
"allowPartialSuccess": false,
"records": [
{
"recordid": "a4c6364e-4446-47d0-b014-c20ca014fdb3",
"ShipToCustomerTextualAddress__c": "TestAddress1",
"ResellerPO__c": "TestAddress1",
"InvoiceCDBID__c": "TestAddress1"
},
{
"recordid": "73781a94-9660-4f69-9bde-f2bf1991317d",
"ShipToCustomerTextualAddress__c": "TestAddress2",
"ResellerPO__c": "TestAddress2",
"InvoiceCDBID__c": "TestAddress2"
}
],
"type": "update"
}
}
Desired Output Payload :
{
"action": {
"allowPartialSuccess": false,
"records": {
"a4c6364e-4446-47d0-b014-c20ca014fdb3": {
"ShipToCustomerTextualAddress__c": "TestAddress1",
"ResellerPO__c": "TestAddress1",
"InvoiceCDBID__c": "TestAddress1"
},
"73781a94-9660-4f69-9bde-f2bf1991317d": {
"ShipToCustomerTextualAddress__c": "TestAddress2",
"ResellerPO__c": "TestAddress2",
"InvoiceCDBID__c": "TestAddress2"
}
},
"type": "update"
}
}
This will help you resolve it.
#(level,attribute) -->does the work.
[
{
"operation": "shift",
"spec": {
"action": {
"*": "action.&",
"records": {
"*": {
"ShipToCustomerTextualAddress__c": "action.records.#(1,recordid).&",
"ResellerPO__c": "action.records.#(1,recordid).&",
"InvoiceCDBID__c": "action.records.#(1,recordid).&"
}
}
}
}
}
]
You can think symbolically through use of ampersands to replicate the respective values within a shift transformation, and a remove transformation in order to get rid of the undesired attribute(recordid) at the end such as
[
{
"operation": "shift",
"spec": {
"*": {
"records": {
"*": "&2.&1.#(0,recordid)" // &2(going two levels up) stands for replicating "action", &1 is for "records"
},
"*": "&1.&" // which represents the "else" case(eg. all but the "records" object); &1 is for "action", and & is for "records"
}
}
},
{
"operation": "remove",
"spec": {
"*": {
"*": {
"*": {
"recordid": ""
}
}
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is
You would use this for solution this problem.
[
{
"operation": "shift",
"spec": {
"action": {
"*": "action.&",
"records": {
"*": "action.records.#recordid"
}
}
}
}
]

Conditional replacement of values in JOLT

I try to replace a value conditionally in JOLT. The first step is to check whether the key "links" exists. If this key exists, it should also be checked whether the key "type" has the value "null". If both conditions are true, the value of the key "type" shall be changed to "buttons".
In the example, this condition only applies to flow1.
Sample Input:
{
"flows": {
"flow1": { // <--- Only this object fulfills both conditions ("link" exist, "type" = null)
"link": "linkname",
"type": "null" // <--- This value is to be replaced
},
"flow2": {
"link": "linkname",
"type": "text"
},
"flow3": {
"type": "null"
}
}
}
Expected Output:
{
"flows": {
"flow1": {
"link": "linkname",
"type": "button" // <-- replaced value
},
"flow2": {
"link": "linkname",
"type": "text"
},
"flow3": {
"type": "null"
}
}
}
My Spec:
[
{
"operation": "shift",
"spec": {
"flows": {
"*": {
"link": {
"#(1,type)": {
"null": {
"#button": "&5.&4.type"
}
},
"*": "&3.&2.&"
},
"*": "&2.&1.&"
}
}
}
}
]
You're on the right track. Just apply some little tweaks within the current shift transformation, and then apply cardinality transformation in order to pick the first components of the generated arrays for the type attribute such as
[
{
"operation": "shift",
"spec": {
"flows": {
"*": {
"link": {
"#(1,type)": {
"null": {
"#button": "&5.&4.type"
},
"*": {
"#(3,type)": "&5.&4.type"
}
},
"#": "&3.&2.&"
},
"*": "&2.&1.&"
}
}
}
},
{
"operation": "cardinality",
"spec": {
"*": {
"*": {
"type": "ONE"
}
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is

JOLT transform based on match condition

I am trying to transform JSON to JSON using JOLT based on match condition.
If 'type': 'true', do the processing.
JSON input:
{
"features": [
{
"type": "true",
"properties": {
"class_lvl": "U",
"image_url": [
"http://www.google.com/149231_294002.jpg",
"https://www.google.com/149231_294002.jpg"
],
"review_date": "2019-03-27T15:42:02.523"
}
}
]
}
The JOLT specs I came up with but it is not generating out put the way I want:
[
{
"operation": "shift",
"spec": {
"features": {
"*": {
"properties": {
//go up one level and check if type = true then copy image URL
"#(1,type)": {
"true": {
"image_url": "Parent[&3].child.grandchild"
}
}
}
}
}
}
}
]
This Spec works:
[
{
"operation": "shift",
"spec": {
"features": {
"*": {
//"type": "Parent[#].child.grandchild.type",
"properties": {
//go up one level and check if type = true then copy image URL
"#(1,type)": {
"true": {
"#2": "Parent[#].child.grandchild"
}
}
}
}
}
}
}
]

Find element by id in json

need to transform based on the element value
input.json
{
"Result": {
"owner": {
"name": "test user"
},
"Components": [
{
"id": "123-456-789"
}
],
"123-456-789": {
"temp": {
"emip": "abc",
"teto": "123"
}
}
}
}
transform json
[
{
"operation": "shift",
"spec": {
"Result": {
"Components": {
"*": {
"id": "compId"
}
},
"compId": {
"#": "component"
}
}
}
},
{
"operation": "default",
"spec": {
"compId": null
}
}
]
output expected to be
"123-456-789": {
"temp": {
"emip": "abc",
"teto": "123"
}
}
but the result answer is below , when I hard code the value with 123-456-789 then I get the value but I need to take the value dynamically.
{
"compId" : "123-456-789"
}
Spec
[
{
"operation": "shift",
"spec": {
"Result": {
"Components": {
"*": { // components array index
"id": {
"*": { // match any value of id
// go back up the tree 5 levels, and then
// lookup a key based on the value of the "id"
// and write that to the output at at that same
// key
"#(4,&)": "&1"
}
}
}
}
}
}
}
]