cancel
Showing results for 
Search instead for 
Did you mean: 

Script Snap Data Structures

jskrable
New Contributor III

Hello,

I have written a script to create a JSON object with counters of a parameterized length, with the desired structure like this:

[
{
“counter”: 1
},
{
“counter”: 2
},
{
“counter”: 3
},

]

However, when I do this through a loop in a script snap, it puts the result in an extraneous array, like this:

[
[
{
“counter”: 1
},
{
“counter”: 2
},
{
“counter”: 3
},

]
]

Can anyone let me know why this is happening? I can’t get rid of the extra array using a structure or mapper snap either. My script looks like this:

def execute(self):
    self.log.info("Executing Transform script")
    list = []
    for i in range(1,277):
        entry = {}
        entry["counter"] = i
        list.append(entry)
        
    output = list
    
    try:
        # Read the next document, wrap it in a map and write out the wrapper
        self.output.write(list)
    except Exception as e:
        errWrapper = {
            'errMsg' : str(e.args)
        }
        self.log.error("Error in python script")
        self.error.write(errWrapper)

    self.log.info("Finished executing the Transform script")

Has anyone run into something like this before?

Thanks,
Jack

2 REPLIES 2

aleung
Contributor III

If that is really all you trying to do. You could achieve what you wanted with just the “Sequence” snap and it would create the structure you desire.

jskrable
New Contributor III

I wasn’t aware of that snap, thanks for bringing it to my attention.