lifeisshort
lifeisshort

Reputation: 283

How to convert XML to JSON with new JSON structure in Nifi?

I get different XMLs from web services. I want to convert this XML to JSON, but structure must be changed.

For example, I have XML structure like this;

<root>
    <A attr="attr1">VAL</A>
    <B attr="attr2">VAL</B>
</root>

And result of JSON that I want.

{
   "root":{
      "Items":[
         {
            "tag_name":"A",
            "attr":"attr1",
            "value":"VAL"
         },
         {
            "tag_name":"B",
            "attr":"attr2",
            "value":"VAL"
         }
      ]
   }
}

How can I do this in Nifi? ConvertRecord or UpdateRecord? Also, how should read and write schema for this if record based processors may be used?

Upvotes: 1

Views: 1264

Answers (1)

&#211;scar Andreu
&#211;scar Andreu

Reputation: 1700

You can do it with a pure NiFi flow, the steps to do this are:

  1. Convert the XML to JSON, this can be done with a ValidateRecord processor, you must define the schema of the json, so during this step you are going to check that the input data is ok.
  2. Modify the JSON structure using the JoltTransform processor.

Upvotes: 1

Related Questions