Question

aggregate the records into a single array of objects

  • 31 July 2022
  • 8 replies
  • 70 views

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 .


8 replies

Userlevel 2
Badge +1

@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 ?

 

Userlevel 2
Badge +1

@N_ali 

 

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

 

Userlevel 2
Badge +1

@N_ali 

My data collector version is 4.4.0 

Userlevel 2
Badge +1

@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.value['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

Userlevel 2
Badge +1

@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.

Reply