我想编写一个 Jolt 定义,循环遍历附件数组中的每个对象,并将该对象移动到数据数组。同时,映射单个对象附件嵌套,并将对象移动到数据对象。
目前,当我的附件 block 是一个数组时,我的下面的规范正在工作。
输入:
{
"IntegrationEntities": {
"integrationEntity": [
{
"integrationEntityHeader": {
"attachments": {
"attachment": [
{
"name": "EV10044.docx"
},
{
"name": "Test1.txt"
}
]
}
}
}
]
}
}
JOLT 规范:
[
{
"operation": "shift",
"spec": {
"IntegrationEntities": {
"integrationEntity": {
"*": {
"integrationEntityHeader": {
"attachments": {
"attachment": {
"*": {
"name": "data[&1].filename"
}
}
}
}
}
}
}
}
}
]
电流输出:
{
"data": [
{
"filename": "EV10044.docx"
},
{
"filename": "Test1.txt"
}
]
}
我想以某种方式处理这种情况,无论附件是数组还是对象,它都应该提供输入。 目前,如果附件 block 是一个对象,并且输出为 NULL,则它会失败。
如果输入是:
{
"IntegrationEntities": {
"integrationEntity": [
{
"integrationEntityHeader": {
"attachments": {
"attachment": {
"name": "EV10044.docx"
}
}
}
}
]
}
}
所需输出:
{
"data": {
"filename": "EV10044.docx"
}
}
最佳答案
您可以使用此规范:
[
{
"operation": "shift",
"spec": {
"IntegrationEntities": {
"integrationEntity": {
"*": {
"integrationEntityHeader": {
"attachments": {
"attachment": {
"name": "data.filename",
"*": {
"name": "data[&1].filename"
}
}
}
}
}
}
}
}
}
]
我刚刚将以下行添加到您的规范中。因此,如果您的附件
中有name
,则假定您有 1 个附件。如果没有找到name
,则假定您有一个数组并且其他代码是正确的。
"name": "data.filename",
关于arrays - 在 JOLT 中循环访问单个数组输出中的多个行项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75387752/