简体   繁体   中英

Need help on replacing JSON field with constant String in Groovy

I want to replace a JSON field - JSONtoReplace by the below JSON strings

[{"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"}}

Groovy Script

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

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"
    }
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM