What I'm looking to do is take the fulfillmentId for each order, get the corresponding object in the fulfillments array and add that to the order object in place of the fulfillmentId.
Input:
{
"orders": [
{
"orderNo": "12345",
"orderType": "online",
"fulfillmentId": "123"
},
{
"orderNo": "54321",
"orderType": "in store",
"fulfillmentId": "987"
}
],
"fulfillments": [
{
"fulfillmentId": "123",
"address": "123 fake street",
"type": "delivery"
},
{
"fulfillmentId": "987",
"address": "321 main street",
"type": "pickup"
}
]
}
output that I'm looking for
{
"orders": [
{
"orderNo": "12345",
"orderType": "online",
"fulfillment": {
"fulfillmentId": "123",
"address": "123 fake street",
"type": "delivery"
}
},
{
"orderNo": "54321",
"orderType": "in store",
"fulfillment": {
"fulfillmentId": "987",
"address": "321 main street",
"type": "pickup"
}
}
]
}
I haven't been able to work playing around with the jolt transform demo. I'm not 100% sure of the viability of this with the built in jolt functionality but think it should be possible.
Here is what I have so far, without having touched the fulfillment
[
{
"operation": "shift",
"spec": {
"orders": {
"*": {
"orderNo": "orders[&1].orderNo",
"orderType": "orders[&1].orderType"
}
}
}
}
]
Any help would be greatly appreciated.
You can use the following transformation :
[
{ // generate two new objects which replicates of the objects
// of "fulfillments" array with "fulfillmentId" valued keys
"operation": "shift",
"spec": {
"*s": { // represents the array "fulfillments" which ends with "-s"
"@": "&", // replicate all elements within the input JSON
"*": {
"*": "@1,&(2,1)Id.&"
}
}
}
},
{
"operation": "shift",
"spec": {
"orders": {
"@": "&", // replicate "orders" array
"*": {
"*Id": { // represents "fulfillmentId" which ends with "-Id"
"*": {
"@4,&": { // go 4 levels up the tree to reach the level of
// "orders" and bring previously generated objects inside
"*": "&5[&4].&(3,1).&" // &5 stands for the literal "order"
// [&4] for indexes of the "order" array to produce arraywise results
// &(3,1) represents going 3 levels up the tree and pick
// 1st asterisk replacement there
// & is the leaf node which replicates values of the attributes respectively
}
}
}
}
}
}
},
{ // get rid of the extra "fulfillmentId" attribute(outer ones)
"operation": "remove",
"spec": {
"*": {
"*": {
"fulfillmentId": ""
}
}
}
}
]
the demo on the site https://jolt-demo.appspot.com/ is :