Created 07-28-2022 11:13 PM
When using the XMLReader service, how do you retain the outermost parent element of an XML document so when I convert it to JSON I have the outermost element in the JSON version too?
For example, given the following XML document how do I capture the "<a>" element rather than just the "<b>" element?
<a>
<b>45</b>
</a>
What I want:
{"a": {"b":45}}
What I get:
{"b":45}
Created 08-02-2022 10:20 PM
@ChuckE ,
You can use a JoltTransformRecord processor to perform the conversion to JSON and, at the same time, add the root node back to it.
The JOLT specification I used is this:
{
"*": "a.&"
}
Cheers,
André
Created 08-03-2022 10:13 AM
I've since discovered a super easy way to resolve this. Simply using the XMLRecordSetWriter does EXACTLY what I was looking for.
Created on 07-29-2022 10:42 AM - edited 07-29-2022 10:42 AM
Hi,
Im not sure this can be fixed by changing some configuration on what you have wither on the processor or the service level. Since the root element is not considered in the conversion from xml to Json, as workaround you can surround your xml with arbitrary root element with the ReplaceText Processor before the ConvertRecrod Processor:
This will output the following xml:
<root>n<a> <b>45</b> </a> n</root>
And After ConvertRecord Prceossor :
{"a": {"b":45}}
Hope that helps. If it does, please Accept Solution.
Thanks
Created 08-01-2022 07:26 AM
Thank you SAMSL for your response. In the interest of scalability I was trying to avoid performing text manipulations. I ultimately decided to go with an XSLT transform since this processor is optimized for performing these types of operations, making it more scalable. I wanted to verify there wasn't an option within the XMLReader that I was missing, but it seems not.
On a side note, as an intellectual curiosity I also tried using the QueryRecord processor to see if I could select the outermost element in a query and slap wrapper text around it. But it seems there is no way to perform this task with an XMLReader because it can't discern the schema of the data, so "select * from flowfile" is the only thing that works apparently.
Created 08-01-2022 02:57 PM
Do your XML messages conform to defined a schema?
Cheers,
André
Created 08-01-2022 05:30 PM
Yes, I validate the incoming xml with a strongly typed xsd.
Created 08-02-2022 10:20 PM
@ChuckE ,
You can use a JoltTransformRecord processor to perform the conversion to JSON and, at the same time, add the root node back to it.
The JOLT specification I used is this:
{
"*": "a.&"
}
Cheers,
André
Created 08-02-2022 10:41 PM
This seems like a good idea. I'll give this a try and test the performance against the XSLT transform. I've never used the JOLT processors before so this will be a good opportunity to experiment with one. Thanks for the idea.
Created 08-02-2022 10:43 PM
I'm also curious about the performance difference. If you could report your results here, I'd appreciate!
Cheers,
André
Created 08-03-2022 10:13 AM
I've since discovered a super easy way to resolve this. Simply using the XMLRecordSetWriter does EXACTLY what I was looking for.
Created 08-03-2022 07:24 PM
Can you explain how did you resolve it with XMLRecrodSetWriter? Thanks