Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

Get dynamic property name in execute script

avatar

Hi,

I am trying to make a Javascript code for the executeScript processor and I wonder if it's possible to get the name of a custom property added from the executeSript Properties tab.

I know that if I add a property "myProperty", I can access it from the script by using myProperty.getValue() (or with .evaluateAttributeExpressions() when needed).

But is it possible to find the key "myProperty" from inside the script ?

I would like to do something like

for(i = 0 ; i < customProperties.length ; i++){
	propertyName = customProperties[i].getName()
	value = propertyName.getValue()
	//Do something with propertyName and its value
}


Thanks in advance !
Félicien

1 ACCEPTED SOLUTION

avatar
Master Guru

You can access all the properties from the "context" variable, then filter on only the dynamic properties. You get a Map of PropertyDescriptors as keys to their values, so you can iterate over the keys to look for your property key(s). Here is a quick Groovy snippet that will log each dynamic property name:

context.properties.findAll {k,v -> k.dynamic}.each {k,v -> log.info(k.name)}

View solution in original post

10 REPLIES 10

avatar
@Félicien Catherin

If I understand your question properly, you want to check all the attributes of a flow file and then take some action on that attribute. For this, you can use getAttributes() function in your script. This will return you a map with attribute name as key and attribute value as value. For example

flowFile = session.get() 

attrMap = flowFile.getAttributes()

You can iterate on the map to check if a certain property exists or not or whatever actions you may want to take.

Hope that helps!

avatar
@Félicien Catherin

Did the answer help in the resolution of your query? Please close the thread by marking the answer as Accepted!

avatar

Not really.. but Matt's answer is what I needed, thank for your answer anyway !

avatar
Master Guru

You can access all the properties from the "context" variable, then filter on only the dynamic properties. You get a Map of PropertyDescriptors as keys to their values, so you can iterate over the keys to look for your property key(s). Here is a quick Groovy snippet that will log each dynamic property name:

context.properties.findAll {k,v -> k.dynamic}.each {k,v -> log.info(k.name)}

avatar

Thanks Matt ! This is exactly what I needed

avatar

This is working perfectly with this groovy script. However, I wanted to do that in Javascript (since I want to manipulate JSON objects), is there an easy way to filter the dynamics properties ?
For now, I can retrieve the list of properties (in context.properties), but I don't see any difference between the "hard coded properties" ("script engine","script file", "modules directory") and the properties I added myself.

Thanks in advance if you can help

avatar

avatar
Master Guru

I used Groovy's findAll method for filtering/iterating in my example, you'd just have to use the Javascript idiom, maybe something like:

for(var pdMap in context.getProperties()) {
if(pd.getKey().isDynamic()) {
// This is a user-defined "dynamic" property, the ExecuteScript properties won't show up here
}
}

Having said that, if you are only using Javascript in order to parse JSON, you can keep Groovy and use JsonSlurper and JsonOutput and other handy classes for manipulating JSON objects.

avatar

Thanks a lot ! , i'll have a look to JsonSlurper and JsonOutput