nifi: how to change value of json?

2019-09-16 10:37发布

i have processor InvokeHTTP which gives json result. for instance:

{
  "revision" : {
    "clientId" : "dc572274-4b71-11b6-e415-b91e391bcf4d",
    "version" : 7
  },
  "id" : "dc572260-4b71-11b6-0371-f73573ab44fe",
  "uri" : "http://x.x.x.x:9090/nifi-api/processors/dc572260-4b71-0371-73ab44fe",
  "position" : {
    "x" : -1021.9568138214972,
    "y" : 333.2029958718132
  }
}

i want to change the value of version dynamically for each incoming response of InvokeHTTP. how to achieve this?

标签: apache-nifi
2条回答
聊天终结者
2楼-- · 2019-09-16 10:59

you can use this sequence of processors: EvaluateJsonPath to get value of attribute, UpdateAttribute to change it, and ReplaceText to substitute old value in content with new one using regular expression.

For the ReplaceText processor use following parameters:

Regexp Replace strategy

Search Value : (?s)("version"\s*:\s*)(\d+)

Replacement Value : $1${VERSION} (where VERSION is an attribute name that holds new value)

Here is a nice resource to test the regular expressions: https://regex101.com/r/JOrZNp/1

查看更多
ゆ 、 Hurt°
3楼-- · 2019-09-16 11:10

i could suggest ExecuteScript processor with groovy language

import groovy.json.*

def ff = session.get()
if(!ff) return
ff = session.write(ff, {rawIn, rawOut->
    //parse flowfile content to maps & arrays
    def json = new JsonSlurper().parse(rawIn, "UTF-8")
    //change json 
    json.revision.version =  (json.revision.version as Long) + 1
    //write to output changed content
    rawOut.withWriter("UTF-8"){ it.write( JsonOutput.toJson(json) )}
} as StreamCallback)
session.transfer(ff, REL_SUCCESS)
查看更多
登录 后发表回答