Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

Jolt to transform muliple values in an array

avatar
Contributor

I am trying to transform a json using jolt where i am moving the array inside serviceCharacteristic to outside of service {}, eventually i plan to move to respective positions 

 

{
  "serviceOrderItem": [
    {
      "action": "modify",
      "id": "0965f63b-db26-4397-b222-190a891838f0",
      "service": {
        "href": "serviceInventory/v4/service/adaptiveNetworkSite/1b9a3fae-f50b-4310-887d-60988f8d5645",
        "id": "adaptiveNetworkSite",
        "serviceCharacteristic": [
          {
            "name": "modifyPath",
            "value": [
              {
                "op": "add",
                "path": "$.supportingService",
                "value": {}
              },
              {
                "op": "sub",
                "path": "$.supportingService",
                "value": {}
              }
            ]
          }
        ]
      }
    },
    {
      "action": "create",
      "id": "0965f63b-db26-4397-b222-190a891838f0",
      "service": {
        "href": "serviceInventory/v4/service/adaptiveNetworkSite/1b9a3fae-f50b-4310-887d-60988f8d5645",
        "id": "adaptiveNetworkSite",
        "serviceCharacteristic": [
          {
            "name": "modifyPath",
            "value": [
              {
                "op": "abcd",
                "path": "$.supportingService",
                "value": {
                  "state": "active"
                }
              },
              {
                "op": "efgh",
                "path": "$.supportingService",
                "value": {
                  "state": "active"
                }
              }
            ]
          }
        ]
      }
    }
  ]
}

 

desired output is 

 

{
  "serviceOrderItem": [
    {
      "action": "modify",
      "id": "0965f63b-db26-4397-b222-190a891838f0",
      "service": {
        "href": "serviceInventory/v4/service/adaptiveNetworkSite/1b9a3fae-f50b-4310-887d-60988f8d5645",
        "id": "adaptiveNetworkSite"
		}
        "modifyPath": [
          
              {
                "op": "add",
                "path": "$.supportingService",
                "value": {}
              },
              {
                "op": "sub",
                "path": "$.supportingService",
                "value": {}
              }
            ] 
    },
    {
      "action": "create",
      "id": "0965f63b-db26-4397-b222-190a891838f0",
      "service": {
        "href": "serviceInventory/v4/service/adaptiveNetworkSite/1b9a3fae-f50b-4310-887d-60988f8d5645",
        "id": "adaptiveNetworkSite"
		}
        "modifyPath": [          
              {
                "op": "abcd",
                "path": "$.supportingService",
                "value": {
                  "state": "active"
                }
              },
              {
                "op": "efgh",
                "path": "$.supportingService",
                "value": {
                  "state": "active"
                }
              }
            ]          
        
      }
    }
  ]
}

 

my Jolt is working only when there is single array in modifyPath

 

 

[{
  "operation": "shift",
  "spec": {
    "*": "&",
    "serviceOrderItem": {
      "*": {
        "*": "serviceOrderItem[&1].&",
        "service": {
          "*": "serviceOrderItem[&2].service.&",
          "serviceCharacteristic": {
            "*": {
              "value": "@(1,name)"
            }
          }
        }
      }
    }
  }
}, {
  "operation": "shift",
  "spec": {
    "*": "&",
    "modifyPath": {
      "@(1,modifyPath)": "serviceOrderItem[0].modifyPath"
    },
    "supportingService": {
      "@(1,supportingService)": "serviceOrderItem[0].service.supportingService"
    }
  }
}]

 

 

when multiple array is given it shuffles the array,

 

  "serviceOrderItem" : [ {
    "action" : "modify",
    "id" : "0965f63b-db26-4397-b222-190a891838f0",
    "modifyPath" : [ {
      "op" : "add",
      "path" : "$.supportingService",
      "value" : { }
    }, {
      "op" : "sub",
      "path" : "$.supportingService",
      "value" : { }
    }, [ {
      "op" : "abcd",
      "path" : "$.supportingService",
      "value" : {
        "state" : "active"
      }
    }, {
      "op" : "efgh",
      "path" : "$.supportingService",
      "value" : {
        "state" : "active"
      }
    } ] ],
    "service" : {
      "href" : "serviceInventory/v4/service/adaptiveNetworkSite/1b9a3fae-f50b-4310-887d-60988f8d5645",
      "id" : "adaptiveNetworkSite"
    }
  }, {
    "action" : "create",
    "id" : "0965f63b-db26-4397-b222-190a891838f0",
    "service" : {
      "href" : "serviceInventory/v4/service/adaptiveNetworkSite/1b9a3fae-f50b-4310-887d-60988f8d5645",
      "id" : "adaptiveNetworkSite"
    }
  } ]
}

 

 

 

 

please advise , thank you for you time

 

 

 

1 ACCEPTED SOLUTION

avatar

Hi @scoutjohn ,

I think you are over complicating the spec unless Im missing something. You can do the transformation in one shift spec if you are just looking to move the "serviceCharacteristic"  elements to the upper level where the service object remain as is, and value of  "name"  is used as parent to the "value" array.

In this case you can use the following spec:

[
  {
    "operation": "shift",
    "spec": {
      "serviceOrderItem": {
        "*": {
          "*": "serviceOrderItem[&1].&",
          "service": {
            "href": "serviceOrderItem[&2].service.href",
            "id": "serviceOrderItem[&2].service.id",
            "serviceCharacteristic": {
              "*": {
                "value": "serviceOrderItem[&4].@(1,name)"
              }
            }
          }
        }
      }
    }
  }
]

If that helps please accept solution.

Thanks

 

 

View solution in original post

2 REPLIES 2

avatar

Hi @scoutjohn ,

I think you are over complicating the spec unless Im missing something. You can do the transformation in one shift spec if you are just looking to move the "serviceCharacteristic"  elements to the upper level where the service object remain as is, and value of  "name"  is used as parent to the "value" array.

In this case you can use the following spec:

[
  {
    "operation": "shift",
    "spec": {
      "serviceOrderItem": {
        "*": {
          "*": "serviceOrderItem[&1].&",
          "service": {
            "href": "serviceOrderItem[&2].service.href",
            "id": "serviceOrderItem[&2].service.id",
            "serviceCharacteristic": {
              "*": {
                "value": "serviceOrderItem[&4].@(1,name)"
              }
            }
          }
        }
      }
    }
  }
]

If that helps please accept solution.

Thanks

 

 

avatar
Contributor

@SAMSAL  thank you, that solved it