Support Questions

Find answers, ask questions, and share your expertise

Apache Nifi - JSON to XML or XML Translation

avatar
Explorer

I am looking for a Processor that can convert a JSON to an XML or Translate an XML

The main requirement is to replace the value of certain XML Elements in a XML or build an XML from a DB Query output.

Would also like to know how to insert XML data to DB Table like JSON to SQL

Appreciate any help

1 ACCEPTED SOLUTION

avatar

Don't know of a processor that directly converts JSON to XML. One option would be to use ExecuteScript with a Groovy script that did the conversion. It looks like there are some examples out there of converting JSON to XML with Groovy:

Example groovyscript:

import net.sf.json.JSON
import net.sf.json.JSONSerializer
import net.sf.json.xml.XMLSerializer


String str = '''{ "glossary": { "title": "example glossary", "GlossDiv": { "title": "S", "GlossList": { "GlossEntry": { "ID": "SGML", "SortAs": "SGML", "GlossTerm": "Standard Generalized Markup Language", "Acronym": "SGML", "Abbrev": "ISO 8879:1986", "GlossDef": { "para": "A meta-markup language, used to create markup languages such as DocBook.", "GlossSeeAlso": ["GML", "XML"] }, "GlossSee": "markup" } } } } }'''
JSON json = JSONSerializer.toJSON( str )
XMLSerializer xmlSerializer = new XMLSerializer()
xmlSerializer.setTypeHintsCompatibility( false )
String xml = xmlSerializer.write( json )
System.out.println(xml)

View solution in original post

9 REPLIES 9

avatar

Don't know of a processor that directly converts JSON to XML. One option would be to use ExecuteScript with a Groovy script that did the conversion. It looks like there are some examples out there of converting JSON to XML with Groovy:

Example groovyscript:

import net.sf.json.JSON
import net.sf.json.JSONSerializer
import net.sf.json.xml.XMLSerializer


String str = '''{ "glossary": { "title": "example glossary", "GlossDiv": { "title": "S", "GlossList": { "GlossEntry": { "ID": "SGML", "SortAs": "SGML", "GlossTerm": "Standard Generalized Markup Language", "Acronym": "SGML", "Abbrev": "ISO 8879:1986", "GlossDef": { "para": "A meta-markup language, used to create markup languages such as DocBook.", "GlossSeeAlso": ["GML", "XML"] }, "GlossSee": "markup" } } } } }'''
JSON json = JSONSerializer.toJSON( str )
XMLSerializer xmlSerializer = new XMLSerializer()
xmlSerializer.setTypeHintsCompatibility( false )
String xml = xmlSerializer.write( json )
System.out.println(xml)

avatar
Explorer

Hi Bekker,

Thanks for the response.

Find below the code I am trying to work on, which is a modification of the code you shared.

I am trying to take the output/flowfile from previous processor (which is a JSON) and trying to convert it to XML flowfile.

Please help identify where I am wrong.

Code:

import net.sf.json.JSON import net.sf.json.JSONSerializer import net.sf.json.xml.XMLSerializer import java.nio.charset.StandardCharsets JSON json = JSONSerializer.toJSON( session.get() ) XMLSerializer xmlSerializer = new XMLSerializer() xmlSerializer.setTypeHintsCompatibility( false ) newFlowFile = session.write(session.create(), {outputStream -> outputStream.write(xmlSerializer.write( json ).getBytes(StandardCharsets.UTF_8)) } as OutputStreamCallback) session.transfer(newFlowFile, REL_SUCCESS)

Error:

06:03:44 EDT ERROR d89b9512-015b-1000-3b38-ef64f47e9b90
ExecuteScript[id=d89b9512-015b-1000-3b38-ef64f47e9b90] ExecuteScript[id=d89b9512-015b-1000-3b38-ef64f47e9b90] failed to process session due to org.apache.nifi.processor.exception.FlowFileHandlingException: StandardFlowFileRecord[uuid=11a424b1-dd59-4199-990a-00afb08f45ba,claim=StandardContentClaim [resourceClaim=StandardResourceClaim[id=1493970997597-1, container=default, section=1], offset=81682, length=5214],offset=0,name=TestTemplate.xml,size=5214] transfer relationship not specified: org.apache.nifi.processor.exception.FlowFileHandlingException: StandardFlowFileRecord[uuid=11a424b1-dd59-4199-990a-00afb08f45ba,claim=StandardContentClaim [resourceClaim=StandardResourceClaim[id=1493970997597-1, container=default, section=1], offset=81682, length=5214],offset=0,name=TestTemplate.xml,size=5214] transfer relationship not specified

avatar
Explorer

Any idea on how to read the payload / content of flowfile to convert the JSON to XML? Please help.

avatar

Hmm. can't find anything obvious. Best to post a new questions on HCC for this, so it gets the proper attention.

avatar
Contributor

Hi @Ward Bekker I am trying the same code but getting error :

64631-error.jpg

Code is below one , could you let me know what is wrong, I have added dependency as well.

64632-deoendency.jpg

import net.sf.json.JSON
import net.sf.json.JSONObject
import net.sf.json.JSONSerializer
import net.sf.json.xml.XMLSerializer

String str = '''{ "glossary": { "title": "example glossary", "GlossDiv": { "title": "S", "GlossList": { "GlossEntry": { "ID": "SGML", "SortAs": "SGML", "GlossTerm": "Standard Generalized Markup Language", "Acronym": "SGML", "Abbrev": "ISO 8879:1986", "GlossDef": { "para": "A meta-markup language, used to create markup languages such as DocBook.", "GlossSeeAlso": ["GML", "XML"] }, "GlossSee": "markup" } } } } }'''JSON json = JSONSerializer.toJSON( str )
XMLSerializer xmlSerializer = new XMLSerializer()
xmlSerializer.setTypeHintsCompatibility( false )
String xml = xmlSerializer.write( json )
print(xml)

avatar
Explorer

Finally was able to achieve this :

flowFile = session.write(flowFile,
        { inputStream, outputStream ->
            def str= IOUtils.toString(inputStream, StandardCharsets.UTF_8)
...
        } as StreamCallback)

avatar

Excellent!

avatar
Explorer

Can you show how you transform the resultant str to xml, in place of the "..." above? Thanks very much.

avatar
Contributor

@Anil Puvvada Could you show the whole solution pls, im pretty new in nifi, and I need this json to xml tranformation too. I dont really understood what I have to setup in execute string now. thanks