我正在尝试使用JOLT转换记录。字符串元素- from access_level
字段和数组元素(from tag
字段)必须转换为properties
数组中的单独对象。而是将两个元素合并为一个:
以下是输入:
{
"access_level": "public",
"tags": [
{
"id": 174,
"tag_name": "machine-learning",
"tag_path": "machine-learning"
},
{
"id": 393,
"tag_name": "chinese",
"tag_path": "chinese"
}
]
}
和spec文件:
[
{
"operation": "shift",
"spec": {
"access_level": {
"*": {
"@1": "properties[#1].value",
"#terms-of-use": "properties[#1].type.code"
}
},
"tags": {
"*": {
"tag_name": "properties[#2].value",
"#keyword": "properties[#2].type.code"
}
}
}
}
]
预期输出为:
{
"properties" : [
{
"value" : "public",
"type" : {
"code" : "terms-of-use"
}
},
{
"type" : {
"code" : "keyword"
},
"value" : "machine-learning"
},
{
"type" : {
"code" : "keyword"
},
"value" : "chinese"
} ]
}
但access_level
和来自tags
的first value正在合并:
{
"properties" : [ {
"value" : [ "public", "machine-learning" ],
"type" : {
"code" : "keyword"
}
}, {
"type" : {
"code" : [ "terms-of-use", "keyword" ]
},
"value" : "chinese"
} ]
}
如果输入上没有access_level
,则输出看起来很好-2在properties
数组中创建对象
发布于 2021-04-08 16:49:03
您可以使用与号符号应用连续的shift转换来确定键,同时在第一步中生成一个数组(p
)(从tags
派生)和一个元素(从access_level
派生);然后将该数组重命名为properties
,同时在最后一步将该元素添加到其中,方法如下:
[
{
"operation": "shift",
"spec": {
"tags": {
"*": {
"tag_name": "p.[&1].value",
"#keyword": "p.[&1].type.code"
}
},
"access_level": {
"*": {
"@(3,access_level)": "&1.value",
"#terms-of-use": "&1.type.code"
}
}
}
},
{
"operation": "shift",
"spec": {
"*": "properties"
}
}
]
https://stackoverflow.com/questions/66985840
复制相似问题