Support Questions

Find answers, ask questions, and share your expertise

SplitJson behavior for non array input

avatar
Contributor

Hi,

If my flowfile contains json, which at JsonPath does not have array, it's routed to failure. But lets assume, that I don't know, whether I get single record or array of recods. Would it make sense to create property to control behavior in such case? User could then decide, whether it's an error, or whether unchanged flow should be routed to original relationship (or any other relationship)

Or is there any other way, how to test flowfile, whether there is at given JsonPath array or something else?

Thanks!

(if change described in first paragraph is necessary and would be accepted, I can do the coding, np).

1 ACCEPTED SOLUTION

avatar
Master Guru
@Martin Mucha

By using RouteOnContent processor we can check the contents of flowfile like is the flowfile message having array in it (or) not,

if it is having array then you can route the flowfile to some processors, if not having array route to some other processors.

Example:-

Let's consider i'm having array of json messages and i'm splitting them individual messages

Input:-

[{"Id": "1","name":"HDP"},
{"Id": "2","name":"HDF"}]

i'm having 2 json messages in an array and i need to add id,name values as attributes for this case i need to split the json array into individual flowfiles.

SplitJSON:-

JsonPath Expression

$.*

this property will splits json array into individual flowfiles.

For out input the output would be 2 flowfiles then i will use Evaljson path to extract attributes and add them to the flowfile.

But i'm not sure everytime i will get json array (or) not, some times i get only single json message like

{"Id": "1","name":"HDP"}

for this input my splitjson processor won't give expected results.

So for this case i'm going to use

RouteOnContent processor:-

to check the contents of flowfile and route them accordingly

Case1:-

if i get json array message then i need to route the flowfile to SplitJson processor.

Configs:-

Add new property as

Array

^\[\{  //we are checking is the content starting with [ and followed by { that means it's an array of json messages

in this case i'm going to transfer Array property relation to SplitJson processor.

Case2:-

if no data is in the json message then i'm checking with below property.

NoData

^\{\} //checking the content having empty message in it

i'm auto terminating this relation as we are not caring if there is no data.

So now the unmatched relation will have single json messages i.e

{"Id": "1","name":"HDP"}

I'm routing this single json message to other processors(like EvalJsonPath..) to extract attribute values.

RouteOnContent Configs:-

54404-routeoncontent.png

Sample Flow:-

54405-flow.png

Like this way you can check the contents of flowfile is content having arrays or not.

for testing in the above example i'm just checking the starting characters of content, but you can check the whole contents of flowfiles by just adding new property with matching regex and change buffer size if needed.

.

If the Answer helped to resolve your issue, Click on Accept button below to accept the answer, That would be great help to Community users to find solution quickly for these kind of issues.


routeoncontent.png

View solution in original post

1 REPLY 1

avatar
Master Guru
@Martin Mucha

By using RouteOnContent processor we can check the contents of flowfile like is the flowfile message having array in it (or) not,

if it is having array then you can route the flowfile to some processors, if not having array route to some other processors.

Example:-

Let's consider i'm having array of json messages and i'm splitting them individual messages

Input:-

[{"Id": "1","name":"HDP"},
{"Id": "2","name":"HDF"}]

i'm having 2 json messages in an array and i need to add id,name values as attributes for this case i need to split the json array into individual flowfiles.

SplitJSON:-

JsonPath Expression

$.*

this property will splits json array into individual flowfiles.

For out input the output would be 2 flowfiles then i will use Evaljson path to extract attributes and add them to the flowfile.

But i'm not sure everytime i will get json array (or) not, some times i get only single json message like

{"Id": "1","name":"HDP"}

for this input my splitjson processor won't give expected results.

So for this case i'm going to use

RouteOnContent processor:-

to check the contents of flowfile and route them accordingly

Case1:-

if i get json array message then i need to route the flowfile to SplitJson processor.

Configs:-

Add new property as

Array

^\[\{  //we are checking is the content starting with [ and followed by { that means it's an array of json messages

in this case i'm going to transfer Array property relation to SplitJson processor.

Case2:-

if no data is in the json message then i'm checking with below property.

NoData

^\{\} //checking the content having empty message in it

i'm auto terminating this relation as we are not caring if there is no data.

So now the unmatched relation will have single json messages i.e

{"Id": "1","name":"HDP"}

I'm routing this single json message to other processors(like EvalJsonPath..) to extract attribute values.

RouteOnContent Configs:-

54404-routeoncontent.png

Sample Flow:-

54405-flow.png

Like this way you can check the contents of flowfile is content having arrays or not.

for testing in the above example i'm just checking the starting characters of content, but you can check the whole contents of flowfiles by just adding new property with matching regex and change buffer size if needed.

.

If the Answer helped to resolve your issue, Click on Accept button below to accept the answer, That would be great help to Community users to find solution quickly for these kind of issues.


routeoncontent.png