使用 Groovy 脚本从 JSON 有效载荷中删除一个块
Remove a block from JSON payload using Groovy script
请找到一个有效负载示例,我在其中尝试根据更新日期删除一个块。我正在寻找更新日期距当前日期不到一周的消息块。
[
{
"firstName": "person1",
"lastName": "lname",
"createdDate": "2021-11-18T03:08:50.000Z",
"externalId": null,
"id": "123",
"updatedDate": "2022-03-18T19:33:28.792Z",
"title": "XXXX",
"email": "123@gmail.com",
"enabled": true
},
{
"firstName": "P2",
"lastName": "lname2",
"createdDate": "2021-10-26T19:12:16.000Z",
"externalId": null,
"id": "125",
"updatedDate": "2022-03-01T18:01:19.762Z",
"title": ".",
"email": "125@gmail.com",
"enabled": true
}
]
使用下面的代码得到想要的结果。 remove 语句似乎没有从 body1
中删除集合,或者我这里有问题。尝试将 body1
添加到邮件正文中,但不起作用。尝试使用构建器,这给出了一个完全不同的错误。任何帮助将不胜感激。
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap; import groovy.json.*;
import groovy.json.JsonSlurper;
import groovy.json.JsonBuilder
def Message processData(Message message) {
//Body
def body = message.getBody(java.lang.String) as String;
def body1 = new JsonSlurper().parseText(body)
def jsonOp
def builder = new JsonBuilder()
def today = new Date()
def targetDate = today - 7
pastweek = (targetDate.format("yyyy-MM-dd\'T\'HH:mm:ss'Z'"))
body1.collect.eachWithIndex { it,index->
if ( it.updatedDate < pastweek )
{
it.remove(index)
}
else {
builder {
'firstName' it.firstName
'lastName' it.lastName
'email' it.email
'updatedDate' it.updatedDate
'enabled' it.enabled
}
}
}
def finalJson = builder.toPrettyString()
message.setBody(finalJson.toString())
return message;
}
import groovy.json.*
def Message processData(Message message) {
def body = message.getBody(java.lang.String) as String
def data = new JsonSlurper().parseText(body)
def pastweek = (new Date()-7).format("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
data.removeAll{it.updatedDate < pastweek}
message.setBody(new JsonBuilder(data).toPrettyString())
return message
}
请找到一个有效负载示例,我在其中尝试根据更新日期删除一个块。我正在寻找更新日期距当前日期不到一周的消息块。
[
{
"firstName": "person1",
"lastName": "lname",
"createdDate": "2021-11-18T03:08:50.000Z",
"externalId": null,
"id": "123",
"updatedDate": "2022-03-18T19:33:28.792Z",
"title": "XXXX",
"email": "123@gmail.com",
"enabled": true
},
{
"firstName": "P2",
"lastName": "lname2",
"createdDate": "2021-10-26T19:12:16.000Z",
"externalId": null,
"id": "125",
"updatedDate": "2022-03-01T18:01:19.762Z",
"title": ".",
"email": "125@gmail.com",
"enabled": true
}
]
使用下面的代码得到想要的结果。 remove 语句似乎没有从 body1
中删除集合,或者我这里有问题。尝试将 body1
添加到邮件正文中,但不起作用。尝试使用构建器,这给出了一个完全不同的错误。任何帮助将不胜感激。
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap; import groovy.json.*;
import groovy.json.JsonSlurper;
import groovy.json.JsonBuilder
def Message processData(Message message) {
//Body
def body = message.getBody(java.lang.String) as String;
def body1 = new JsonSlurper().parseText(body)
def jsonOp
def builder = new JsonBuilder()
def today = new Date()
def targetDate = today - 7
pastweek = (targetDate.format("yyyy-MM-dd\'T\'HH:mm:ss'Z'"))
body1.collect.eachWithIndex { it,index->
if ( it.updatedDate < pastweek )
{
it.remove(index)
}
else {
builder {
'firstName' it.firstName
'lastName' it.lastName
'email' it.email
'updatedDate' it.updatedDate
'enabled' it.enabled
}
}
}
def finalJson = builder.toPrettyString()
message.setBody(finalJson.toString())
return message;
}
import groovy.json.*
def Message processData(Message message) {
def body = message.getBody(java.lang.String) as String
def data = new JsonSlurper().parseText(body)
def pastweek = (new Date()-7).format("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
data.removeAll{it.updatedDate < pastweek}
message.setBody(new JsonBuilder(data).toPrettyString())
return message
}