json 如何在一个数组中使用另一个数组?

yk9xbfzb  于 2023-02-10  发布在  其他
关注(0)|答案(1)|浏览(121)

我在处理JSON文档中另一个数组中的嵌套数组时遇到了问题。我需要获取一个包含一个JSON文档的数组,该文档具有"CandidateEmail"和"ApplicationId""JobRefNumber"。我认为下面的示例将帮助您更好地理解。
我的意见是:

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

62lalag41#

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

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

相关问题