0

我是一个颠簸的新手。我有以下问题,我有一个 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 类型层次结构验证它。

4

1 回答 1

0

你可以做的就是向下匹配“type”的值,然后向上跳一些,将消息处理为“text”类型或“image”类型。

输入

{
  "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"
              }
            }
          }
        }
      }
    }
  }
]
于 2017-08-04T22:12:54.157 回答