在JOLT中循环遍历单个数组输出中的多个行项目

huangapple go评论72阅读模式
英文:

Looping through multiple line items in a single array output in JOLT

问题

{
  "data": {
    "filename": "EV10044.docx"
  }
}
英文:

I want to write a Jolt definition that loops through each object in my attachment array, and move the object to the data array. At the same time, it should map single object attachment nest, and move the object to the data object.

Currently my spec below is working when my attachment block is an array.

Input :

{
  "IntegrationEntities": {
    "integrationEntity": [
      {
        "integrationEntityHeader": {
          "attachments": {
            "attachment": [
              {
                "name": "EV10044.docx"
              },
              {
                "name": "Test1.txt"
              }
            ]
          }
        }
      }
    ]
  }
}

JOLT Spec :

[
  {
    "operation": "shift",
    "spec": {
      "IntegrationEntities": {
        "integrationEntity": {
          "*": {
            "integrationEntityHeader": {
              "attachments": {
                "attachment": {
                  "*": {
                    "name": "data[&1].filename"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
]

Current Output :

{
  "data": [
    {
      "filename": "EV10044.docx"
    },
    {
      "filename": "Test1.txt"
    }
  ]
}

I want to handle the scenario in a way where regardless if the attachment is an array or object it should give the input. Currently it fails if the attachment block is an object and gives the output as NULL.

IF Input is:

{
  "IntegrationEntities": {
    "integrationEntity": [
      {
        "integrationEntityHeader": {
          "attachments": {
            "attachment": {
              "name": "EV10044.docx"
            }
          }
        }
      }
    ]
  }
}

Desired Output:

{
  "data": {
    "filename": "EV10044.docx"
  }
}

答案1

得分: 1

你可以使用这个规范:

[
  {
    "operation": "shift",
    "spec": {
      "IntegrationEntities": {
        "integrationEntity": {
          "*": {
            "integrationEntityHeader": {
              "attachments": {
                "attachment": {
                  "name": "data.filename",
                  "*": {
                    "name": "data[&1].filename"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
]

我刚刚在你的规范中添加了以下行。所以如果在attachment中有name,则假定你有1个附件。如果找不到name,则假定你有一个数组,你的其他代码是正确的。

"name": "data.filename"
英文:

You can use this spec:

[
  {
    "operation": "shift",
    "spec": {
      "IntegrationEntities": {
        "integrationEntity": {
          "*": {
            "integrationEntityHeader": {
              "attachments": {
                "attachment": {
                  "name": "data.filename",
                  "*": {
                    "name": "data[&1].filename"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
]

I just added the below line to your spec. So if you have name in the attachment it is assumed that you have 1 attachment. If no name is found it is assumed you have an array and your other codes is correct.

"name": "data.filename",

答案2

得分: 1

一个选项是使用shift转换规范,其中对象通过路径表达式进行限定,数组与符号表达式**"*": {**一起构建,包括子索引,例如:

[
  {
    "operation": "shift",
    "spec": {
      "@IntegrationEntities.integrationEntity": {
        "*": {
          "@integrationEntityHeader.attachments.attachment": {
            "*": {
              "*": "data[].file&",
              "@1,name": "data.file&"
            }
          }
        }
      }
    }
  }
]

顺便说一下,如果您不需要将最内层的属性**name重命名为fieldname**,以下更简短的规范就足够了:

[
  {
    "operation": "shift",
    "spec": {
      "@IntegrationEntities.integrationEntity": {
        "*": {
          "@integrationEntityHeader.attachments.attachment": "data"
        }
      }
    }
  }
]
英文:

An option is to use a shift transformation spec in which the objects are qualified by path expressions and the arrays are constructed along with sub-indexes by symbolic expression "*": { such as

[
  {
    "operation": "shift",
    "spec": {
      "@IntegrationEntities.integrationEntity": {
        "*": { // indexes of the array
          "@integrationEntityHeader.attachments.attachment": {
            "*": { // indexes of the array
              "*": "data[].file&", // if "attachment" is an array
              "@1,name": "data.file&" // if "attachment" is an object
            }
          }
        }
      }
    }
  }
]

Btw, if you didn't need to rename the innermost attribute name to fieldname, the following shorter spec would suffice :

[
  {
    "operation": "shift",
    "spec": {
      "@IntegrationEntities.integrationEntity": {
        "*": {
          "@integrationEntityHeader.attachments.attachment": "data"
        }
      }
    }
  }
]

huangapple
  • 本文由 发表于 2023年2月8日 23:08:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75387752.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定