admin管理员组文章数量:1336632
I'm expected to read the value of where(key = Canonical message) and the value format is combination of many fields where I expected the read 'casetTitle' value in the Payload section mentioned below and set a property using JsonSlurper()
Input JsoN
{
"Id": "db338737-af14-ef11-9f8a-000d3aad8ef7",
"InputParameters": [
{
"key": "CanonicalMessage",
"value": "{\"ActivityId\":\"9f368268\",\"Payload\":{\"CaseTitle\":\"Material Description 000\",\"CustomerAssetNumber\":\"104628\"}}"
}
{
"key": "transactionlogid",
"value": "9f36828e9"
}
]
}
how this be achieved through using JsonSlurper()?
Thank you very much
Yours sincerely Sateesh
I'm expected to read the value of where(key = Canonical message) and the value format is combination of many fields where I expected the read 'casetTitle' value in the Payload section mentioned below and set a property using JsonSlurper()
Input JsoN
{
"Id": "db338737-af14-ef11-9f8a-000d3aad8ef7",
"InputParameters": [
{
"key": "CanonicalMessage",
"value": "{\"ActivityId\":\"9f368268\",\"Payload\":{\"CaseTitle\":\"Material Description 000\",\"CustomerAssetNumber\":\"104628\"}}"
}
{
"key": "transactionlogid",
"value": "9f36828e9"
}
]
}
how this be achieved through using JsonSlurper()?
Thank you very much
Yours sincerely Sateesh
Share Improve this question asked Nov 20, 2024 at 19:36 SateeshSateesh 531 silver badge9 bronze badges 1- 1 Please add the code you have tried and how it failed (e.g. errors, stacktraces, logs, ...) so we can improve on it. – cfrick Commented Nov 20, 2024 at 19:46
1 Answer
Reset to default 0It'd look something like the following. Keep in mind I had to escape the quote escape sequence because I inlined this into a String literal. Otherwise the language was interpret \" -> " and create invalid JSON. If you were reading this from input you wouldn't need to do that. This is just an artifact of this example; not real world code.
import groovy.json.*
def slurper = new JsonSlurper()
def json = slurper.parseText("""
{
"Id": "db338737-af14-ef11-9f8a-000d3aad8ef7",
"InputParameters": [
{
"key": "CanonicalMessage",
"value": "{\\"ActivityId\\":\\"9f368268\\",\\"Payload\\":{\\"CaseTitle\\":\\"Material Description 000\\",\\"CustomerAssetNumber\\":\\"104628\\"}}"
},
{
"key": "transactionlogid",
"value": "9f36828e9"
}
]
}
""")
List<String> caseTitles = json.InputParameters
.findAll { it.key == "CanonicalMessage" }
.collect {
def innerJson = slurper.parseText( it.value )
innerJson.Payload.CaseTitle
}
println(caseTitles)
This is executable and works.
本文标签: groovyParse value from JSON Input message with special char formatStack Overflow
版权声明:本文标题:groovy - Parse value from JSON Input message with special char format - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742334194a2455295.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论