DataWeave JSON 转换/提取和连接值
DataWeave JSON Transformation/ Extract and concatenate values
我希望从看起来像这样的 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": "jdoe001@gmail.com",
"flat_value": "jdoe001@gmail.com",
"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: jdoe001@gmail.com\n",
"file": {
"id": "12345678C",
"name": "file.txt"
}
}
在原来的JSON中,'data'对象表示表单提交。每个子对象代表提交表单上的一个字段。我感兴趣的唯一区别是字段的 'type' 标识为 'file'.
每个不是 'file' 类型的响应,我想连接成一个大字符串值,如下所示:'label1: flat_value1\nlabel2: flat_value2...'
请注意,实际字段的数量是可变的。
然后我需要第二个对象来显示类型 'file' 的字段,方法是识别 'field' id 和文件名。
我已经开始使用它了。例如,使用 pluck 和 filter,我已经能够分离字段的类型。
像这样:
%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": "jdoe001@gmail.com",
"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"
]
但我似乎无法将所有内容放在一起并从头到尾。
有几种方法可以实现这一点。我建议尝试封装您开始工作的部分并将它们用作构建块。
%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: jdoe001@gmail.com\n",
"files": {
"type": "file",
"fieldId": "12345678"
}
}
我希望从看起来像这样的 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": "jdoe001@gmail.com",
"flat_value": "jdoe001@gmail.com",
"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: jdoe001@gmail.com\n",
"file": {
"id": "12345678C",
"name": "file.txt"
}
}
在原来的JSON中,'data'对象表示表单提交。每个子对象代表提交表单上的一个字段。我感兴趣的唯一区别是字段的 'type' 标识为 'file'.
每个不是 'file' 类型的响应,我想连接成一个大字符串值,如下所示:'label1: flat_value1\nlabel2: flat_value2...'
请注意,实际字段的数量是可变的。
然后我需要第二个对象来显示类型 'file' 的字段,方法是识别 'field' id 和文件名。
我已经开始使用它了。例如,使用 pluck 和 filter,我已经能够分离字段的类型。
像这样:
%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": "jdoe001@gmail.com",
"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"
]
但我似乎无法将所有内容放在一起并从头到尾。
有几种方法可以实现这一点。我建议尝试封装您开始工作的部分并将它们用作构建块。
%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: jdoe001@gmail.com\n",
"files": {
"type": "file",
"fieldId": "12345678"
}
}