将父对象中的所有键应用于数组中的所有对象的Jolt规范。

huangapple go评论64阅读模式
英文:

Jolt spec to apply all keys in a parent object to all objects in an array

问题

Here is the translation of the code part you provided:

给定一个包含各种键的输入数组和一个 active_candidates_group 键,该键是候选对象的数组,创建一个新的输出数组,其中每个候选对象与父对象的键组合在一起。

需要注意以下几点:

  1. 父对象中有数百个键,最好不要硬编码它们。
  2. 父对象的键的顺序不重要。
  3. 父对象和候选对象之间不应有重复的键。

输入

[
  {
    "id": 123,
    "name": "foo",
    // { 数百个其他键 }
    "active_candidates_group": [
      {
        "person_id": 123,
        "person_name": "john"
        // { 与‘候选’相关的许多字段 ——不希望与父对象重复 }
      },
      {
        "person_id": 321,
        "person_name": "bob"
        // { 与‘候选’相关的许多字段 ——不希望与父对象重复 }
      }
    ]
  }
]

期望的输出

[
   {
      "id": 123,
      "name": "foo",
      // { 来自父对象的数百个其他键 }
      "person_id": 123,
      "person_name": "john"
      // { 其他候选字段 }
   },
   {
      "id": 123,
      "name": "foo",
      // { 来自父对象的数百个其他键 }
      "person_id": 321,
      "person_name": "bob"
      // { 其他候选字段 }
   }
]

我已翻译了代码部分,如果需要进一步解释或帮助,请随时告诉我。

英文:

Given an input array with objects containing various keys and an active_candidates_group key, with this key being an array of candidate objects, create a new output array where each candidate object is combined with the keys from the parent object.

It is important to note the following points:

  1. There are hundreds of keys in the parent object and it's not ideal to hard-code them.
  2. The parent object's keys' order does not matter.
  3. There should be no duplicate keys between the parent object and the candidate objects.

Input

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

Desired Output

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

What I tried so far:

[
  {
    "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

得分: 2

你离解决方案很近了,只需在两个不同级别使用"*": "[&1].&"稍微扭曲一下,而不需要硬编码单独的属性,如下所示:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "active_candidates_group": {
          "*": {
            "@2": { "*": "[&1].&" }, // 在更深的级别匹配
            "*": "[&1].&"
          }
        }
      }
    }
  },
  { // 仅对数组进行清除处理
    "operation": "remove",
    "spec": {
      "*": {
        "active_candidates_group": ""
      }
    }
  }
]

在网站http://jolt-demo.appspot.com/上的演示如下:

将父对象中的所有键应用于数组中的所有对象的Jolt规范。

英文:

You are so close to the solution, just distort a bit through use of "*": "[&1].&" at two different level without hardcoding the attributes individually such as

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

the demo on the site http://jolt-demo.appspot.com/ is

将父对象中的所有键应用于数组中的所有对象的Jolt规范。

huangapple
  • 本文由 发表于 2023年5月22日 23:08:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76307564.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定