我是个新手。
我有以下问题,我有一个JSON文档结构,可能会根据type
属性而有所不同。请参阅下面的示例。
{
"recipientId": "xxx",
"messages": [
{
"type": "text",
"text": "hi there!"
},
{
"type": "image",
"url": "http://example.com/image.jpg",
"preview": "http://example.com/thumbnail.jpg"
}
]
}
转换后我想收到以下输出:
{
"messages" : [ {
"text" : "hi there!",
"type" : "text"
}, {
"type" : "image",
"url" : "http://example.com/image.jpg",
"preview": "http://example.com/thumbnail.jpg"
} ],
"to" : "xxx"
}
以下是我提出的规范:
[
{
"operation": "shift",
"spec": {
"recipientId": "to",
"messages": {
"*": {
"type": "messages[&1].type",
"text": "messages[&1].text",
"url": "messages[&1].url",
"preview": "messages[&1].previewImageUrl"
}
}
}
}
]
这种方法的问题在于,如果我有"type": "text"
并且如果我也将"preview"
属性与值一起抛出,那么它将没有意义,因为类型text
不应该{ {1}}属性集。
所以,我想要根据" type"的值来忽略一些属性。财产或避免转换此类有效载荷。
有没有办法做到这样做"验证"在JOLT?我看到的另一个选项是用Jackson类型层次结构来验证它。
答案 0 :(得分:0)
您可以做的是匹配"键入"的值,然后将树跳回一些,并将消息作为"文本"处理。键入或"图像"类型。
输入
{
"recipientId": "xxx",
"messages": [
{
"type": "text",
"text": "hi there!",
"preview": "SHOULD NOT PASS THRU"
},
{
"type": "image",
"url": "http://example.com/image.jpg",
"preview": "http://example.com/thumbnail.jpg"
}
]
}
规格
[
{
"operation": "shift",
"spec": {
"recipientId": "to",
"messages": {
"*": { // the array index of the messages array, referenced below
// as [&2] or [&4] depending how far down they have gone
"type": {
// always pass the value of type thru
"@": "messages[&2].type",
"text": {
// if the value of type was "text", then
// go back up the tree 3 levels (0,1,2)
// and process the whole message as a "text" type
"@2": {
"text": "messages[&4].text"
}
},
"image": {
"@2": {
// if the value of type was "image", then
// go back up the tree 3 levels (0,1,2)
// and process the whole message as a "image" type
"url": "messages[&4].url",
"preview": "messages[&4].previewImageUrl"
}
}
}
}
}
}
}
]