Home > Enterprise >  Binary addition using JOLT
Binary addition using JOLT

Time:02-05

How do I transform below JSON using JOLT

  1. Input will be array of strings
  2. This string will be binary number , 0 or 1.

I want to perform binary addition and then split resultant array as split and every element should be a seperate property

{
  "binaryarray": [
    "0100",
    "0010"
  ]
}

Output

{
  "field1" : 0,
  "field2" : 1,
  "field3" : 1,
  "field4" : 0
}

CodePudding user response:

I was able to partially achieve it

  1. Convert array of string to array of int

  2. perform Addition of array

  3. Convert above to string

  4. Split above string

    [
       {
         "operation": "modify-overwrite-beta",
         "spec": {
           "binaryarray": ["=toInteger", 0]
         }
         },
       {
         "operation": "modify-overwrite-beta",
         "spec": {
           //
           // Sums
           "sumIntData": "=intSum(@(1,binaryarray))"
         }
       },
       {
         "operation": "modify-overwrite-beta",
         "spec": {
           "sumIntData": ["=toString", 0]
         }
         },
       {
         "operation": "modify-overwrite-beta",
         "spec": {
           "PID3": "=split('', @(1,sumIntData))"
         }
         }
     ]
    

CodePudding user response:

You can successively use split and max functions within modify transformation specs such as

[
  { //label the components of the arrays by their respective indexes(0,1) while converting them to independent attributes
    "operation": "shift",
    "spec": {
      "*": {
        "*": "&"
      }
    }
  },
  { //convert those attributes again to arrays which has splitted elements one by one
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=split('',@(1,&))"
    }
  },
  { //transpose the arrays
    "operation": "shift",
    "spec": {
      "*": {
        "*": "&"
      }
    }
  },
  { //perform binary sum through use of max function
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=max"
    }
  },
  { //rename the attributes
    "operation": "shift",
    "spec": {
      "*": "field&"
    }
  }
]

the demo on the site enter image description here

  • Related