Forum Discussion
Pretpark
9 months agoNew Contributor II
Even though this is an old question, i will answer it. I encountered this problem myself, and could only solve it using a script snap. The script i used is regardless of input data (depending on the layer of where you want to count).
The script snap was set to use Python, for which this is the code:
# Import the interface required by the Script snap.
from com.snaplogic.scripting.language import ScriptHook
class TransformScript(ScriptHook):
def __init__(self, input, output, error, log):
self.input = input
self.output = output
self.error = error
self.log = log
self.all_indocs = []
# The "execute()" method is called once when the pipeline is started
# and allowed to process its inputs or just send data to its outputs.
def execute(self):
self.log.info("Executing Transform script")
counter = 0
while self.input.hasNext():
try:
# Read the next input document, store it in a new dictionary, and write this as an output document.
inDoc = self.input.next()
if 'counter' not in inDoc:
inDoc['counter'] = counter
# inDoc['downOneLevel']['downAnother'] = counter <-- use this to set an existing field to a new value, and comment the 2 lines above
self.all_indocs.append(inDoc)
counter += 1
except Exception as e:
errDoc = {
'error' : str(e)
}
self.log.error("Error in python script")
self.error.write(errDoc)
outDoc = {
'output_group': self.all_indocs
}
self.output.write(outDoc)
self.log.info("Script executed")
# The "cleanup()" method is called after the snap has exited the execute() method
def cleanup(self):
self.log.info("Cleaning up")
# The Script Snap will look for a ScriptHook object in the "hook"
# variable. The snap will then call the hook's "execute" method.
hook = TransformScript(input, output, error, log)
I placed a comment in line 25, which you can un-comment in the case you have an existing JSON key, which needs a new value.