2017-05-07 28 views
1

我的輸入JSON如下dataweave總和功能: -JSON爲xml使用

{ 
    "Studentvalue": [ 
    { 
     "StudentDetails": { 
     "valueId": "default", 
     "reason": "default", 
     "type": "high", 
     "Schoolbudget": [ 
      { 
      "Id": "100", 
      "Age": "23" 
      }, 
      { 
      "Id": "101", 
      "Age": "24" 
      }, 
      { 
      "Id": "102", 
      "Age": "25" 
      } 
     ], 
     "isApplicable": "boolean", 
     "isNotified": "boolean" 
     } 
    } 
    ] 
} 

輸出XML是:

<schoolmemo> 
<active>yes<active> 
<schooltype>Primary<schooltype> 
<validity>?<validity> 
</schoolmemo> 

我有一個條件,如果在JSON 「AGE」 字段的總和大於比100中的有效性標籤的值是'yes'else'no'

你能幫助我如何在數據編輯中使用sum函數來驗證有效性字段嗎?重要的一點是我正在獲取「age」字段的數組JSON。

乾杯, Bsolver

回答

0

這裏是你正在尋找的dataweave:

%dw 1.0 
%output application/xml 
%var agesCombined = sum payload.StudentDetails.StudentType.Age 
--- 
schoolmemo: { 
    active: "yes", 
    schooltype: "Primary", 
    validity: "yes" when agesCombined < 100 otherwise "no" 
} 
+0

here ..%var agescombined將無法正常工作,因爲我正在獲取年齡值數組我想在這裏結合所有年齡:-23 + 24 + 25 請告訴我如何總結所有年齡段在排列比應用條件 – Isranis

+0

Yevgeniy的答案是正確的,試試看。 payload.StudentDetails.StudentType.Age產生一個數組。你可以把它寫成payload.StudentDetails。* StudentType.Age這使得它更清晰,但結果是一樣的。 –

+0

沒有它的不工作它給出了一個錯誤「null到數組」,我已經更新了我的JSON與準確的領域,你可以請現在幫助我...對不起一個更新: - (....我必須做總結年齡23 + 24 + 25,並進行驗證。 – Isranis

0

試試這個dataweave腳本

%dw 1.0 
%output application/xml 
%var sumOfAge = sum payload.Studentvalue.StudentDetails.Schoolbudget..Age 
--- 
schoolmemo: { 
    active: "yes", 
    schooltype: "Primary", 
    validity: "yes" when sumOfAge < 100 otherwise "no" 
} 

payload.Studentvalue.StudentDetails.Schoolbudget..Age將獲得青睞的陣列。