2017-08-24 7 views
0

を変換:各エントリの中に、Joltのは、私はこのフラットな構造を有している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で何をすべきかの限界を押し進めています。

1つのピボットといくつかの再マッピングは維持可能ですが、何かがうまくいかないとデバッグするのが非常に難しくなります。

5つのステップが必要です。 STATUSを文字列からブール値に修正するには2つ。データをピボットしてサブピボットします。そして最後のものはすべてを正しい最終場所に置く。

各ステップを確認してください。各ステップを確認することをお勧めします。Joltのデモサイトのコピー/タブのコピーは、各ステップで何が行われているかを確認するために使用します。

スペック

[ 
    { 
    // 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" 
      } 
      } 
     } 
     } 
    } 
    } 
] 
関連する問題