需要在groovy中用恒定字符串替换JSON字段的帮助

发布于 2025-02-02 02:58:36 字数 1764 浏览 2 评论 0 原文

我想替换一个JSON领域-Json Strings

[{"RoleName": "Normal User","ModuleName": "Calendar Management"},
 {"RoleName": "Reporter","ModuleName": "Incident Management"},
 {"RoleName": "Viewer","ModuleName": "ESG"},
 {"RoleName": "Viewer","ModuleName": "Apps"}]

源json json hody {“ personInformation”:{“ emailAddress”:“ ” ,“名称”:{“ firstName”:“ fabian”,“ lastname”:“ koehlmann”,“ preferredname”:“fabiankã¶hlmann”},address“:{” adversionLine1“:” berllin gmbh“ “:”eRasmusstraã²20,,,,,“邮政编码”:“ 10553”},“员工信息”:{“ losplyepy型”:“内部”,“ homploymentStatus”:“ active”,“ active”,“ jobrole”,“ jobrole”:{“decr疑:“财务 - it”,“ isCreateDepartment”:“ null”,“ jobtitle”:“专家IT安全& Infrastructure”,“ susterisorid”:“ null”,“ startdate”:“ 09/29/2021”,“ “:“ null”},“位置”:“柏林GmbH”,“职业”:“ null”},“ Isuser”:“ true”,“ userInformation”:{“ userId”:“ 7FA1785F-F4F4F3-42B5-96BD -33F195521635“,“状态”:“活动”,“位置”:{“ scope”:“ trn-loc”,“角色”,“ jsontoreplace”}},“ recorduid”:“ 2022/05/27”,“ id“:” A0201412“}}}

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 - 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":"[email protected]","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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡淡绿茶香 2025-02-09 02:58:36

我想替换JSON字段

您可以做这样的事情:

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()

输出:

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

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"
    }
]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文