0
votes

I want to split and transfer the json data in NiFi, Here is my json structure look like this; I want to split json by id1,id2 array of json transfer to respective processor group say example processor_group a,b. I tried with evaluate json path $.id1,$.id2 but i didn't get exact solution. Can you please help me out from this issue;

{
    "id1": [{
        "u_name": "aa"
    }, {
        "addr": "bb"
    }],
    "id2": [{
        "u_name": "aa"
    }, {
        "addr": "bb"
    }]
}
2
Title of question doesn't match ask for a method to split.... also I would avoid posting on multiple platforms, most of us are on both. My reply below is from Cloudera Community. - steven-matison

2 Answers

0
votes

The processor you're looking for is SplitJSON. Configure it as follows: enter image description here

Then, you'll receive two FlowFiles: enter image description here

First one will contain the id1:

[{
        "u_name": "aa"
    }, {
        "addr": "bb"
    }]

second one will contain id2:

[{
        "u_name": "aa"
    }, {
        "addr": "bb"
    }]
0
votes

Here is how to get to the values you want with EvaluateJsonPath:

@varun_rathinam Accessing json in an array object via EvaluateJsonPath can be quite confusing.   I also notice the structure of your json is kind of confusing with same values in both.  I have adjusted id2 for cc and dd for testing so that I can tell id1 and id2 values apart.

The solution you want is (see template for exact string values):

enter image description here

Notice we use the normal tree for each json object ( $.object ) then access the array ( 0, 1 ) then access the array's objects.   Also notice it is possible to access the json object array with or without a . before the [.

Reference: https://community.cloudera.com/t5/Support-Questions/how-to-extract-fields-in-flow-file-which-are-surrounded-by/m-p/208635

You can also find my template during testing of your issue on my GitHub: https://github.com/steven-dfheinz/NiFi-Templates/blob/master/NiFI_EvaluateJsonPath_Demo.xml