DataWeave JSON转换/提取和连接值

发布于 2025-01-24 10:28:44 字数 3651 浏览 6 评论 0原文

我希望从看起来像这样的JSON结构:

{
  "id": "955559665",
  "timestamp": "2022-04-21 00:00:19",
  "user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Safari/605.1.15",
  "remote_addr": "123.456.789.012",
  "read": "0",
  "data": {
      "80928111": {
          "field": "80928111",
          "value": "Z01234567",
          "flat_value": "Z01234567",
          "label": "ID",
          "type": "text"
      },
      "90924321": {
          "field": "90924321",
            "value": {
                "first": "Jane",
                "last": "Doe"
            },
            "flat_value": "first = Jane\nlast = Doe",
            "label": "Name",
            "type": "name"
        },
        "88888770": {
            "field": "88888770",
            "value": "[email protected]",
            "flat_value": "[email protected]",
            "label": "Email",
            "type": "email"
        },
        "12345678": {
            "field": "12345678",
            "value": "https://www.google.com/subdomain/attachment/file.txt",
            "flat_value": "https://www.google.com/subdomain/attachment/file.txt",
            "label": "Choose File",
            "type": "file"
        }
    }
}

最终转到类似的东西:

{
    "name_val":"Name: first = Jane\nlast = Doe\nEmail: [email protected]\n",
    "file": {
      "id": "12345678C",
      "name": "file.txt"
    }
}

在原始JSON中,“数据”对象代表表单提交。每个子对象代表提交表单上的字段。我感兴趣的唯一区别是将字段的“类型”识别为“文件”。

每个不是“文件”类型的响应,我都想将其加入为一个看起来像:'label1:flat_value1 \ nlabel2:flat_value2 ...'的大字符串值,'

注意,实际字段的数量是可变的。

然后,我需要第二个对象,通过识别“字段” ID和文件名来显示“文件”字段。

我已经开始工作了。例如,使用拔毛和过滤器,我能够将字段类型分开。

类似的东西:

%dw 2.0
output application/json
---
[
    "fields": payload.data pluck(
        {
            "field": $."label",
            "value": $."flat_value",
            "type": $."type"
        }
    ) filter ($."type" != "file") default "",
    "files": payload.data pluck(
        {
            "type": $."type",
            "fieldId": $."field"
        }
    ) filter ($."type" == "file") default ""
]

给我:

[
  {
    "fields": [
      {
        "field": "ID",
        "value": "Z01234567",
        "type": "text"
      },
      {
        "field": "Name",
        "value": "first = Jane\nlast = Doe",
        "type": "name"
      },
      {
        "field": "Email",
        "value": "[email protected]",
        "type": "email"
      }
    ]
  },
  {
    "files": [
      {
        "type": "file",
        "fieldId": "12345678"
      }
    ]
  }
]

玩修改后的JSON输入,我能够轻松地看到与我想看的串联相似的串联,但不完全是:

%dw 2.0
output application/json
var inputJson = [
    {
        "field": "ID",
        "value": "Z01234567",
        "type": "text"
    },
    {
        "field": "Name",
        "value": "first = Jane\nlast = Doe",
        "type": "name"
    }
]
---


inputJson map ((value, index) -> value.field ++ ': ' ++ value.value)

给我:

[
  "ID: Z01234567",
  "Name: first = Jane\nlast = Doe"
]

但是我似乎无法将其全部放置在一起,从头到尾。

I'm looking to go from a JSON structure that looks something like this:

{
  "id": "955559665",
  "timestamp": "2022-04-21 00:00:19",
  "user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Safari/605.1.15",
  "remote_addr": "123.456.789.012",
  "read": "0",
  "data": {
      "80928111": {
          "field": "80928111",
          "value": "Z01234567",
          "flat_value": "Z01234567",
          "label": "ID",
          "type": "text"
      },
      "90924321": {
          "field": "90924321",
            "value": {
                "first": "Jane",
                "last": "Doe"
            },
            "flat_value": "first = Jane\nlast = Doe",
            "label": "Name",
            "type": "name"
        },
        "88888770": {
            "field": "88888770",
            "value": "[email protected]",
            "flat_value": "[email protected]",
            "label": "Email",
            "type": "email"
        },
        "12345678": {
            "field": "12345678",
            "value": "https://www.google.com/subdomain/attachment/file.txt",
            "flat_value": "https://www.google.com/subdomain/attachment/file.txt",
            "label": "Choose File",
            "type": "file"
        }
    }
}

