Search code examples
javajsonxslttransformationjolt

How to transform list with in a list using jolt library


I am trying to transform a JSON into another using Jolt library and not getting the desired output.

Here is my input file.


{
  "maxResults": 150,
  "total": 89,
  "issues": [
    {
      "key": "1",
      "fields": {
        "fixVersions": [
          {
            "self": "FIX1-01",
            "id": "11"
          },
          {
            "self": "FIX1-02",
            "id": "12"
          }
        ]
      }
    },
    {
      "key": "2",
      "fields": {
        "fixVersions": [
          {
            "self": "FIX2-01",
            "id": "21"
          }
        ]
      }
    },
    {
      "key": "3",
      "fields": {
        "fixVersions": []
      }
    }
  ]
}

and this is the spec file I am using for transformation.


[
  {
    "operation": "shift",
    "spec": {
      "issues": {
        "*": {
          "key": "[&1].id",
          "fields": {
            "fixVersions": {
              "*": {
                "self": "[&1].fixVersion.name"
              }
            }
          }
        }
      }
    }
  }
]

And I am getting this output

[
  {
    "id": "1",
    "fixVersion": {
      "name": [
        "FIX1-01",
        "FIX2-01"
      ]
    }
  },
  {
    "fixVersion": {
      "name": "FIX1-02"
    },
    "id": "2"
  },
  {
    "id": "3"
  }
]

Its not right. What it is doing is it is getting first self field value of each issue and populating it as a array in first fixVersions of the output and getting second self field value and populating it in the second fixVersions. What I want to do is keep the structure as it is in input just change self field name like this.

[
  {
    "id": "1",
    "fixVersion": [
      {
        "name": "FIX1-01"
      },
      {
        "name": "FIX1-02"
      }
    ]
  },
  {
    "fixVersion": [
      {
        "name": "FIX2-01"
      }
    ],
    "id": "2"
  },
  {
    "id": "3"
  }
]

What am I doing wrong?


Solution

  • See if this spec is what you are looking for:

    [
      {
        "operation": "shift",
        "spec": {
          "issues": {
            "*": {
              "key": "[&1].id",
              "fields": {
                "fixVersions": {
                  "*": {
                    "self": "[&4].fixVersion[&1].name"
                  }
                }
              }
            }
          }
        }
      }
    ]
    

    Output using it against your input would result into:

    [
      {
        "id": "1",
        "fixVersion": [
          {
            "name": "FIX1-01"
          },
          {
            "name": "FIX1-02"
          }
        ]
      },
      {
        "id": "2",
        "fixVersion": [
          {
            "name": "FIX2-01"
          }
        ]
      },
      {
        "id": "3"
      }
    ]
    

    Please note the change on the indexes you are using to rebuild the fixVersion (needs to take into account whe issues index as well) list, you were missing this:

    [&4].fixVersion[&1].name