Support Questions

Find answers, ask questions, and share your expertise

Apache NiFi. ConvertRecord XML to JSON. interpret all values as text

avatar
New Contributor

Hi all!

i faced out with the problem.
I need to convert xml file to json.
here is my flow

moshell281_0-1724848662183.pngmoshell281_1-1724848692878.png
ConvertRecord

moshell281_2-1724848729150.png
XMLReader

moshell281_3-1724848805804.png
JsonRecordSetWritter


Everything is ok, but the problem is in convetring digit-values

for example
xml data

<document_code>
<code>000</code>
</document_code>

converted to

 

"document_code": {
  "code": 0
}

 

another one is:

<service-code>0012388833456</service-code>

converted to

 

"service-code": 12388833456

 


this is critical data, so i want Ni-Fi to convert them as string.
all values as string - is what i need.

Help please.

4 REPLIES 4

avatar
Community Manager

@moshell281 Welcome to the Cloudera Community!

To help you get the best possible solution, I have tagged our NiFi experts @steven-matison @joseomjr  who may be able to assist you further.

Please keep us updated on your post, and we hope you find a satisfactory solution to your query.


Regards,

Diana Torres,
Community Moderator


Was your question answered? Make sure to mark the answer as the accepted solution.
If you find a reply useful, say thanks by clicking on the thumbs up button.
Learn more about the Cloudera Community:

avatar

@moshell281   This is indeed a very tough solution to solve.   The only thing that comes to mind is creating your own schema, versus inferring the schema.  Creating that schema from scratch could be a long task.  So one trick you can do would be to run the flow to infer the schema, and configure the controller service to write that schema to an attribute.   Inspect a flowfile and grab that schema.   Next modify the schema to suit your needs, and then modify and test the flow with the specified schema (no longer inferred).   This should allow you control to specify the correct data type for "000" so it is not assumed to be "0".

avatar
Super Guru

Hi @moshell281 ,

@steven-matisonis right, when you use infer schema  the processor will map the value as it thinks the data type should be, so if it sees an integer it will map to an integer and so on. To force certain datatypes you have to define your own Avro schema and feed it to the Json Record Set Writer since this is your target format. If you are not comfortable working with Avro schema then you can use UpdateRecord or even JsonJoltTransformation processors to cast the value into the proper type.

Hope you find this helpful.

avatar
Community Manager

@moshell281 Has the reply helped resolve your issue? If so, please mark the appropriate reply as the solution, as it will make it easier for others to find the answer in the future.  Thanks.


Regards,

Diana Torres,
Community Moderator


Was your question answered? Make sure to mark the answer as the accepted solution.
If you find a reply useful, say thanks by clicking on the thumbs up button.
Learn more about the Cloudera Community: