2017-08-24 43 views
0

我有了該平坦結構的JSON:顛簸變換嵌套分組

[{ 
    "PK": "1111", 
    "SOURCE_DB": "Oracle", 
    "CONTACT_TYPE": "Phone", 
    "CONTACT_SUBTYPE": "Work", 
    "EMAIL": null 
    "PHONE_COUNTRY_CODE": "44", 
    "PHONE_NUMBER": "12345678", 
    "PHONE_EXT": "907643", 
    "STATUS": "Active" 
    }, { 
    "PK": "1111", 
    "SOURCE_DB": "Oracle", 
    "CONTACT_TYPE": "Phone", 
    "CONTACT_SUBTYPE": "Home", 
    "EMAIL": null 
    "PHONE_COUNTRY_CODE": "353", 
    "PHONE_NUMBER": "87654321", 
    "PHONE_EXT": null, 
    "STATUS": "Active" 
    }, { 
    "PK": "1111", 
    "SOURCE_DB": "", 
    "CONTACT_TYPE": "Email", 
    "CONTACT_SUBTYPE": "Personal", 
    "EMAIL": "[email protected]" 
    "PHONE_COUNTRY_CODE": null, 
    "PHONE_NUMBER": null, 
    "PHONE_EXT": null, 
    "STATUS": "Active" 
    }, 
    { 
    "PK": "2222", 
    "SOURCE_DB": "DB2", 
    "CONTACT_TYPE": "Phone", 
    "CONTACT_SUBTYPE": "Home", 
    "EMAIL": null 
    "PHONE_COUNTRY_CODE": "44", 
    "PHONE_NUMBER": "98761234", 
    "PHONE_EXT": null, 
    "STATUS": "Inactive" 
    }, { 
    "PK": "2222", 
    "SOURCE_DB": "DB2", 
    "CONTACT_TYPE": "Email", 
    "CONTACT_SUBTYPE": "Work", 
    "EMAIL": "[email protected]" 
    "PHONE_COUNTRY_CODE": null, 
    "PHONE_NUMBER": null, 
    "PHONE_EXT": null, 
    "STATUS": "Active" 
    } 
] 

然後,我想將它們組,首先由密鑰(PK),則內的每個條目,ContactMethods將被分組在一起。這是輸出:

{ 
    "Accounts": [{ 
      "Reference": { 
       "Key": "1111", 
       "System": "Oracle" 
      }, 
      "ContactMethods": { 
       "Phone": [{ 
         "Subtype": "Work", 
         "CountryCode": "44", 
         "Number": "12345678", 
         "Extension": "907643", 
         "Active": true 
        }, { 
         "Subtype": "Home", 
         "CountryCode": "353", 
         "Number": "87654321", 
         "Extension": null, 
         "Active": true 
        } 
       ], 
       "Email": [{ 
         "Subtype": "Personal", 
         "EmailAddress": "[email protected]", 
         "Active": true 
        } 
       ] 
      } 
     }, { 
      "Reference": { 
       "Key": "2222", 
       "System": "DB2" 
      }, 
      "ContactMethods": { 
       "Phone": [{ 
         "Subtype": "Home", 
         "CountryCode": "44", 
         "Number": "98761234", 
         "Extension": null, 
         "Active": false 
        } 
       ], 
       "Email": [{ 
         "Subtype": "Work", 
         "EmailAddress": "[email protected]", 
         "Active": true 
        } 
       ] 
      } 
     } 
    ] 
} 

我能集團這個由PK,但我有在第二部分,關於如何將嵌套結構內做分組難度。你能展示一個樣本規範並提供一些解釋嗎?

+0

@RomanPerekhrest什麼關係呢?這是一個Java庫,所以我不認爲OS很重要。這甚至可以在瀏覽器上進行測試,http://jolt-demo.appspot.com。 – menorah84

回答

1

可能但真正令人費解/冗長。這推動了Jolt應該做的事情。

一個支點和一些重映射是維護,但這是夠複雜了,這將是非常困難,如果出現錯誤/你的數據被怪異的調試。

需要5個步驟。兩個將STATUS從一個字符串修復爲一個布爾值。兩個用於樞軸轉動數據。最後一個把所有東西放在正確的最終位置。

我建議檢查每個步驟檢查它自己的選項卡每個步驟/顛簸演示站點的副本,看看/神交了每個步驟做。

規格

[ 
    { 
    // ninja in a true and false value so that 
    // Status "Active"/"Inactive" can be "mapped" to booleans 
    "operation": "default", 
    "spec": { 
     "*": { 
     "FALSE": false, 
     "TRUE": true 
     } 
    } 
    }, 
    { 
    // fix STATUS 
    "operation": "shift", 
    "spec": { 
     "*": { 
     // 
     "STATUS": { 
      // Match "Active" as make STATUS be true 
      "Active": { 
      "@(2,TRUE)": "[&3].STATUS" 
      }, 
      // Everything else set to false 
      "*": { 
      "@(2,FALSE)": "[&3].STATUS" 
      } 
     }, 
     // match and discard TRUE and FALSE 
     "TRUE|FALSE": null, 
     // pass everything else thru 
     "*": "[&1].&" 
     } 
    } 
    }, 
    { 
    // now, group by PK value 
    "operation": "shift", 
    "spec": { 
     // top level array 
     "*": { 
     "PK": { 
      "*": { // match any value of PK 
      // go back up and grab the whole block and write 
      // it to the ouput where the key, is the value of PK 
      "@2": "&1[]" 
      } 
     } 
     } 
    } 
    }, 
    { 
    // sub group by CONTACT_TYPE, with the complication of 
    // pulling one entry off to serve as the "Reference" 
    "operation": "shift", 
    "spec": { 
     "*": { // pk value 
     "0": { // special case the Zeroth item so that 
      // we can pull off once copy to serve as the 
      // Reference 
      "@": "&2.Reference", 
      // sub group by CONTACT_TYPE 
      "CONTACT_TYPE": { 
      "*": { 
       "@2": "&4.ContactMethods.&1[]" 
      } 
      } 
     }, 
     "*": { // all the rest of the array indicies 
      // sub group by CONTACT_TYPE 
      "CONTACT_TYPE": { 
      "*": { 
       "@2": "&4.ContactMethods.&1[]" 
      } 
      } 
     } 
     } 
    } 
    }, 
    { 
    // Data fixing and Grouping done, now put everything 
    // in its final place 
    "operation": "shift", 
    "spec": { 
     "*": { // top level pk 
     "Reference": { 
      "PK": "Accounts[#3].Reference.Key", 
      "SOURCE_DB": "Accounts[#3].Reference.System" 
     }, 
     "ContactMethods": { 
      "Phone": { 
      "*": { 
       "CONTACT_SUBTYPE": "Accounts[#5].ContactMethods.Phone[&1].Subtype", 
       "PHONE_COUNTRY_CODE": "Accounts[#5].ContactMethods.Phone[&1].CountryCode", 
       "PHONE_NUMBER": "Accounts[#5].ContactMethods.Phone[&1].Number", 
       "PHONE_EXT": "Accounts[#5].ContactMethods.Phone[&1].Extension", 
       "STATUS": "Accounts[#5].ContactMethods.Phone[&1].Active" 
      } 
      }, 
      "Email": { 
      "*": { 
       "CONTACT_SUBTYPE": "Accounts[#5].ContactMethods.Email[&1].Subtype", 
       "EMAIL": "Accounts[#5].ContactMethods.Email[&1].EmailAddress", 
       "STATUS": "Accounts[#5].ContactMethods.Email[&1].Active" 
      } 
      } 
     } 
     } 
    } 
    } 
]