给定一个输入数组,该数组包含包含各种关键点的对象和active_candidates_group个关键点,其中该关键点是候选对象的数组,创建一个新的输出数组,其中每个候选对象与来自父对象的关键点相结合.

请务必注意以下几点:

  1. 父对象中有数百个关键点,对它们进行硬编码并不理想.
  2. 父对象的关键点的顺序并不重要.
  3. 父对象和候选对象之间不应该有重复的关键点.

输入

[
  {
    "id": 123,
    "name": "foo",
    // { hundreds of other keys }
    "active_candidates_group": [
      {
        "person_id": 123,
        "person_name": "john"
        // { many fields specific to ‘candidate’ —no duplicates to parent expected }
      },
      {
        "person_id": 321,
        "person_name": "bob"
        // { many fields specific to ‘candidate’ —no duplicates to parent expected }
      }
    ]
  }
]

期望输出

[
   {
      "id": 123,
      "name": "foo",
      // { hundreds of other keys from parent }
      "person_id": 123,
      "person_name": "john"
      // { other candidate fields }
   },
   {
      "id": 123,
      "name": "foo",
      // { hundreds of other keys from parent }
      "person_id": 321,
      "person_name": "bob"
      // { other candidate fields }
   }
]

到目前为止,我try 的是:

[
  {
    "operation": "shift",
    "spec": {
      "*": { // Iterate for each object in the input array
        "active_candidates_group": { // Match 'active_candidates_group' key
          "*": { // Iterate for each object inside 'active_candidates_group'
            "@2": "&2[&1]", // Copy all keys from the parent object to the same index in the output array
            "*": "&2[&1].&" // Copy the current object to its corresponding index in the output array
          }
        }
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "active_candidates_group": {
        "*": {
          "active_candidates_group": ""
        }
      }
    }
  }
]

推荐答案

您离解决方案太近了,只需在两个不同的级别上使用"*": "[&1].&"来稍微扭曲一下,而不需要单独对属性进行硬编码,例如

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "active_candidates_group": {
          "*": {
            "@2": { "*": "[&1].&" }, // match at the deeper level
            "*": "[&1].&"
          }
        }
      }
    }
  },
  { // only purify for the array everyehere  
    "operation": "remove",
    "spec": {
      "*": {
        "active_candidates_group": ""
      }
    }
  }
]

站点http://jolt-demo.appspot.com/上的101

enter image description here

Json相关问答推荐

在Go中,当字段可以根据其他字段具有不同的类型时,什么是正确的方法来卸载JSON?

最新版本的Deneb在数据溢出时不支持滚动

盒子图显示不正确

无法使用Jolt变换在嵌套的JSON中提取值

如何在 terraform 输出中打印一组用户信息

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

如何使用 boost::json::value 调用无参数函数

N1QL 聚合查询 Couchbase

如何从 rails 中的 respond_to 方法生成 json?

将 ES6 类对象序列化为 JSON

如何通过 NSJSONSerialization 在 JSON 中包含空值?

在 Jersey 服务中使用 JSON 对象

如何使用 SwiftyJSON 将字符串转换为 JSON

有什么方法可以在 elasticsearch 服务器中导入 json 文件(包含 100 个文档).?

是否可以将数据写入本地 json 文件,除了Angular 之外什么都没有?

从 JSON 创建 Hashtable

ASP.NET Core API 仅返回列表的第一个结果

字符串格式 JSON 字符串给出 KeyError

JSON - 是否有任何 XML CDATA 类似功能?

使用 JSON 迭代器输入安全警告