java - 在 Jolt 中向数组添加默认值

标签 java json jolt

我正在尝试使用 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 并且在文档中没有找到任何内容。有人可以帮我解决这个问题吗?

提前谢谢您!

最佳答案

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

[
  {
   // 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].&"
    }
  }
]

enter image description here

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

[
  {
   // 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"
        }
      }
    }
  }
]

enter image description here

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

关于java - 在 Jolt 中向数组添加默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70808743/

相关文章:

java - 使用 Apache Beam Java SDK 读取 Parquet 文件,无需提供架构

json - 我如何在 Dart 中对集合进行 JSON.stringify

java - 使用SearchView到ListView进行搜索(关于JSON)

java - 我正在尝试为 jolt 变换创建一个 json,但我陷入了嵌套数组格式

java - 使用 Jolt 变换对象数组

java - 将 java web 应用程序部署到 heroku : NoClassDefFoundError caused by ClassNotFoundException

java - 使用 spring mvc 在浏览器上显示欢迎消息

json - Apache NiFi 中的 JSON Jolt 支持 concat

java - SAP CF XSUAA 服务 - 错误消息 : Invalid redirect <redirect_uri>/login/callback did not matched the registered values

java - Jackson - 反序列化为不同类型