Skip to main content
moshell281
Bronze ⭐️⭐️⭐️
August 28, 2024
Question

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

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

DianaTorres
Community Manager
Community Manager
August 28, 2024

@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.

Translate
Regards, Diana Torres, Senior Community Moderator
steven-matison
Platinum ⭐️⭐️
Platinum ⭐️⭐️
August 29, 2024

@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".

Translate
SAMSAL
Platinum ⭐️⭐️⭐️
August 30, 2024

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.

Translate
DianaTorres
Community Manager
Community Manager
September 11, 2024

@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.

Translate
Regards, Diana Torres, Senior Community Moderator