[英]Need help on replacing JSON field with constant String in Groovy
I want to replace a JSON field - JSONtoReplace by the below JSON strings我想用下面的 JSON 字符串替换一个 JSON 字段 - JSONtoReplace
[{"RoleName": "Normal User","ModuleName": "Calendar Management"},
{"RoleName": "Reporter","ModuleName": "Incident Management"},
{"RoleName": "Viewer","ModuleName": "ESG"},
{"RoleName": "Viewer","ModuleName": "Apps"}]
Source JSON BODY {"PersonInformation":{"EmailAddress":"fabian.koehlmann@atotech.com","Name":{"FirstName":"Fabian","LastName":"KOEHLMANN","PreferredName":"Fabian Köhlmann"},"Address":{"AddressLine1":"Berlin GmbH","AddressLine2":"Erasmusstraße 20,,,,,","PostalCode":"10553"},"employeeInformation":{"EmployeeType":"internal","EmploymentStatus":"Active","JobRole":{"Department":"Finance - IT","IsCreateDepartment":"null","JobTitle":"Specialist IT Security & Infrastructure","SupervisorId":"null","StartDate":"09/29/2021","EffectiveDate":"null"},"Location":"Berlin GmbH","Occupation":"null"},"IsUser":"true","UserInformation":{"UserId":"7fa1785f-f4f3-42b5-96bd-33f195521635","Status":"active","Locations":{"Scope":"TRN-LOC","Roles":"JSONtoReplace"}},"RecordUid":"2022/05/27","Id":"A0201412"}}源 JSON正文 {"PersonInformation":{"EmailAddress":"fabian.koehlmann@atotech.com","Name":{"FirstName":"Fabian","LastName":"KOEHLMANN","PreferredName":"Fabian Köhlmann"},"Address":{"AddressLine1":"Berlin GmbH","AddressLine2":"Erasmusstraße 20,,,,,","PostalCode":"10553"},"employeeInformation":{"EmployeeType ":"internal","EmploymentStatus":"Active","JobRole":{"Department":"Finance - IT","IsCreateDepartment":"null","JobTitle":"Specialist IT Security & Infrastructure"," SupervisorId":"null","StartDate":"09/29/2021","EffectiveDate":"null"},"Location":"Berlin GmbH","Occupation":"null"},"IsUser": "true","UserInformation":{"UserId":"7fa1785f-f4f3-42b5-96bd-33f195521635","Status":"active","Locations":{"Scope":"TRN-LOC","Roles ":"JSONtoReplace"}},"RecordUid":"2022/05/27","Id":"A0201412"}}
Groovy Script Groovy 脚本
def Message processData(Message message) {
def body = message.getBody(java.lang.String) as String;
def jsonSlurper = new JsonSlurper()
def SourceBody = jsonSlurper.parseText(body)
def ReplaceData = jsonSlurper.parseText('[{"RoleName": "Normal User","ModuleName":
"Calendar Management"},{"RoleName": "Reporter","ModuleName": "Incident Management"},
{"RoleName": "Viewer","ModuleName": "ESG"},{"RoleName": "Viewer","ModuleName":
"Apps"}]')
body = body.replaceAll('JSONtoReplace','ReplaceData');
message.setBody(body);
return message;
}
I want to replace a JSON field
我想替换一个 JSON 字段
You can do something like this:你可以这样做:
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
// ...
// This hardcoded String represents "body" from the code in the question
String jsonString = '''
[{"RoleName": "Normal User","ModuleName": "Calendar Management"},
{"RoleName": "Reporter","ModuleName": "Incident Management"}]
'''
def slurper = new JsonSlurper()
def json = slurper.parseText(jsonString)
println 'Before Update...'
println new JsonBuilder(json).toPrettyString()
json[0].'RoleName' = 'Updated'
println 'After Update...'
println new JsonBuilder(json).toPrettyString()
The output:输出:
Before Update...
[
{
"RoleName": "Normal User",
"ModuleName": "Calendar Management"
},
{
"RoleName": "Reporter",
"ModuleName": "Incident Management"
}
]
After Update...
[
{
"RoleName": "Updated",
"ModuleName": "Calendar Management"
},
{
"RoleName": "Reporter",
"ModuleName": "Incident Management"
}
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.