jmarco10
jmarco10

Reputation: 525

Nifi - How to insert XML whole content into JSON attribute

I am trying to insert the whole content of a row of an XML file into a JSON attribute (I am a newbie).

I am doing it this way (tell me if there is an easier way, it's good to now):

enter image description here

I have configured Extract text this way:

enter image description here

And to finish, I configure the Replace Text, giving a JSON format:

enter image description here

But he result appears to be wrong (doesn't work like a normal JSON file, for example if I a try to do a httpPost):

enter image description here

How can I fix this problem?

cheers

Upvotes: 2

Views: 1487

Answers (1)

notNull
notNull

Reputation: 31540

If you are concern regards to new lines and json key/values then use NiFi expression language functions on the extracted attribute(data).

ReplaceText Configs:

enter image description here Replacement value:

{"name" : "user1","time" : "${now()}","data" : "${data:replaceAll('\s',''):escapeJson()}"}

Use escapeJson and replaceAll function to replace all spaces,newlines with '' Replacement Strategy as Always Replace

(or)

Another way of preparing json message is by using AttributesToJson processor. if we are using this processor then we need to prepare attributes/values before AttributesToJson processor by using UpdateAttribute processor

Flow:

1.SplitXml
2.ExtractText //add data property to extract content to flowfile attribute
3.UpdateAttribute //add name property -> user1 
                    add time property -> ${now()} 
                    add data property -> ${data:replaceAll('\s',''):escapeJson()}}
4.AttributeToJson //Attributes List -> name,time,data
                    Destination     -> flowfile content
                    include core attributes -> false

Upvotes: 6

Related Questions