【问题标题】:Adding default values to array in Jolt在 Jolt 中为数组添加默认值
【发布时间】:2022-01-21 23:36:27
【问题描述】:

我正在尝试使用 jolt 转换 json,它只是向数组添加一些默认值。

输入看起来像这样:

{
  "valueA": "A",
  "valueB": "B"
}

输出应该是:

{
  "listWithItems": [ 
    {
      "valA": "A",
      "valB": "B",
      "valC": "C"
    }
  ]
}

我的规范现在看起来:

[
  {
    "operation": "shift",
    "spec": {
      "valueA": "listWithItems[0].valA",
      "valueB": "listWithItems[0].valB"
    }
  },
  {
    "operation": "default",
    "spec": {
      "listWithItems": [
        {
          "valC": "valC"
        }
      ]
    }
  }
]

我只是无法将 valC 传递给 listWithItems 并且在文档中没有找到任何内容。有人可以帮我解决这个问题吗?

提前谢谢你!

【问题讨论】:

    标签: java json jolt


    【解决方案1】:

    一种选择是改变转换的顺序,例如

    [
      {
       // Add a default attribute "C" to the current object
        "operation": "default",
        "spec": {
          "valueC": "C"
        }
      },
      {
       // Nest all children of the object under "listWithItems" key name as an array
        "operation": "shift",
        "spec": {
          "*": "listWithItems[0].&"
        }
      }
    ]
    

    另一个选项使用 modify 转换,例如

    [
      {
       // Nest all of the current elements of the object under the "listWithItems" array 
        "operation": "shift",
        "spec": {
          "*": "listWithItems[0].&"
        }
      },
      {
       // Add new key-value pair to the array
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "*": {
              "valueC": "C"
            }
          }
        }
      }
    ]
    

    您可以查看https://github.com/bazaarvoice/jolt/releases/ 作为来源和https://jolt-demo.appspot.com/ 作为游乐场。

    【讨论】:

    • 感谢 Barbados Ozhan 我现在可以单独挑选它们了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 2014-01-31
    • 2021-12-22
    相关资源
    最近更新 更多