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

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

How to work with an array inside of another array?

问题

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

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

我的输入是:

  1. {
  2. "Content": [
  3. {
  4. "CandidateEmail": "john1@noexist.com",
  5. "Applications": [
  6. {
  7. "ApplicationId": "app1",
  8. "JobRefNumber": "REF1"
  9. },
  10. {
  11. "ApplicationId": "app2",
  12. "JobRefNumber": "REF2"
  13. }
  14. ]
  15. },
  16. {
  17. "CandidateEmail": "carl2@email.com",
  18. "Applications": [
  19. {
  20. "ApplicationId": "app3",
  21. "JobRefNumber": "REF3"
  22. },
  23. {
  24. "ApplicationId": "app4",
  25. "JobRefNumber": "REF4"
  26. }
  27. ]
  28. }
  29. ]
  30. }

预期的输出是:

  1. [
  2. {
  3. "CandidateEmail": "john1@noexist.com",
  4. "ApplicationId": "app1",
  5. "JobRefNumber": "REF1"
  6. },
  7. {
  8. "CandidateEmail": "john1@noexist.com",
  9. "ApplicationId": "app2",
  10. "JobRefNumber": "REF2"
  11. },
  12. {
  13. "CandidateEmail": "carl2@email.com",
  14. "ApplicationId": "app3",
  15. "JobRefNumber": "REF3"
  16. },
  17. {
  18. "CandidateEmail": "carl2@email.com",
  19. "ApplicationId": "app4",
  20. "JobRefNumber": "REF4"
  21. }
  22. ]

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

  1. [
  2. {
  3. "operation": "shift",
  4. "spec": {
  5. "Content": {
  6. "*": {
  7. "Applications": {
  8. "*": {
  9. "@(2,CandidateEmail)": "[&1].CandidateEmail",
  10. "ApplicationId": "[&1].ApplicationId",
  11. "JobRefNumber": "[&1].JobRefNumber"
  12. }
  13. }
  14. }
  15. }
  16. }
  17. }
  18. ]
英文:

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:

  1. {
  2. "Content": [
  3. {
  4. "CandidateEmail": "john1@noexist.com",
  5. "Applications": [
  6. {
  7. "ApplicationId": "app1",
  8. "JobRefNumber": "REF1"
  9. },
  10. {
  11. "ApplicationId": "app2",
  12. "JobRefNumber": "REF2"
  13. }
  14. ]
  15. },
  16. {
  17. "CandidateEmail": "carl2@email.com",
  18. "Applications": [
  19. {
  20. "ApplicationId": "app3",
  21. "JobRefNumber": "REF3"
  22. },
  23. {
  24. "ApplicationId": "app4",
  25. "JobRefNumber": "REF4"
  26. }
  27. ]
  28. }
  29. ]
  30. }

The expected output is:

  1. [
  2. {
  3. "CandidateEmail": "john1@noexist.com",
  4. "ApplicationId": "app1",
  5. "JobRefNumber": "REF1"
  6. },
  7. {
  8. "CandidateEmail": "john1@noexist.com",
  9. "ApplicationId": "app2",
  10. "JobRefNumber": "REF2"
  11. },
  12. {
  13. "CandidateEmail": "carl2@email.com",
  14. "ApplicationId": "app3",
  15. "JobRefNumber": "REF3"
  16. },
  17. {
  18. "CandidateEmail": "carl2@email.com",
  19. "ApplicationId": "app4",
  20. "JobRefNumber": "REF4"
  21. }
  22. ]

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

  1. [
  2. {
  3. "operation": "shift",
  4. "spec": {
  5. "Content": {
  6. "*": {
  7. "Applications": {
  8. "*": {
  9. "@(2,CandidateEmail)": "[&1].CandidateEmail",
  10. "ApplicationId": "[&1].ApplicationId",
  11. "JobRefNumber": "[&1].JobRefNumber"
  12. }
  13. }
  14. }
  15. }
  16. }
  17. }
  18. ]

答案1

得分: 1

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

  1. [
  2. {
  3. "operation": "shift",
  4. "spec": {
  5. "Content": {
  6. "*": {
  7. "Appl*": {
  8. "*": {
  9. "@2,CandidateEmail": "&3[#2].CandidateEmail", // 可以沿树向上两级以达到CandidateEmail级别
  10. "*": "&3[#2].&" // 通过使用[#2]和&3区分"Applications"和"Content"数组的索引。使用&复制键,使用*通配符复制内部最深层对象中的所有属性。
  11. }
  12. }
  13. }
  14. }
  15. }
  16. },
  17. {
  18. "operation": "shift",
  19. "spec": {// 去掉包装数组和对象的键
  20. "*": {
  21. "*": ""
  22. }
  23. }
  24. }
  25. ]
英文:

You can use a shift transformation spec such as

  1. [
  2. {
  3. "operation": "shift",
  4. "spec": {
  5. "Content": {
  6. "*": {
  7. "Appl*": {
  8. "*": {
  9. "@2,CandidateEmail": "&3[#2].CandidateEmail", // you an go two levels up the tree to reach the level of CandidateEmail
  10. "*": "&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.
  11. }
  12. }
  13. }
  14. }
  15. }
  16. },
  17. {
  18. "operation": "shift",
  19. "spec": {// get rid of the keys of wrapper arrays and objects
  20. "*": {
  21. "*": ""
  22. }
  23. }
  24. }
  25. ]

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:

确定