Search code examples
javajsonjolt

how to add an entry into final list by jolt map to list


I'm trying to transform a map to list by Jolt, which is done ok the problem is that I need to add an entry as a default(not in input schema) but it doesn't work

I have a simple schema as is:

{
  "iban": "IR970130100000000000001399",
  "paymentCode": "124"
}

and except the output like this:

{
  "parameters": [
    {
      "name": "iban",
      "value": "IR970130100000000000001399"
    },
    {
      "name": "paymentCode",
      "value": "124"
    },
    {
      "name": "P_RQID",
      "value": "4f2ba174-690f-4d41-9bcc-b958789fe6d7"
    }
  ]
}

this is my spec:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "$": "parameters.[#2].name",
        "@": "parameters.[#2].value"
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      "parameters": {
        "name": "P_RQID",
        "value": "4f2ba174-690f-4d41-9bcc-b958789fe6d7"
      }
    }
  }
]

and this is the output I have:

{
  "parameters": [
    {
      "name": "iban",
      "value": "IR970130100000000000001399"
    },
    {
      "name": "paymentCode",
      "value": "124"
    }
  ]
}

without the effect of default operation. anyone can help?


Solution

  • You might change the order of the transformations while adding "parameters" case along with the else case( "*" ) to the shift transformation spec such as

    [
      {
        "operation": "default",
        "spec": {
          "parameters": {
            "name": "P_RQID",
            "value": "4f2ba174-690f-4d41-9bcc-b958789fe6d7"
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "parameters": "&",
          "*": { // non-parameters case
            "$": "parameters[#2].name",
            "@": "parameters[#2].value"
          }
        }
      }
    ]
    

    the demo on the site http://jolt-demo.appspot.com/ is :

    enter image description here