2017-05-27 199 views
0

以下是場景,我正在讀取SQL Server及其在地圖結構中讀取的數據。問題是其中一個字段是JSON字符串,但預期的輸出是JSON列表/對象。 這裏是源數據(ESB內部讀): 源數據:將json字符串轉換爲muler中的json對象

{ 
    price_distributor=44.000000000, 
    width=0E-12, 
    tax_status=taxable, 
    web_ref=OLG, 
    stock_qty=0E-9, 
    price_dealer=40.000000000, 
    sync_uid=11, 
    shipping_class_id=null, 
    shipping_required=true, 
    attributes=[{"parent_sku":"BBB_BBBBB","variation":1,"attid":18,"attname":"Fabric Type","attslug":"pa_fabric-type","attuid":837,"option":"Black","termid":219,"termslug":"black","termuid":854},{"parent_sku":"AAA_AAAAA","variation":1,"attid":18,"attname":"Fabric Type","attslug":"pa_fabric-type","attuid":837,"option":"Green","termid":233,"termslug":"green","termuid":853},{"parent_sku":"YYYY_YYYYY","variation":1,"attid":18,"attname":"Fabric Type","attslug":"pa_fabric-type","attuid":837,"option":"Blue","termid":232,"termslug":"blue","termuid":848},{"parent_sku":"XXXX_XXXX","variation":1,"attid":18,"attname":"Fabric Type","attslug":"pa_fabric-type","attuid":837,"option":"Red","termid":235,"termslug":"red","termuid":851}] 
} 

領域中的問題是屬性和它的內容是不是一個JSON數組,而它的一個strigified陣列。

這裏是輸出:

{ 
    "price_distributor": "44", 
    "width": "0", 
    "tax_status": "taxable", 
    "web_ref": "OLG", 
    "stock_qty": "0", 
    "price_dealer": "40", 
    "sync_uid": 11, 
    "shipping_class_id": null, 
    "shipping_required": true, 
    "attributes": "[{\"parent_sku\":\"BBB_BBBBB\",\"variation\":1,\"attid\":18,\"attname\":\"Fabric Type\",\"attslug\":\"pa_fabric-type\",\"attuid\":837,\"option\":\"Black\",\"termid\":219,\"termslug\":\"black\",\"termuid\":854},{\"parent_sku\":\"AAA_AAAAA\",\"variation\":1,\"attid\":18,\"attname\":\"Fabric Type\",\"attslug\":\"pa_fabric-type\",\"attuid\":837,\"option\":\"Green\",\"termid\":233,\"termslug\":\"green\",\"termuid\":853},{\"parent_sku\":\"YYYY_YYYYY\",\"variation\":1,\"attid\":18,\"attname\":\"Fabric Type\",\"attslug\":\"pa_fabric-type\",\"attuid\":837,\"option\":\"Blue\",\"termid\":232,\"termslug\" 

:\"blue\",\"termuid\":848},{\"parent_sku\":\"XXXX_XXXX\",\"variation\":1,\"attid\":18,\"attname\":\"Fabric Type\",\"attslug\":\"pa_fabric-type\",\"attuid\":837,\"option\":\"Red\",\"termid\":235,\"termslug\":\"red\",\"termuid\":851}]" 
} 

屬性場而打算轉型應該是

{ 
    "price_distributor": "44", 
    "width": "0", 
    "tax_status": "taxable", 
    "web_ref": "OLG", 
    "stock_qty": "0", 
    "price_dealer": "40", 
    "sync_uid": 11, 
    "shipping_class_id": null, 
    "shipping_required": true, 
    "attributes": [ 
     { 
      "parent_sku":"BBB_BBBBB", 
      "variation":1, 
      "attid":18, 
      "attname":"Fabric Type", 
      "attslug":"pa_fabric-type", 
      "attuid":837, 
      "option":"Black", 
      "termid":219, 
      "termslug":"black", 
      "termuid":854 
     }, 
     { 
      "parent_sku":"AAA_AAAAA", 
      "variation":1, 
      "attid":18, 
      "attname":"Fabric Type", 
      "attslug":"pa_fabric-type", 
      "attuid":837, 
      "option":"Green", 
      "termid":233, 
      "termslug":"green", 
      "termuid":853 
     }, 
     { 
      "parent_sku":"YYYY_YYYYY", 
      "variation":1, 
      "attid":18, 
      "attname":"Fabric Type", 
      "attslug":"pa_fabric-type", 
      "attuid":837, 
      "option":"Blue", 
      "termid":232, 
      "termslug":"blue", 
      "termuid":848 
     }, 
     { 
      "parent_sku":"XXXX_XXXX", 
      "variation":1, 
      "attid":18, 
      "attname":"Fabric Type", 
      "attslug":"pa_fabric-type", 
      "attuid":837, 
      "option":"Red", 
      "termid":235, 
      "termslug":"red", 
      "termuid":851 
     } 
    ] 
} 

我用屬性的dataweave改造是這樣的:

attributes: payload.attributes 

我知道這是不正確的,但有沒有一個運算符/函數可以幫助將字符串化json轉換爲json對象/列表,像json.parse()也許?

非常感謝您的幫助。

謝謝,-Sam

回答

0

我想通了。以下是供參考的數據編輯片段:

attributes: (payload.attributes replace /\[|\]|[\\]|\"/ with "" splitBy "}") 
    map using(attarr = $ replace /\{|,\{/ with "" splitBy ",") 
     { 
      parent_sku: (attarr[0] splitBy ":")[1], 
      variation: (attarr[1] splitBy ":")[1], 
      attid: (attarr[2] splitBy ":")[1], 
      attname: (attarr[3] splitBy ":")[1], 
      attslug: (attarr[4] splitBy ":")[1], 
      attuid: (attarr[5] splitBy ":")[1], 
      option: (attarr[6] splitBy ":")[1]//, 
      termid: (attarr[7] splitBy ":")[1], 
      termslug: (attarr[8] splitBy ":")[1], 
      termuid: (attarr[9] splitBy ":")[1] 
     }, 
相關問題