Jolt将一个json的子元素数组与父元素数组进行转换

kiayqfof  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(176)

我正在尝试转换一个在子元素中有数组元素的json。我想把它转换成所有json元素中都有父属性的父元素的json数组
我的输入json

{
    "Transmission": {
        "TransmissionGUID": "transId1",
        "Employer": [
            {
                "EmployerPartyID": "employerId1",
                "Employee": [
                    {
                        "EmployeeGenderCode": "M",
                        "MaritalStatusCode": "Married"

                    },
                    {
                        "EmployeeGenderCode": "F",
                        "MaritalStatusCode": "UnMarried"

                    }
                ]
            },
            {
                "EmployerPartyID": "employerId2",
                "Employee": [
                    {
                        "EmployeeGenderCode": "M2",
                        "MaritalStatusCode": "Married2"

                    },
                    {
                        "EmployeeGenderCode": "F2",
                        "MaritalStatusCode": "UnMarried"

                    }
                ]
            }
        ]
    }
}

字符串
我想把它变成

[
    {
        "Transmission": {
            "TransmissionGUID": "transId1",
            "Employer": [
                {
                    "EmployerPartyID": "employerId1",
                    "Employee": [
                        {
                            "EmployeeGenderCode": "M",
                            "MaritalStatusCode": "Married"
                        }
                    ]
                }
            ]
        }
    },
     {
        "Transmission": {
            "TransmissionGUID": "transId1",
            "Employer": [
                {
                    "EmployerPartyID": "employerId1",
                    "Employee": [
                        {
                            "EmployeeGenderCode": "F",
                            "MaritalStatusCode": "UnMarried"
                        }
                    ]
                }
            ]
        }
    },
    {
        "Transmission": {
            "TransmissionGUID": "transId1",
            "Employer": [
                {
                    "EmployerPartyID": "employerId2",
                    "Employee": [
                        {
                            "EmployeeGenderCode": "M2",
                            "MaritalStatusCode": "Married2"
                        }
                    ]
                }
            ]
        }
    },
    {
        "Transmission": {
            "TransmissionGUID": "transId1",
            "Employer": [
                {
                    "EmployerPartyID": "employerId",
                    "Employee": [
                        {
                            "EmployeeGenderCode": "F2",
                            "MaritalStatusCode": "UnMarried2"
                        }
                    ]
                }
            ]
        }
    }
]


我试着写了如下的转换的震动规格

[
    {
      "operation": "shift",
      "spec": {
        "Transmission": {
    
          "Employer": {
            "*": {
     
              "Employee": {
                "*": {
                    "@(4,TransmissionGUID)": "[].Transmission.TransmissionGUID",
                    "@(2,EmployerPartyID)": "[].Transmission.Employer[].EmployerPartyID",
                  "@": "[].Transmission.Employer[].Employee[]"
                  
                  
                }
              }
            }
          }
        }
      }
    }
  ]


但是它没有给出想要的输出。有人能帮我吗?

jfewjypa

jfewjypa1#

我不认为你可以用一个移位规范来完成这个转换。我可以用两个移位来完成,第一个移位将父子信息分组在唯一键下。唯一键是使用父\子数组中的子索引和父索引的组合创建的。例如,第一个雇主(索引0)有两个孩子,所以如果你用下划线(_)连接父索引和子索引,在中间你会得到一个唯一的键为每个父孩子为0_0 & 0_1.对于第二雇主的孩子它将1_0 & 1_1等等.一旦你已经分组每个父孩子在其唯一的键,第二个移位转换将基本上抓住每个父孩子和转储它在一个数组没有唯一的键,这应该给给予你需要的结果.

[
  {
    "operation": "shift",
    "spec": {
      "Transmission": {
        "Employer": {
          "*": {
            "Employee": {
              "*": {
                "@(4,TransmissionGUID)": "&3_&1.Transmission.TransmissionGUID",
                "@(2,EmployerPartyID)": "&3_&1.Transmission.Employer[#].EmployerPartyID",
                "@": "&3_&1.Transmission.Employer[#].Employee[]"
              }
            }
          }
        }
      }
    }
    },
  {
    "operation": "shift",
    "spec": {
      "*": "[]"
    }
  }
 ]

字符串

相关问题