这是一个场景,我正在读取SQL Server上的数据及其在地图结构中的读取。问题是其中一个字段是JSON字符串,但预期的输出是JSON列表/对象。 这是源数据(在ESB中读取): 来源数据:
{
price_distributor=44.000000000,
width=0E-12,
tax_status=taxable,
web_ref=OLG,
stock_qty=0E-9,
price_dealer=40.000000000,
sync_uid=11,
shipping_class_id=null,
shipping_required=true,
attributes=[{"parent_sku":"BBB_BBBBB","variation":1,"attid":18,"attname":"Fabric Type","attslug":"pa_fabric-type","attuid":837,"option":"Black","termid":219,"termslug":"black","termuid":854},{"parent_sku":"AAA_AAAAA","variation":1,"attid":18,"attname":"Fabric Type","attslug":"pa_fabric-type","attuid":837,"option":"Green","termid":233,"termslug":"green","termuid":853},{"parent_sku":"YYYY_YYYYY","variation":1,"attid":18,"attname":"Fabric Type","attslug":"pa_fabric-type","attuid":837,"option":"Blue","termid":232,"termslug":"blue","termuid":848},{"parent_sku":"XXXX_XXXX","variation":1,"attid":18,"attname":"Fabric Type","attslug":"pa_fabric-type","attuid":837,"option":"Red","termid":235,"termslug":"red","termuid":851}]
}
有问题的字段是属性,它的内容不是json数组,而是一个strigified数组。
以下是输出:
{
"price_distributor": "44",
"width": "0",
"tax_status": "taxable",
"web_ref": "OLG",
"stock_qty": "0",
"price_dealer": "40",
"sync_uid": 11,
"shipping_class_id": null,
"shipping_required": true,
"attributes": "[{\"parent_sku\":\"BBB_BBBBB\",\"variation\":1,\"attid\":18,\"attname\":\"Fabric Type\",\"attslug\":\"pa_fabric-type\",\"attuid\":837,\"option\":\"Black\",\"termid\":219,\"termslug\":\"black\",\"termuid\":854},{\"parent_sku\":\"AAA_AAAAA\",\"variation\":1,\"attid\":18,\"attname\":\"Fabric Type\",\"attslug\":\"pa_fabric-type\",\"attuid\":837,\"option\":\"Green\",\"termid\":233,\"termslug\":\"green\",\"termuid\":853},{\"parent_sku\":\"YYYY_YYYYY\",\"variation\":1,\"attid\":18,\"attname\":\"Fabric Type\",\"attslug\":\"pa_fabric-type\",\"attuid\":837,\"option\":\"Blue\",\"termid\":232,\"termslug\"
:\"blue\",\"termuid\":848},{\"parent_sku\":\"XXXX_XXXX\",\"variation\":1,\"attid\":18,\"attname\":\"Fabric Type\",\"attslug\":\"pa_fabric-type\",\"attuid\":837,\"option\":\"Red\",\"termid\":235,\"termslug\":\"red\",\"termuid\":851}]"
}
请参阅属性字段,而预期转化应为:
{
"price_distributor": "44",
"width": "0",
"tax_status": "taxable",
"web_ref": "OLG",
"stock_qty": "0",
"price_dealer": "40",
"sync_uid": 11,
"shipping_class_id": null,
"shipping_required": true,
"attributes": [
{
"parent_sku":"BBB_BBBBB",
"variation":1,
"attid":18,
"attname":"Fabric Type",
"attslug":"pa_fabric-type",
"attuid":837,
"option":"Black",
"termid":219,
"termslug":"black",
"termuid":854
},
{
"parent_sku":"AAA_AAAAA",
"variation":1,
"attid":18,
"attname":"Fabric Type",
"attslug":"pa_fabric-type",
"attuid":837,
"option":"Green",
"termid":233,
"termslug":"green",
"termuid":853
},
{
"parent_sku":"YYYY_YYYYY",
"variation":1,
"attid":18,
"attname":"Fabric Type",
"attslug":"pa_fabric-type",
"attuid":837,
"option":"Blue",
"termid":232,
"termslug":"blue",
"termuid":848
},
{
"parent_sku":"XXXX_XXXX",
"variation":1,
"attid":18,
"attname":"Fabric Type",
"attslug":"pa_fabric-type",
"attuid":837,
"option":"Red",
"termid":235,
"termslug":"red",
"termuid":851
}
]
}
我用于属性的数据编辑转换是这样的:
attributes: payload.attributes
我知道这不正确,但有没有一个运算符/函数可以帮助将字符串化的json转换为json对象/列表,比如json.parse()?
非常感谢您的帮助。
谢谢,-Sam
答案 0 :(得分:0)
这个问题可以通过使用" read"来解决。功能
您可以考虑使用类似以下DataWeave 1.0代码段的内容:
%dw 1.0
%var content = {
key1: "value1",
attributes: "{ \"stringKey\":\"stringValue\" }"
}
%output application/json
---
read(content.attributes, "application/json")
输出:
{
"stringKey": "stringValue"
}
另请参阅read function in the official reference:
read函数返回解析content参数的结果 使用指定的mimeType阅读器。
第一个参数指向必须读取的内容,第二个参数指向 写它的格式。第三个可选参数列表 读者配置属性。
答案 1 :(得分:-1)
我明白了。以下是dataweave片段供参考:
attributes: (payload.attributes replace /\[|\]|[\\]|\"/ with "" splitBy "}")
map using(attarr = $ replace /\{|,\{/ with "" splitBy ",")
{
parent_sku: (attarr[0] splitBy ":")[1],
variation: (attarr[1] splitBy ":")[1],
attid: (attarr[2] splitBy ":")[1],
attname: (attarr[3] splitBy ":")[1],
attslug: (attarr[4] splitBy ":")[1],
attuid: (attarr[5] splitBy ":")[1],
option: (attarr[6] splitBy ":")[1]//,
termid: (attarr[7] splitBy ":")[1],
termslug: (attarr[8] splitBy ":")[1],
termuid: (attarr[9] splitBy ":")[1]
},