nifi: how to change value of json?

筅森魡賤 提交于 2019-12-11 07:32:21

问题


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?


回答1:


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)



回答2:


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



来源:https://stackoverflow.com/questions/44335048/nifi-how-to-change-value-of-json

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!