我有以下JSON输入,我希望使用Jolt进行转换:

{
  "data": {
    "tests": [
      {
        "id": 304674,
        "name": "Test Number One",
        "description": "First Test"
      },
      {
        "id": 12345,
        "name": "Test Number Two",
        "description": "Second test"
      }
    ]
  }
}

我希望输出重命名一些内容,然后添加一个名为Locations的新数组,从而产生以下所需的输出:

{
  "data": {
    "tests": [
      {
        "TestID": 304674,
        "Name": "Test Number One",
        "Description": "First Test",
        "locations": [
          "Paris",
          "London"
        ],
      },
      {
        "id": 12345,
        "name": "Test Number Two",
        "description": "Second test",
        "locations": [
          "Paris",
          "London"
        ],
      }
    ]
  }
}

我在apache-nifi上try 了以下jolt规范,但当我运行它时,我只能得到位置的"paris".

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "tests": {
          "*": {
            "id": "data.tests[&1].TestID",
            "name": "data.tests[&1].Name",
            "description": "data.tests[&1].Description"
          }
        }
      }
    }
  },
  {
    "operation": "modify-default-beta",
    "spec": {
      "data": {
        "tests": {
          "*": {
            "locations": ["Paris", "London"]
          }
        }
      }
    }
  }
]

推荐答案

到目前为止,这是一次不错的try .我可以补充以下观点:

  • 不需要重复文字,但使用与号符号,如&2(eg.:going two levels up the tree and grabbing the literal 100)、&3(data)
  • &(0,1)代表水平流第1次出现星号(第0次)
  • 可以在Shift变换中直接添加"locations": ["Paris", "London"](不需要额外的变换),同时重命名当前对象的属性

因此,您可以使用以下规范:

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "tests": {
          "*": {
            "id": "&3.&2[&1].TestID",
            "n*": "&3.&2[&1].N&(0,1)",
            "d*": "&3.&2[&1].D&(0,1)",
            "#Paris|#London": "&3.&2[&1].locations"
          }
        }
      }
    }
  }
]

Json相关问答推荐

Jolt转换问题—使用键查找匹配对象

在scala中将字符串列转换为 struct 的JSON映射

如何在对象投影(*)上应用滤镜投影([?port==`eth1`])?

JQ如何获取特定子元素的所有父母

在Vega中如何通过滑块改变条形图的宽度

使用JQ合并JSON列表中的对象

jq 对特定键进行过滤并将值整理到单个 csv 单元格中

将 std::可选值存储到 json 文件 C++

使用动态语言jQuery:根据匹配模式提取与其他值匹配的值

如何使用 jq 在连续的 json 记录流上调用操作

如何在 Flutter 中遍历嵌套的动态 JSON 文件

Vue 3如何将参数作为json发送到axios get

PowerShell - JSON/PsCustomObject - 为什么我的数组被扁平化为一个对象?

没有很多类的 GSON 解析

在 C# 中解析 Json rest api 响应

使用杰克逊创建一个 json 对象

将序列化表单的数据转换为 json 对象

将 javascript 对象或数组转换为 json 以获取 ajax 数据

NSManagedObject 属性值的 NSNull 处理

在 .NET 中缩小缩进的 JSON 字符串