簡體   English   中英

Dataweave中Json數組的轉換

[英]Transformation of Json array in Dataweave

如何在 Anytime Studio 中為給定的 Json 數組輸入和輸出編寫 Dataweave 轉換。 輸入:

{
    "result": [{
        "Labels": [{
            "value": [{
                    "fieldName": "firstName",
                    "value": "John"
                },
                {
                    "fieldName": "lastName",
                    "value": "Doe"
                },
                {
                    "fieldName": "fullName",
                    "value": "John Doe"
                }
            ]
        }]
    }]
}

輸出:

 {
    "result": [{
        "Labels": [{
            "value": [{
                "firstName": "John",
                "lastName": "Doe",
                "fullName": "John Doe"
            }]
        }]
    }]
 }

https://docs.mulesoft.com/dataweave/2.4/dw-core-functions-reduce Reduce 函數可能是應該使用的函數提前謝謝你

您可以使用map將所有數組映射到所需的格式。 對於值部分,您可以將值映射為fieldName: value數組並通過將數組包裹在括號中將它們解構為對象

%dw 2.0
output application/json  
---
{
  result: payload.result map ((item) -> {
    Labels: item.Labels map ((label) -> {
      value: [
        {
          (label.value map ((field) -> 
            (field.fieldName): field.value
          )) //wrap the array, i.e. lavel.value map ... in parentheses so that it will give you individual key pair.
        }
      ]
    })
  })
}

如果您知道 keyNames 不會改變,您可以在下面嘗試:

%dw 2.0
output application/json  
---
payload update {
  case res at .result -> res map (res, resIndex) -> (res update {
      case lbl at .Labels -> lbl map (lbl, lblIndex) -> (lbl update {
          case val at .value -> [
            (val reduce ((item, acc = {}) -> acc ++ {
                (item.fieldName): (item.value)
              }))
          ]
        }
        )
    }
    )
}

這是2個警告和一個解決方案。 您的輸入和輸出文件都不是有效的 JSON。

  1. 輸入文件,在您的“結果”對象中,“標簽”需要花括號 {},因為它們是對象。 鍵值對應該看起來像這樣 {key:value} 而不是那個 key:value
  2. 輸出文件,在你的“值”數組中,鍵值對需要有 curlies {key:value}

所以這是您輸入的有效 JSON 版本

{
 "result": [
  {"Labels": [
        {
          "value": [
            {"fieldName": "firstName","value": "John"},
            {"fieldName": "lastName","value": "Doe"},
            {"fieldName": "fullName","value": "John Doe"}
          ]
        }
      ]},
   {"Labels": [
        {
          "value": [
            {"fieldName": "firstName","value": "John"}
          ]
        }
      ]}
]}  

這是一個解決方案

%dw 2.0
import keySet from dw::core::Objects

// this is "result"
var layer1key = keySet(payload)[0]
// this is "Labels" and grabs the first Labels, so assumes Labels doesn't change
var layer2 = payload[layer1key]
var layer2key = keySet(layer2[0])[0]
// this is "value"
var layer3 = layer2[layer2key]
var layer3key = keySet(layer3[0][0])[0]
// this is "fieldName" and "value"
var layer4 = layer3 map (x) -> x['value']

var data1 = ((layer1key) : layer4 map (x) -> {
    (layer2key): x map (y) -> {
        (layer3key): y map (z) -> {
            (z['fieldName']):z['value']
        }
    }
})
output application/json
---
data1

以及輸出的有效 JSON 版本

{
  "result": [
    {
      "Labels": [
        {
          "value": [
            {
              "firstName": "John"
            },
            {
              "lastName": "Doe"
            },
            {
              "fullName": "John Doe"
            }
          ]
        }
      ]
    },
    {
      "Labels": [
        {
          "value": [
            {
              "firstName": "John"
            }
          ]
        }
      ]
    }
  ]
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM