rennyB
rennyB

Reputation: 173

NiFi XML to JSON

I have a NiFi flow that among other things transforms XML into JSON. This is done to prep the data for inserting into MongoDB. I'm using the TransformXML processor and an XSL to do the transform. Is this the correct method? Ordinarily, I would say that XSLT is not the best way to transform XML to JSON but it wasn't able to find another way in NiFi.

Upvotes: 4

Views: 8361

Answers (3)

dd619
dd619

Reputation: 6170

XSLT feels like overhead here as it requires writing transformation.

ConvertRecord processor is the simplest way to do it. Please refer configuration from below screenshot: enter image description here

So basically we just need to mention Record Reader and Record Writer. In your case Record Reader= XMLReader and Record Writer= JsonRecordSetWriter

Upvotes: 0

lifeisshort
lifeisshort

Reputation: 283

If your XML has a specific structure(not dynamic), you can use ConvertRecord processor.

  1. Choose XMLReader for read XML. For this, you must define an avro schema.
  2. Choose JsonRecordSetWriter for write converted result. In this state, if you don't want to change structure, you don't have to change anything on JsonRecordSetWriter.

For more information, I suggest you look at the link below.

https://pierrevillard.com/2018/06/28/nifi-1-7-xml-reader-writer-and-forkrecord-processor/

Upvotes: 4

Vebbie
Vebbie

Reputation: 1695

Well, there can be two most preferable approaches to convert XML data with Apache NiFi:

A. Using the TransformXML processor with a XSLT file

There are so many examples providing solution to transform any XML into a JSON document using XSLT. And it’s very easy to use. But based on your requirement, you might need specific features.

E.g. https://community.hortonworks.com/articles/29474/nifi-converting-xml-to-json.html

https://gist.github.com/speby/9561961e06dc1b38822764b26ddc2159

https://community.hortonworks.com/questions/91784/could-transformxml-work-with-several-xslts.html

B. Using a Java processor with JSONObject library

Working with this approach you need to write your own custom processor.

Note: org.json is NOT Apache friendly in terms of licensing.

A very good example on this can be: https://gist.github.com/pvillard31/408c6ba3a9b53880c751a35cffa9ccea

Upvotes: 0

Related Questions