Support Questions
Find answers, ask questions, and share your expertise
Announcements
Alert: Welcome to the Unified Cloudera Community. Former HCC members be sure to read and learn how to activate your account here.

Apache Nifi - JSON to XML or XML Translation

Solved Go to solution
Highlighted

Apache Nifi - JSON to XML or XML Translation

New Contributor

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

Accepted Solutions

Re: Apache Nifi - JSON to XML or XML Translation

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)
9 REPLIES 9

Re: Apache Nifi - JSON to XML or XML Translation

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)

Re: Apache Nifi - JSON to XML or XML Translation

New Contributor

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

Re: Apache Nifi - JSON to XML or XML Translation

New Contributor

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

Re: Apache Nifi - JSON to XML or XML Translation

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

Re: Apache Nifi - JSON to XML or XML Translation

New 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)

Re: Apache Nifi - JSON to XML or XML Translation

New Contributor

Finally was able to achieve this :

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

Re: Apache Nifi - JSON to XML or XML Translation

Excellent!

Re: Apache Nifi - JSON to XML or XML Translation

New Contributor

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

Re: Apache Nifi - JSON to XML or XML Translation

New 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

Don't have an account?
Coming from Hortonworks? Activate your account here