通过 Jolt 将 JSON 数组转换为新的 JSON 对象并保持对象分离

问题描述 投票:0回答:1

我有一个 JSON 对象数组,需要将其转换为与我的架构一致的新 JSON 对象数组。我是 Jolt 新手,我的输出不是我需要的。

我尝试了多种方法,但未能得到我想要的结果。

这是我的输入 JSON:

{
   "totalApplication":{
      "applicationName":"MSOffice",
      "products":[
         {
            "productName":"Yearly",
            "userList":[
               {
                  "userId":"3544353",
                  "email":"[email protected]",
                  "userName":"Joe Hashashi",
                  "lastActive":"1705088660"
               },
               {
                  "userId":"3544354",
                  "email":"[email protected]",
                  "userName":"Jacob Martin",
                  "lastActive":"1705088661"
               }
            ]
         },
         {
            "productName":"Monthly",
            "userList":[
               {
                  "userId":"4545455",
                  "email":"[email protected]",
                  "userName":"Amanda Kore",
                  "lastActive":"1705088662"
               },
               {
                  "userId":"4545456",
                  "email":"[email protected]",
                  "userName":"Leo Biz",
                  "lastActive":"1705088663"
               }
            ]
         }
      ]
   }
}

这是我尝试过的 Jolt 规格:

[
  {
    "operation": "shift",
    "spec": {
      "totalApplication": {
        "products": {
          "*": {
            "userList": {
              "*": {
                "userName": "[&3].name",
                "email": "[&3].email",
                "userId": "[&3].userId",
                "@(2,productName)": "[&3].additionalAttribute.planName",
                "lastActive": "[&3].additionalAttribute.lastActive",
                "@(4,applicationName)": "[&3].additionalAttribute.applicationName"
              }
            }
          }
        }
      }
    }
 }
]

根据上述规格,我得到以下结果:

[ {
  "additionalAttribute" : {
    "planName" : [ "Yearly", "Yearly" ],
    "applicationName" : [ "MSOffice", "MSOffice" ],
    "lastActive" : [ "1705088660", "1705088661" ]
  },
  "name" : [ "Joe Hashashi", "Jacob Martin" ],
  "email" : [ "[email protected]", "[email protected]" ],
  "userId" : [ "3544353", "3544354" ]
}, {
  "additionalAttribute" : {
    "planName" : [ "Monthly", "Monthly" ],
    "applicationName" : [ "MSOffice", "MSOffice" ],
    "lastActive" : [ "1705088662", "1705088663" ]
  },
  "name" : [ "Amanda Kore", "Leo Biz" ],
  "email" : [ "[email protected]", "[email protected]" ],
  "userId" : [ "4545455", "4545456" ]
} ]

我想要这个所需的输出:

[
   {
      "name":"Joe Hashashi",
      "email":"[email protected]",
      "userId":"3544353",
      "additionalAttribute":{
         "lastActive":"1705088660",
         "productName":"Yearly",
         "applicationName":"MSOffice"
      }
   },
   {
      "name":"Jacob Martin",
      "email":"[email protected]",
      "userId":"3544354",
      "additionalAttribute":{
         "lastActive":"1705088661",
         "productName":"Yearly",
         "applicationName":"MSOffice"
      }
   },
   {
      "name":"Amanda Kore",
      "email":"[email protected]",
      "userId":"4545455",
      "additionalAttribute":{
         "lastActive":"1705088662",
         "productName":"Monthly",
         "applicationName":"MSOffice"
      }
   },
   {
      "name":"Leo Biz",
      "email":"[email protected]",
      "userId":"4545456",
      "additionalAttribute":{
         "lastActive":"1705088663",
         "productName":"Monthly",
         "applicationName":"MSOffice"
      }
   }
]

它没有给出所需的输出。谁能帮我解决这个问题吗?

java json transformation jolt
1个回答
0
投票

这个规范应该适合你

[
  {
    "operation": "shift",
    "spec": {
      "totalApplication": {
        "products": {
          "*": {
            "userList": {
              "*": {
                "userName": "@(1,userId).name",
                "email": "@(1,userId).email",
                "userId": "@(1,userId).userId",
                "lastActive": "@(1,userId).additinalAttribute.lastActive",
                "@(2,productName)": "@(1,userId).additinalAttribute.productName",
                "@(4,applicationName)": "@(1,userId).additinalAttribute.applicationName"
              }
            }
          }
        }
      }
    }
 },
  {
    "operation": "shift",
    "spec": {
      "*": "[]"
    }
  }
]
© www.soinside.com 2019 - 2024. All rights reserved.