我是一个颠簸的新手。我有以下问题,我有一个 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
不应该"preview"
设置属性。因此,我希望根据“类型”属性的值忽略某些属性,或者避免转换此类有效负载。
有没有办法在 JOLT 中进行这种“验证”?我看到的另一个选项是使用 Jackson 类型层次结构验证它。