Ask Your Question

Creating nested JSON

asked 2019-01-23 10:14:08 -0600

srinath_222 gravatar image

updated 2019-01-23 12:31:46 -0600

metadaddy gravatar image


I have a requirement with input data as: {"field1":"xyz","field2":"xyz2","field3":"xyz3", "field4":"xyz4"}

I need the output data as below: {"Data":{"field1":"xyz","field2":"xyz2","field3":"xyz3"},"field4":"xyz4"} to load into MongoDB.

Please suggest whether to use Data Generator or JSON Generator or custom Groovy code will be useful to create a nested JSON object

edit retag flag offensive close merge delete


Hi! What's the criteria for leaving out "field4" in your example?

iamontheinet gravatar imageiamontheinet ( 2019-01-23 11:35:54 -0600 )edit

Hi, I have a classification for nested object which only need field1, field2 and field3. If we can do only with all the fields, I can separate field4 later using a processor like field merger.

srinath_222 gravatar imagesrinath_222 ( 2019-01-23 11:46:15 -0600 )edit

Are the field names actually "field1", "field2"...?

iamontheinet gravatar imageiamontheinet ( 2019-01-23 12:17:22 -0600 )edit

We have a business names for these columns...just for generalization named it as field1, field2

srinath_222 gravatar imagesrinath_222 ( 2019-01-23 12:19:11 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2019-01-23 12:41:09 -0600

metadaddy gravatar image

You can manipulate the record with the Expression Evaluator and Field Renamer, then either do the JSON encoding in the destination or use Data Generator.

First, use Expression Evaluator to create the /Data field as an empty map:

image description

Then use Field Renamer to put /field1, /field2 and /field3 under /Data:

image description

This combination will restructure the records as you require:

image description

Now, if you need the JSON as a string, you can use Data Generator to create the JSON, but if you are writing to a file, Kafka etc, you can just set the data format in the destination to JSON.

edit flag offensive delete link more


Cheers...It worked. I tried with all the processors like Field Merger, JSON Generator and Data Generator, but was unable to do it. Nice to know about how to use emptyMap expression.

srinath_222 gravatar imagesrinath_222 ( 2019-01-23 12:50:24 -0600 )edit

Great! By the way - you don't need to close questions when they're answered. There may be someone with an alternate way of solving the problem.

metadaddy gravatar imagemetadaddy ( 2019-01-23 13:26:33 -0600 )edit

what if the field 3 is nested with field 5 and field 6? How do we write the multi level json? Please provide your answer. Thanks!

Mahender gravatar imageMahender ( 2020-11-19 10:44:32 -0600 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2019-01-23 10:14:08 -0600

Seen: 1,628 times

Last updated: Jan 23 '19