如何在另一个数组内操作数组?

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

How to work with an array inside of another array?

问题

以下是您提供的内容的翻译,我将只翻译其中的文本部分,不包括代码:

我在处理 JSON 文档中的嵌套数组时遇到问题。我需要获取一个包含“CandidateEmail”和“ApplicationId”以及“JobRefNumber”的 JSON 文档的数组。我认为下面的示例将有助于您更好地理解。

我的输入是:

{
  "Content": [
    {
      "CandidateEmail": "john1@noexist.com",
      "Applications": [
        {
          "ApplicationId": "app1",
          "JobRefNumber": "REF1"
        },
        {
          "ApplicationId": "app2",
          "JobRefNumber": "REF2"
        }
      ]
    },
    {
      "CandidateEmail": "carl2@email.com",
      "Applications": [
        {
          "ApplicationId": "app3",
          "JobRefNumber": "REF3"
        },
        {
          "ApplicationId": "app4",
          "JobRefNumber": "REF4"
        }
      ]
    }
  ]
}

预期的输出是:

[
  {
    "CandidateEmail": "john1@noexist.com",
    "ApplicationId": "app1",
    "JobRefNumber": "REF1"
  },
  {
    "CandidateEmail": "john1@noexist.com",
    "ApplicationId": "app2",
    "JobRefNumber": "REF2"
  },
  {
    "CandidateEmail": "carl2@email.com",
    "ApplicationId": "app3",
    "JobRefNumber": "REF3"
  },
  {
    "CandidateEmail": "carl2@email.com",
    "ApplicationId": "app4",
    "JobRefNumber": "REF4"
  }
]

我不知道这是否有所帮助,但以下是我目前已经起草的规范:

[
  {
    "operation": "shift",
    "spec": {
      "Content": {
        "*": {
          "Applications": {
            "*": {
              "@(2,CandidateEmail)": "[&1].CandidateEmail",
              "ApplicationId": "[&1].ApplicationId",
              "JobRefNumber": "[&1].JobRefNumber"
            }
          }
        }
      }
    }
  }
]
英文:

I'm having problems while working with a nested array inside another array on a JSON document. I need to obtain an array containing one JSON document with the "CandidateEmail" and the "ApplicationId" "JobRefNumber". I think that the example below will help you understand better.

My input is:

{
  "Content": [
    {
      "CandidateEmail": "john1@noexist.com",
      "Applications": [
        {
          "ApplicationId": "app1",
          "JobRefNumber": "REF1"
        },
        {
          "ApplicationId": "app2",
          "JobRefNumber": "REF2"
        }
      ]
    },
    {
      "CandidateEmail": "carl2@email.com",
      "Applications": [
        {
          "ApplicationId": "app3",
          "JobRefNumber": "REF3"
        },
        {
          "ApplicationId": "app4",
          "JobRefNumber": "REF4"
        }
      ]
    }
  ]
}

The expected output is:

[
  {
    "CandidateEmail": "john1@noexist.com",
    "ApplicationId": "app1",
    "JobRefNumber": "REF1"
  },
  {
    "CandidateEmail": "john1@noexist.com",
    "ApplicationId": "app2",
    "JobRefNumber": "REF2"
  },
  {
    "CandidateEmail": "carl2@email.com",
    "ApplicationId": "app3",
    "JobRefNumber": "REF3"
  },
  {
    "CandidateEmail": "carl2@email.com",
    "ApplicationId": "app4",
    "JobRefNumber": "REF4"
  }
]

I don't know if it will be of any help, but here is the spec I've been able to draft so far:

[
  {
    "operation": "shift",
    "spec": {
      "Content": {
        "*": {
          "Applications": {
            "*": {
              "@(2,CandidateEmail)": "[&1].CandidateEmail",
              "ApplicationId": "[&1].ApplicationId",
              "JobRefNumber": "[&1].JobRefNumber"
            }
          }
        }
      }
    }
  }
]

答案1

得分: 1

你可以使用shift转换规范,例如

[
  {
    "operation": "shift",
    "spec": {
      "Content": {
        "*": {
          "Appl*": {
            "*": {
              "@2,CandidateEmail": "&3[#2].CandidateEmail", // 可以沿树向上两级以达到CandidateEmail级别
              "*": "&3[#2].&" // 通过使用[#2]和&3区分"Applications"和"Content"数组的索引。使用&复制键,使用*通配符复制内部最深层对象中的所有属性。
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {// 去掉包装数组和对象的键
      "*": {
        "*": ""
      }
    }
  }
]
英文:

You can use a shift transformation spec such as

[
  {
    "operation": "shift",
    "spec": {
      "Content": {
        "*": {
          "Appl*": {
            "*": {
              "@2,CandidateEmail": "&3[#2].CandidateEmail", // you an go two levels up the tree to reach the level of CandidateEmail 
              "*": "&3[#2].&" // distinguish by indexes of "Applications" and "Content" arrays through use of [#2] and &3 respectively. Replicate the keys by using &, and all attributes nested within innermost objects by using * wildcards.
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {// get rid of the keys of wrapper arrays and objects
      "*": {
        "*": ""
      }
    }
  }
]

huangapple
  • 本文由 发表于 2023年2月7日 04:33:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75366290.html
匿名

发表评论

匿名网友

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

确定