Ask Your Question
1

Creating nested JSON

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

srinath_222 gravatar image

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

metadaddy gravatar image

Hi,

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

Comments

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

iamontheinet gravatar imageiamontheinet ( 2019-01-23 11:35:54 -0500 )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 -0500 )edit

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

iamontheinet gravatar imageiamontheinet ( 2019-01-23 12:17:22 -0500 )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 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

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

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

Comments

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 -0500 )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 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

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

Seen: 403 times

Last updated: Jan 23