【问题标题】:Jolt transform: extract nested array from an objectJolt 变换:从对象中提取嵌套数组
【发布时间】:2021-10-19 13:51:22
【问题描述】:

我有如下 JSON 输入

{
  "data": [
    {
      "items": [
        {
          "item": "R11",
          "code": "8611A"
        },
        {
          "item": "R2",
          "code": "8611B"
        }
      ]
    }
  ]
}

我需要提取项目数组如下

[
  {
    "item": "R11",
    "code": "8611A"
  },
  {
    "item": "R2",
    "code": "8611B"
  }
]

请复制粘贴上面的INPUT和OUTPUThttps://jolt-demo.appspot.com/#inception 我已经测试过[{"operation":"shift","spec":{"data":{"*":""}}}] 但它返回{"items" : [ {...}, {...} ] }

【问题讨论】:

    标签: java json transform jolt


    【解决方案1】:

    您可以使用 shift 转换,如下所示

    [
      {
        "operation": "shift",
        "spec": {
          "*": { //might be replaced with "data"
            "*": {
              "*": //might be replaced with "items"
              { 
                "@": "" 
              }
            }
          }
        }
      }
    ]
    

    星号通配符分别代表数组data、其索引和数组items"@":""items 表示为没有键的对象数组。

    【讨论】:

    • 感谢@Barbaros,它成功了。经过多次尝试,我又找到了另一种选择[{"operation":"shift","spec":{"data":{"*":{"items":""}}}}]
    • 不客气@SR,是的,这也可以用作替代方案。
    【解决方案2】:

    替代规格:

    [
      {
        "operation": "shift",
        "spec": {
          "data": {
            "*": {
              "items": ""
            }
          }
        }
      }
    ]
    

    【讨论】:

      【解决方案3】:

      这就是答案

      [{
        "operation": "shift",
        "spec": {
          "data": {
            "0": {
              "*": ""
            }
          }
        }
      }]
      

      【讨论】:

        猜你喜欢
        • 2023-03-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多