Ultimately to something like this:

{
    "name_val":"Name: first = Jane\nlast = Doe\nEmail: [email protected]\n",
    "file": {
      "id": "12345678C",
      "name": "file.txt"
    }
}

In the original JSON, the 'data' object represents a form submission. Each sub object represents a field on the submitted form. The only distinction I'm interested in is the 'type' of field identified as 'file'.

Every response that is not of 'file' type, I want to concatenate into one large String value that looks like: 'label1: flat_value1\nlabel2: flat_value2...'

Note, the number of actual fields is variable.

Then I need a second object to show the field of type 'file', by identifying the 'field' id, and the name of the file.

I've gotten pieces of this to work. For example, using pluck and filter, I've been able to separate the types of fields.

Something like this:

%dw 2.0
output application/json
---
[
    "fields": payload.data pluck(
        {
            "field": $."label",
            "value": $."flat_value",
            "type": $."type"
        }
    ) filter ($."type" != "file") default "",
    "files": payload.data pluck(
        {
            "type": $."type",
            "fieldId": $."field"
        }
    ) filter ($."type" == "file") default ""
]

Gives me:

[
  {
    "fields": [
      {
        "field": "ID",
        "value": "Z01234567",
        "type": "text"
      },
      {
        "field": "Name",
        "value": "first = Jane\nlast = Doe",
        "type": "name"
      },
      {
        "field": "Email",
        "value": "[email protected]",
        "type": "email"
      }
    ]
  },
  {
    "files": [
      {
        "type": "file",
        "fieldId": "12345678"
      }
    ]
  }
]

And playing around with a modified JSON input, I was able to easily see concatenation similar to how I want to see it, but not quite there:

%dw 2.0
output application/json
var inputJson = [
    {
        "field": "ID",
        "value": "Z01234567",
        "type": "text"
    },
    {
        "field": "Name",
        "value": "first = Jane\nlast = Doe",
        "type": "name"
    }
]
---


inputJson map ((value, index) -> value.field ++ ': ' ++ value.value)

Gives me:

[
  "ID: Z01234567",
  "Name: first = Jane\nlast = Doe"
]

But I can't seem to put it all together and go from Beginning to End.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

违心° 2025-01-31 10:28:44

有几种实施此方法的方法。我建议尝试封装您工作的零件并将其用作构建块。

%dw 2.0
output application/json
fun fields(x) = x.data pluck(
        {
            "field": $."label",
            "value": $."flat_value",
            "type": $."type"
        }
    ) filter ($."type" != "file") default ""
fun files(x) = x.data pluck(
        {
            "type": $."type",
            "fieldId": $."field"
        }
    ) filter ($."type" == "file") default ""
---
{
    name_val: fields(payload) reduce ((item,acc="") -> acc ++ item.field ++ ': ' ++ item.value ++ "\n"),
    files: files(payload)[0]
}

输出:

{
  "name_val": "ID: Z01234567\nName: first = Jane\nlast = Doe\nEmail: [email protected]\n",
  "files": {
    "type": "file",
    "fieldId": "12345678"
  }
}

There are several ways to implement this. I recommend to try to encapsulate the parts that you get working and use them as building blocks.

%dw 2.0
output application/json
fun fields(x) = x.data pluck(
        {
            "field": $."label",
            "value": $."flat_value",
            "type": $."type"
        }
    ) filter ($."type" != "file") default ""
fun files(x) = x.data pluck(
        {
            "type": $."type",
            "fieldId": $."field"
        }
    ) filter ($."type" == "file") default ""
---
{
    name_val: fields(payload) reduce ((item,acc="") -> acc ++ item.field ++ ': ' ++ item.value ++ "\n"),
    files: files(payload)[0]
}

Output:

{
  "name_val": "ID: Z01234567\nName: first = Jane\nlast = Doe\nEmail: [email protected]\n",
  "files": {
    "type": "file",
    "fieldId": "12345678"
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文