使用 JOLT V0.1.1 填充可以包含单个元素的 JSON 数组

问题描述 投票:0回答:1

我正在研究 NiFi,我想要进行 JOLT V0.1.1 转换,从 JSON 输入生成 JSON 数组。但我遇到的问题是,当 JSON 附带该数组的单个潜在元素时,输出为 null。这是 Este es uno de los 输入的转换:

{
  "data": {
    "type": "applications",
    "id": "66g4cfcfc9efaad060007dda346b24",
    "attributes": {
      "appliedAt": 1716321481,
      "completedAt": 1716321566,
      "status": "new",
      "progress": {
        "all": 5,
        "completed": 5
      },
      "matchingScore": 12,
      "language": "en-GB",
      "matchingProfile": {
        "bracketId": "poor",
        "label": "Below B2"
      }
    }
  },
  "included": [
    {
      "type": "matching-indicators",
      "id": "65bd129c74158f00070d67d4",
      "attributes": {
        "name": "English Proficiency",
        "score": 12,
        "bracketId": "poor",
        "externalId": "",
        "fail": false,
        "label": "Below B2"
      }
    },
    {
      "type": "matching-indicators",
      "id": "65bd1463de98c00007e0fa9d",
      "attributes": {
        "name": "Situational Judgement Test",
        "score": 42,
        "bracketId": "good",
        "externalId": "",
        "fail": false,
        "label": "Good Fit",
        "brackets": [
          {
            "bracketId": "good",
            "cutoff": false,
            "name": "Good Fit",
            "treshold": 40
          },
          {
            "bracketId": "great",
            "cutoff": false,
            "name": "Great Fit",
            "treshold": 80
          }
        ]
      }
    }
  ]
}

这是我用来获取包含所有匹配指标信息的数组的 JOLT 转换:

    [
  {
    "operation": "shift",
    "spec": {
      "included": {
        "*": {
          "type": {
            "matching-indicators": {
              "@2": ""
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "attributes": {
          "name": "[&2].name",
          "label": "[&2].label",
          "score": "[&2].score"
        }
      }
    }
  }
]

获得的输出是:

[ {
  "name" : "English Proficiency",
  "label": "Below B2",
  "score" : 12
}, {
  "name" : "Situational Judgement Test",
  "label": "Good Fit",
  "score" : 42
} ]

这正是我这个案例所需要的。

现在,当我在该输入中只有一个匹配指标时,我使用完全相同的 JOLT 得到空输出。这是带有一个匹配指示器的输入示例:

{
  "data": {
    "type": "applications",
    "id": "664cfcc9efaa0600rgd0798346b24",
    "attributes": {
      "appliedAt": 1716321481,
      "completedAt": 1716321566,
      "status": "new",
      "progress": {
        "all": 5,
        "completed": 5
      },
      "matchingScore": 12,
      "language": "en-GB",
      "matchingProfile": {
        "bracketId": "poor",
        "label": "Below B2"
      }
    }
  },
  "included": [
    {
      "type": "matching-indicators",
      "id": "65bd129c74skyt15h8f00070d67d4",
      "attributes": {
        "name": "English Proficiency",
        "score": 12,
        "bracketId": "poor",
        "externalId": "",
        "fail": false,
        "label": "Below B2"
      }
    }
  ]
}

我也尝试过这种转变:

[
  {
    "operation": "shift",
    "spec": {
      "included": {
        "*": {
          "attributes": {
            "name": "[&1].name",
            "label": "[&1].label",
            "score": "[&1].score"
          }
        }
      }
    }
  }
]

当我在 https://jolt-demo.appspot.com/ 验证它时,它似乎适用于这两种情况。但是当我直接在 NiFi 中使用它时,它为我的第二个输入提供了这种输出:

[null,null,null,null,null,{"name":"English Proficiency","label":"Below B2","score":12}]

如何获得正确的输出?至少有什么方法可以在同一转换中删除那些“空”元素吗?

arrays json apache-nifi jolt
1个回答
0
投票

问题是由于嵌套在

"matching-indicators"
对象中的键值对缺少方括号,应该是:
"@2": "[]"
而不是
"@2": ""
。该技术主要用于嵌套在数组中的单个对象,并且对于嵌套在数组中的多个对象也不会导致问题。

© www.soinside.com 2019 - 2024. All rights reserved.