How to store json object in a variable using apache nifi? - apache-nifi

The following flowfile is the response of an "InvokeHttp":
[
{"data1":"[{....},{...},{....}]","info":"data-from_site"},
{"data2":"[{....},{...},{....}]","info":"data-from_site"},
{"data3":"[{....},{...},{....}]","info":"data-from_site"}
]
I did a "SplitJson", i got each json record as a single flowfile
flowfile 1:
{"data1":"[{....},{...},{....}]","info":"data-from_site"}
flowfile 2:
{"data2":"[{....},{...},{....}]","info":"data-from_site"}
flowfile 3:
{"data3":"[{....},{...},{....}]","info":"data-from_site"}
I want to store each json record in each flowfile in a variable like that:
variable1 = "{"data1":"[{....},{...},{....}]","info":"data-from_site"}"
variable2 = "{"data2":"[{....},{...},{....}]","info":"data-from_site"}"
variable3 = "{"data3":"[{....},{...},{....}]","info":"data-from_site"}"
can someone show me how to store the json record in a variable !

If I understand correctly what you want to do (by "variable", do you mean what is called "attribute" in NiFi?), you can use the EvaluateJsonPath processor configured with:
flowfile-attribute as Destination
json as Return type

Related

How to load json record to json colum in postgres with apache nifi?

This is my flow file content:
{
"a":"b",
"c":"y",
"d":"z",
"e":"w",
"f":"u",
"g":"v",
"h":"o",
"x":"t"
}
The final result should look like that in Postgres :
| test |
|----------------------------------------------------------------|
|{"a":"b,"c":"y","d":"z","e":"w","f":"u","g":"v","h":"o","x":"t"}|
the table is: json_test
the column name is test
Those steps shows how i tried to solve the problem:
My method was to store the json record in a variable as string with "ExtractText":
the attribute data take only some key-values from the json not the entire record:
data = {"a":"b",
"c":"y",
"d":"z",
"e":"w",
"f":
so i have a problem in the regex expression.
next i used PutSQL with the following SQL statement:
Unfortunately the result isn't the wanted one.
I need to know the exact expression that i should set in ExtractText to get the entire json record in a variable as string.
The sql statement should be:
insert into schema.table_name(column_name) values(the_variable_where the flowfile data was stored)

Get id from previous processor NiFi

Processors I'm referring to
Is it possible that the processor "InvokeHTTP" takes the information "id" from the previous processor(in this case SELECT_FROM_SNOWFLAKE)?
Where i want to change
I would like the "Remote URL" to be something like:
http://${hostname()}:8080/nifi-api/processors/${previousProcessorId()}
No, you can't. But you can get name, id or other properties for current processor group using ExecuteScript or ExecuteGroovy processors somewhere in this flow to find these informations with script:
def flowFile = session.get()
if(!flowFile) return
processGroupId = context.procNode?.processGroupIdentifier ?: 'unknown'
processGroupName = context.procNode?.getProcessGroup().getName() ?: 'unknown'
flowFile = session.putAttribute(flowFile, 'processGroupId', processGroupId)
flowFile = session.putAttribute(flowFile, 'processGroupName', processGroupName)
session.transfer(flowFile, REL_SUCCESS)
After that, you can find get the id of this snow_flake processor in this processor group for example in rest api.
the Remote URL property in InvokeHTTP processor supports nifi expression language.
So, if previous processor sets attribute hostname then you can use it as http://${hostname}:8080/...
However SelectSQL returns result in Avro format.
Probably before InvokeHTTP you need to convert avro to json and then evaluatejsonpath to extract required values into attributes.

How to remove key of JSON and return entire thing as Content?

My attribute employee has below JSON value:
{"testQuery": {"$set": {"name":"Harsh"} }}
I want to send the {"$set": {"name":"Harsh"} } to a customizedProcessor as flowFile content not as attribute, need to perform the following,
Remove the testQuery key from the JSON value of attribute employee,
Convert the value of testQuery to flowFile content and return.
I assume you have employee attribute in the flow file with value:
{"testQuery": {"$set": {"name":"Harsh"} }}
Flow to get {"$set": {"name":"Harsh"} } as a flow file content:
1. ReplaceText - to store attribute into content
Replacement Value = ${employee}
2. EvaluateJsonPath - to extract required json value and put into content
Destination = flowfile-content
value = $.testQuery

Jmeter, How to get one of response result to assign to next request

I have a response for request #1: 2 step codes that correspond with stepId as below.
In next request, I want to use only stepId = stepId that I assign from CSV file
**content**":[
{
"stepId":21,
"stepCode":"11",
"stepName":"11",
},
{
"stepId":17,
"stepCode":"??",
"stepName":"To be checked",
}
]
For example if you need to get stepCode value where stepId is 17 you can use JSON Extractor and the following JSON Path query:
$..[?(#.stepId == '17')].stepCode
Demo:
You can replace this 17 with a variable coming from the CSV Data Set Config like:
$..[?(#.stepId == '${your_variable_from_csv}')].stepCode
References:
Json Path Operators
JMeter's JSON Path Extractor Plugin - Advanced Usage Scenarios

Transform data with NIFI

What's the best practice with NIFI to extract an attribute in a flowfile and transform it in a Text Format Example :
{ "data" : "ex" } ===> My data is ex
How can I do this with NIFI wihtout using a executeScript Processor
You could use ExtractText to extract the values into attributes. If you added a property in ExtractText like foo = {"(.+)" : "(.+)"} then your flow file would get two attributes for each of the capture groups in the regex:
foo.1 = data
foo.2 = ex
Then you can use ReplaceText with a Replacement Value of:
My ${foo.1} is ${foo.2}

Resources