我有两个json文件。
一个是 InputFile.json:
[{"no":"48","name":"Mahe","Age":"23"},
{"no":"49","name":"Siva","Age":"23"},
{"no":"50","name":"prabhu","Age":"37"}]
另一个是 Result.json
{"results":"[
{"Grade":"4","Result":"PASS"},
{"Grade":"5","Result":"FAIL"},
{"Grade":"6","Result":"PASS"}]"
}
在那个json中,我需要将这两个json组合起来,如下所示..,
[{"no":"48","name":"Mahe","Age":"23","Grade":"4","Result":"PASS"},
{"no":"49","name":"Siva","Age":"23","Grade":"5","Result":"FAIL"},
{"no":"50","name":"prabhu","Age":"37","Grade":"6","Result":"PASS"}]
基于索引结果[0]将与InputFile [0]结合。
现在我尝试在Groovy(ExecuteScript)NiFi中进行此操作。
无法从Result.json获得“4”,“5”,“6”成绩索引。
尝试回答的代码。
def slurper = new groovy.json.JsonSlurper()
def input = slurper.parse(inputFile)
def res = slurper.parse(result)
def joined = [input, res.results].transpose().collect { a, b -> a + b }
我遇到问题。
groovy.json.JsonException: expecting '}' or ',' but got current char 'G' with an int value of 71
The current character read is 'G' with an int value of 71
expecting '}' or ',' but got current char 'G' with an int value of 71
line number 2
index number 17
{"Grade":"4","Result":"PASS"},
..^
任何人都可以建议我将这两个json合并到NiFi中吗?
答案 0 :(得分:3)
您可以使用JsonSlurper
和transpose
def slurper = new groovy.json.JsonSlurper()
def input = slurper.parse(inputFile)
def res = slurper.parse(result)
def joined = [input, res.results].transpose().collect { a, b -> a + b }
啊,我看到......在你的例子中,由于某种原因,results
被包裹了一个字符串......所以我们需要将该字符串重新解析为json
这应该适用于这种情况:
def input = slurper.parse(inputFile)
def res = slurper.parseText(slurper.parse(result).results)
def joined = [input, res].transpose().collect { a, b -> a + b }