Swayam's Micro-blogging: Combine multiple JSON records into a single one

  • 4 September 2021
  • 1 reply
  • 252 views

Userlevel 3
Badge

Dear Community users,

To extend the last topic where I shared how to convert the CSV to JSON, what if you have to combine multiple JSON records in a batch into a single JSON?

I often came with requirements where we have to call an external API which suports both single as well as batch requests and most often batch is preffered to avoid the multiple round-trips and increae performance. This topic can be very useful for developers who wants to convert the multiple CSV records coming in a batch to a single JSON list array and send them as a single record to a target destination.

Problem Statement

Convert the 2 input CSV records into a single JSON.

Input CSV Records:

NAME|AGE|DEPARTMENT|ADDRESS
"John"|"23"|"HR"|"31, 2nd Cross, 4th Street, Wilson-543234"
"Amanda"|"26"|"HR"|"24, 1st Cross, Wilson Garden-543234"

Output Single JSON Record:

[
{
"data": {
"NAME": "John",
"AGE": "23",
"DEPARTMENT": "HR",
"ADDRESS": "31, 2nd Cross, 4th Street, Wilson-543234"
}
},
{
"data": {
"NAME": "Amanda",
"AGE": "26",
"DEPARTMENT": "HR",
"ADDRESS": "24, 1st Cross, Wilson Garden-543234"
}
}
]

 

Pipeline (Extending with just one more Jython evaluator) from the previous blog:

Past Blog Reference: 

The new Jython Eveluator combines all the JSON records coming in a batch into a single record.

whole_data = ""
for record in records:
try:
# Access each record field and its value
for (field,val) in record.value.iteritems():
whole_data = whole_data+ val + ","

# Write record to processor output
t_whole_data = whole_data.rstrip(whole_data[-1])
record.value['data'] = t_whole_data

#output.write(record)

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

output.write(record)

 

As you can see in the pipeline review, both input records are now combined into a single record.

Conclusion

Simple. Honestly, I’ve seen developers spending time to carry out quick PoCs but spending long hours to find how they can do it inside SDC. I’m sure there may be other better ways to do it, but idea here is to make a quick start to see the power of data and tools like SDC that helps us.

 


1 reply

Userlevel 5
Badge

@swayam Moved this to the Community Articles section. Awesome stuff! 

Reply