Search code examples
jsonjolt

Jolt spect improve


as I new in jolt and next to the previous question I'm trying to add some more steps.

again from the input as this

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

I want to have such output

{
  "parameters": [
    {
      "name": "P_IBAN",
      "value": "IR970130100000000000001399"
    },
    {
      "name": "P_PAYMENTCODE",
      "value": "124"
    },
    {
      "name": "P_RQID",
      "value": "4f2ba174-690f-4d41-9bcc-b958789fe6d7"
    },
    {
      "name": "P_TYPEX",
      "value": "1"
    },
    {
      "name": "P_BIC",
      "value": "1"
    }
  ],
  "requestID": "RequestID",
  "callType": "Reader",
  "encoding": "ASCII"
}

the steps are as:

  1. add multi entry in default spec
  2. add P_ prefix to "name" of input (transform to p_name)
  3. transform p_name to uppercase

this is the spec I have

[
  {
    "operation": "default",
    "spec": {
      "parameters": {        
        "name": "P_RQUD",
        "value": "4f2ba174-690f-4d41-9bcc-b958789fe6d7"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "parameters": "&",
      "*": {
        "$": "parameters[#2].name",
        "@": "parameters[#2].value"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "parameters": {
        "*": {
          "name": "=toUpper"
        }
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      "callType": "Reader",
      "encoding": "ASCII",
      "requestID": "RequestID"
    }
  }
]

Solution

  • You can convert the first "parameter" node to an array key while getting rid of the last default transformation spec, as enough to use # prefixes on the LHS to generate fixed key-value pairs within a shift transformation spec such as

    [
      {
        "operation": "default",
        "spec": {
          "parameters": [
            {
              "name": "RQUD",
              "value": "4f2ba174-690f-4d41-9bcc-b958789fe6d7"
            },
            {
              "name": "TYPEX",
              "value": "1"
            },
            {
              "name": "BIC",
              "value": "1"
            }
         ]
        }
      },
      {
        "operation": "shift",
        "spec": {
          "parameters": { "*": "&1" }, //&1 copies the literal value "parameters" after traversing 1 level above
          "*": {
            "$": "parameters[#2].name",
            "@": "parameters[#2].value"
          },
          "#RequestID": "requestID",
          "#Reader": "callType",
          "#ASCII": "encoding"
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "parameters": {
            "*": {
              "n": "=toUpper(@(1,name))",
              "name": "=concat('P_',@(1,n))"
            }
          }
        }
      },
      { // get rid of the newly generated extra attribute
        "operation": "remove",
        "spec": {
          "parameters": {
            "*": {
              "n": ""
            }
          }
        }
      },
      { // sorts the elements alphabetically as default
        "operation": "sort"
      }
    ]
    

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

    enter image description here