Mule 4 -在json中查找一个值并导出相应的json值

nfg76nw0  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(66)

在这里,我需要在我的json arrayObject中查找value 1(颜色/大小),并Map到mule 4 dataweave中相同json数组对象的value 2(黄色/28英寸)。
请注意,systemAttributeName不应与group相同。只有当group == 'Color'时,才应填充相应的systemAttributeName

JSON输入

{
  "system": "Computer",
  "description": "",
  "details": "",
  "systemDetails": [
    {
      "systemnumber": "A123",
      "description": [
        {
          "group": "color",
          "desc": "yellow"
        },
         {
          "group": "variant",
          "desc": "20k model"
        },
        {
          "group": "category",
          "desc": "Desktop"
        }
      ]
    },
    {
      "systemnumber": "B123",
      "description": [
        {
          "group": "size",
          "desc": "28 inch"
        },
        {
          "group": "category",
          "desc": "Laptop"
        }
      ]
    }
  ]
}

字符串

我的预期输出如下

{
  "systemId": "A123",
  "systemLevelDetails": [
    {
      "systemAttributeName": "Colordescription", 
      "values": "yellow"
    },// if my corresponding input systemDetails.description has a group 'color' - then this should be present
    {
      "systemAttributeName": "Categorydescription",
      "values": "Desktop"
    }// if my corresponding input systemDetails.description has a group 'category' - then this should be present
  ]
}

{
  "systemId": "B123",
  "systemLevelDetails": [
    {
      "systemAttributeName": "Sizedescription",
      "values": "28inch"
    },// if my corresponding input systemDetails.description has a group 'size' - then this should be present
    {
      "systemAttributeName": "Categorydescription",
      "values": "Laptop"
    }
  ]
}


让我知道我如何才能做到这一点?

zysjyyx4

zysjyyx41#

假设输出应该是数组(因为还能是什么呢?),它看起来和payload.systemDetails一样,只是键值改变了。一种方法是只对description中的嵌套数组使用map和嵌套map。如果只允许description.group中的某些值,我们可以使用filter()函数删除它们,通过验证group值是否存在于有效值列表中。DataWeave脚本:

%dw 2.0
import capitalize from dw::core::Strings
output application/json
var validGroups=["color", "size", "category"]
---
payload.systemDetails map {
    systemId: $.systemnumber,
    systemLevelDetails: $.description 
        filter (validGroups contains $.group)
        map {
            systemAttributeName: capitalize($.group)++"description",
            values: $.desc
        }
}

字符串
输出量:

[
  {
    "systemId": "A123",
    "systemLevelDetails": [
      {
        "systemAttributeName": "Colordescription",
        "values": "yellow"
      },
      {
        "systemAttributeName": "Categorydescription",
        "values": "Desktop"
      }
    ]
  },
  {
    "systemId": "B123",
    "systemLevelDetails": [
      {
        "systemAttributeName": "Sizedescription",
        "values": "28 inch"
      },
      {
        "systemAttributeName": "Categorydescription",
        "values": "Laptop"
      }
    ]
  }
]


如果需要单独处理输出数组的每一项,只需在流中使用<foreach>作用域。

相关问题