Mule 4 - 查找 json 中的值并导出相应的 jsons 值

问题描述 投票:0回答:1

在这里,我需要在 json arrayObject 中查找 value1(颜色/大小),并映射到 mule 4 dataweave 中相同 json 数组对象的 value2(黄色/28 英寸)。下面是我的 json 输入

JSON 输入

{
  "system": "Computer",
  "description": "",
  "details": "",
  "systemDetails": [
    {
      "systemnumber": "A123",
      "description": [
        {
          "group": "color",
          "desc": "yellow"
        },
        {
          "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"
    }
  ]
}

让我知道如何实现这一目标?

arrays json dataweave mule4
1个回答
0
投票

假设输出应该是一个数组(因为它还能是什么?),它看起来与

payload.systemDetails
相同,但键名发生了变化。一种方法是仅使用映射和嵌套映射来表示
description
:

中的嵌套数组
%dw 2.0
output application/json
---
payload.systemDetails map {
    systemId: $.systemnumber,
    systemLevelDetails: $.description map {
        systemAttributeName: $.group,
        values: $.desc
    }
}

输出:

[
  {
    "systemId": "A123",
    "systemLevelDetails": [
      {
        "systemAttributeName": "color",
        "values": "yellow"
      },
      {
        "systemAttributeName": "category",
        "values": "Desktop"
      }
    ]
  },
  {
    "systemId": "B123",
    "systemLevelDetails": [
      {
        "systemAttributeName": "size",
        "values": "28 inch"
      },
      {
        "systemAttributeName": "category",
        "values": "Laptop"
      }
    ]
  }
]
© www.soinside.com 2019 - 2024. All rights reserved.