Home > Software design >  Jolt- Based on the index need to iterate a list and form common object from the index
Jolt- Based on the index need to iterate a list and form common object from the index

Time:12-22

I have requirement of iterating a list which can have same tag name but different indexes like 0,1,2. So I need to iterate the list, take out the common indexes and from that index take out name and value tag and form as another list.

Request:

{
  "characteristic": [
    {
      "name": "BucketName",
      "value": "testName0",
      "@type": "bucketInfo",
      "arrayIndex": "0"
    },
    {
      "name": "BucketName",
      "value": "testName1",
      "@type": "bucketInfo",
      "arrayIndex": "1"
    },
    {
      "name": "BucketName",
      "value": "testName2",
      "@type": "bucketInfo",
      "arrayIndex": "2"
    },
    {
      "name": "BucketId",
      "value": "testId0",
      "@type": "bucketInfo",
      "arrayIndex": "0"
    },
    {
      "name": "BucketId",
      "value": "testId1",
      "@type": "bucketInfo",
      "arrayIndex": "1"
    },
    {
      "name": "BucketId",
      "value": "testId2",
      "@type": "bucketInfo",
      "arrayIndex": "2"
    }
  ]
}

Response Required:

{
  "bucketList": [
    {
      "BucketName": "testName0",
      "BucketId": "testId0"
    },
    {
      "BucketName": "testName1",
      "BucketId": "testId1"
    },
    {
      "BucketName": "testName2",
      "BucketId": "testId2"
    }
  ]
}

How could we achieve this based on the index from alist?

When More elements are there how to handle this case to skip if values not coming and add only tags that are coming. Example Request:

{
  "characteristic": [
    {
      "name": "BucketName",
      "value": "testName0",
      "@type": "bucketInfo",
      "arrayIndex": "0"
    },
    {
      "name": "BucketId",
      "value": "testId0",
      "@type": "bucketInfo",
      "arrayIndex": "0"
    },
    {
      "name": "BucketType",
      "value": "testType1",
      "@type": "bucketInfo",
      "arrayIndex": "1"
    },
    {
      "name": "BucketId",
      "value": "testId1",
      "@type": "bucketInfo",
      "arrayIndex": "1"
    },
    {
      "name": "BucketName",
      "value": "testName2",
      "@type": "bucketInfo",
      "arrayIndex": "2"
    },
    {
      "name": "BucketId",
      "value": "testId2",
      "@type": "bucketInfo",
      "arrayIndex": "2"
    },
    {
      "name": "BucketId",
      "value": "testId3",
      "@type": "bucketInfo",
      "arrayIndex": "3"
    },
    {
      "name": "BucketName",
      "value": "testName3",
      "@type": "bucketInfo",
      "arrayIndex": "3"
    },
    {
      "name": "BucketData",
      "value": "testData3",
      "@type": "bucketInfo",
      "arrayIndex": "3"
    }
  ]
}

Response expected:

{
    "bucketlist": [
        {
            "BucketName": "testName0",
            "BucketId": "testId0"
        },
        {
            "BucketType": "testType1",
            "BucketId": "testId1"
        },
        {
            "BucketName": "testName2",
            "BucketId": "testId2"
        },
        {
            "BucketName": "testName3",
            "BucketId": "testId3",
            "BucketData": "testData3"
        }
    ]
}

CodePudding user response:

You can apply two successive shift transformations. Take "@(2,arrayIndex)" as a common factor in order to combine the elements under the commonly tagged arrays in the first transformation, and then display them as desired within the second one such as

[
  {
    "operation": "shift",
    "spec": {
      "characteristic": {
        "*": {
          "value": {
            "@(1,value)": "@(2,arrayIndex).@(2,name)"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "bucketList[]"
    }
  }
]

enter image description here

  • Related