Our Community is getting an upgrade! To get everything ready for the relaunch, we’ll be placing the site in read-only mode starting September 21st.
We really appreciate your understanding while we get things set up behind the scenes. Catch up on all the exciting details about the move here.
Need help or have questions? Drop us a line at [email protected]
Created on 05-05-2017 02:38 PM - edited 09-16-2022 04:34 AM
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
Created 05-05-2017 07:00 PM
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)
Created 05-05-2017 07:00 PM
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)
Created 05-08-2017 10:05 AM
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:
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
Created 05-12-2017 06:39 AM
Any idea on how to read the payload / content of flowfile to convert the JSON to XML? Please help.
Created 05-12-2017 06:45 AM
Hmm. can't find anything obvious. Best to post a new questions on HCC for this, so it gets the proper attention.
Created on 03-16-2018 03:29 AM - edited 08-17-2019 07:09 PM
Hi @Ward Bekker I am trying the same code but getting error :
Code is below one , could you let me know what is wrong, I have added dependency as well.
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)
Created 05-12-2017 12:08 PM
Finally was able to achieve this :
flowFile = session.write(flowFile,
{ inputStream, outputStream ->
def str= IOUtils.toString(inputStream, StandardCharsets.UTF_8)
...
} as StreamCallback)
Created 05-12-2017 12:09 PM
Excellent!
Created 12-04-2017 01:39 PM
Can you show how you transform the resultant str to xml, in place of the "..." above? Thanks very much.
Created 01-20-2018 07:45 AM
@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