Ask Your Question
0

How do I flatten an array on each element of a list?

asked 2020-07-30 11:55:46 -0500

Jorge Arévalo gravatar image

updated 2020-07-31 05:58:16 -0500

Each record of my input looks like this:

{
  "id":1,
  "results":[
    {
      "foo":[
        "bar"
      ]
    },
    {
      "foo":[
        "baz"
      ]
    },
    {
      "foo":[
        "whatever"
      ]
    }
  ],
  "another_field":"blahblahblah"
}

And I want this

{
  "id":1,
  "results":[
    {
      "foo":"bar"
    },
    {
      "foo":"baz"
    },
    {
      "foo":"whatever"
    }
  ],
  "another_field":"blahblahblah"
}

Tried with field renamer and expression evaluator, but no luck yet :-(

EDITED TO SHOW CODE INSTEAD OF IMGS

edit retag flag offensive close merge delete

Comments

Can you update your question and add sample input as text (instead of img) so it's easy to copy-paste? Tx!

iamontheinet gravatar imageiamontheinet ( 2020-07-30 17:10:35 -0500 )edit
1

Sure! Done

Jorge Arévalo gravatar imageJorge Arévalo ( 2020-07-31 05:58:39 -0500 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2020-07-31 11:19:22 -0500

iamontheinet gravatar image

Hi!

If you're open to adding a Jython Evaluator, here's the code that will give you the desired output:

for record in sdc.records:
  try:    
    results = []
    for result in record.value['results']:
      for k,v in result.iteritems():
        results.append({k:v[0]})

    record.value['results'] = results
    sdc.output.write(record)
  except Exception as e:
    # Send record to error
    sdc.error.write(record, str(e))

image description

Hope this helps.

Cheers, Dash

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2020-07-30 11:55:46 -0500

Seen: 72 times

Last updated: Jul 31