Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Within Designer, create a new pipeline.
  2. Add and configure a JSON Generator Snap.
    • Click Edit JSON and add the following sample data:

      Code Block
      {
          "Key": "001",
          "Month01": 151,
          "Month02": 152,
          "Month03": 153,
          "Month04": 154,
          "Month05": 155,
          "Month06": 156,
          "Month07": 157,
          "Month08": 158,
          "Month09": 159,
          "Month10": 160,
          "Month11": 161,
          "Month12": 162
      }


  3. Add and configure a Script Snap.
    • Set Scripting language to Python.

    • Select Execute during preview.

    • Click Edit Script and add the following sample script:
       

      Code Block
      from com.snaplogic.scripting.language import ScriptHook
      from com.snaplogic.scripting.language.ScriptHook import *
       
      class TransformScript(ScriptHook):
          def __init__(self, input, output, error, log):
              self.input = input
              self.output = output
              self.error = error
              self.log = log
       
          def execute(self):
              self.log.info("Executing Transform script")
              i = 1
              while self.input.hasNext():
                  data = self.input.next()
                  dataoutkey = data['Key']
                  for x in range(1, 13):
                      dataout = {}
                      monthkey = 'Month' + str(x).zfill(2)
                      dataout['key'] = dataoutkey
                      dataout['monthkey'] = monthkey
                      dataout['value'] = data[monthkey]
                      self.output.write(dataout)
                      x = x + 1
              self.log.info("Finished executing the Transform script")
      
       hook = TransformScript(input, output, error, log)


...