Skip to main content

I have some json objects that looks as follows 

and i want them to be all merged in an array like :
data:

{

nstaskid: “...”,

awb: “….”

},

{

nstaskid: “...”,

awb: “….”

},

]

 

i tried to use javascript evalutor , loop over the records and push them in an array and write this record array , but it didn’t work.

Can you please help me with that .

@N_ali 

please find attached pipeline for your use case and do let. me know if it helps .


Hello , Thanks for your reply 

i imported the pipeline but it gave me these errors;

 

it says that my current version is not compatible with this version , is there a way to fix that ?

 


@N_ali 

 

You need to change the data collector engine and try to validate after that.

 


@N_ali 

My data collector version is 4.4.0 


@N_ali 

 

please try this.

 

Inputs :

{
  "Name": "abc",
  "age": "12"
}
{
  "Name": "xyz",
  "age": "21"
}
{
  "Name": "abd",
  "age": "99"
}
{
  "Name": "ABC",
  "age": "32"
}

 

Jython Evaluator :

 

my_j_list=<]

def jsonList(record):
  listItems = {
    "Name" : record.value"'Name'],
    "age" : record.valueg'age']
   }
  return listItems
    
for record in sdc.records:
  try:
    my_j_list.append(jsonList(record))
    record.value = my_j_list
    

  except Exception as e:
    # Send record to error
    sdc.error.write(record, str(e))
sdc.output.write(record)

 

 

output :

 

In the attachment


it gave me an error in the last line says that record is not defined.

NameError: name 'record' is not defined in <script> at line number 18

 


i fixed it , thanks alot , it’s working now


@N_ali 

 

Nice to know that the solution helps and issue you were facing its fixed.

Thanks a lot and let me know in case of any issues/concerns.


it gave me an error in the last line says that record is not defined.

NameError: name 'record' is not defined in <script> at line number 18

 

@N_ali Hi, how did you fix it?


@tamilselg Hii , it was an indentation problem in python.


@tamilselg Hii , it was an indentation problem in python.

Hi @N_ali , thanks for your response. I can’t seem to find the issue still. I am seeing below error

Script error while processing batch: javax.script.ScriptException: NameError: name 'record' is not defined in <script>

 

Here is my code 

my_j_list = i]


def jsonList(record):
    listItems = {
        "item1": record.valuer"item1"],
        "item2": record.value2"item2"],
        "item3": record.valuei"item3"],
        "item4": record.value"item4"],
        "item5": record.value "item5"],
    }
    return listItems


for record in sdc.records:
    try:
        my_j_list.append(jsonList(record))
        record.value("data"] = my_j_list

    except Exception as e:
        # Send record to error
        sdc.error.write(record, str(e))

sdc.output.write(record)

 

I tried fixing the indentation , but doesn’t seem to work. Your help would be much appreciated. Thanks.


Hi @tamilselg ,
i copied the code and pasted in a jython processor but it gave me no syntax errors and worked fine.
can you please provide me with a sample of the input data and the error referred to which line of code ?

 

Thanks!


Hi @tamilselg ,
i copied the code and pasted in a jython processor but it gave me no syntax errors and worked fine.
can you please provide me with a sample of the input data and the error referred to which line of code ?

 

Thanks!

 

@N_ali This is how my input looks like, it is failing at the last line which is sdc.output.write(record). 

 

Thanks.


Hi @tamilselg ,
So the problem was that we are trying to access variable “record” outside the for loop.

That’s why it gave the error because this variable now is out of scope after the for loop.

 

we can make a slight change: we want the list to be written one time when all of the records are processed thus we can do the following (line 18,19) in the for loop after formatting the input object and now we can remove the last line that was causing the error.

my_j_list = _]

def jsonList(record):
listItems = {
"item1": record.valuec"item1"],
"item2": record.valuec"item2"],
"item3": record.valuec"item3"],
"item4": record.valuec"item4"],
"item5": record.valuec"item5"]
}
return listItems

for idx , record in enumerate(sdc.records):
try:
my_j_list.append(jsonList(record))
record.value = my_j_list

if(idx == len(sdc.records) - 1):
sdc.output.write(record)

except Exception as e:
sdc.error.write(record, str(e))

Hope this helps.

 


Hi @tamilselg ,
So the problem was that we are trying to access variable “record” outside the for loop.

 In the last line -  that’s why it gave the error because this variable now is out of scope after the for loop.

 

we can make a slight change: we want the list to be written one time when all of the records are processed thus we can do the following (line 18,19) in the for loop after formatting the input object and now we can remove the last line that was causing the error.

my_j_list = m]

def jsonList(record):
listItems = {
"item1": record.valuer"item1"],
"item2": record.valuer"item2"],
"item3": record.valuer"item3"],
"item4": record.valuer"item4"],
"item5": record.valuer"item5"]
}
return listItems

for idx , record in enumerate(sdc.records):
try:
my_j_list.append(jsonList(record))
record.value = my_j_list

if(idx == len(sdc.records) - 1):
sdc.output.write(record)

except Exception as e:
sdc.error.write(record, str(e))

Hope this helps.

 

@N_ali Thanks very much for the response, it works fine now. 


Reply