如何使用 JOLT 表示法将 JSON 转换为另一种 JSON 格式

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

输入 JSON :

{
  "sid": "1",
  "poste": "MapRecord[{altitude=473.0, latitude=49.92665, poste=Libramont, longitude=5.36080}]",
  "mtime": "2023-07-12T01:00:00Z",
  "tsa": "17.2",
  "tha": "16.2",
  "hra": "90.2",
  "tsf": "15.8",
  "tss": "21.4",
  "ens": "0.0",
  "dvt": "319.6",
  "vvt": "0.9",
  "plu": "0.0",
  "hct": "60.0"
}

所需的输出 JSON :

{
  "Date": "2023-03-14 01:00:00",
  "stationname": "Libramont",
  "data": [
    {
      "code": "tsa",
      "value": "9.0"
    },
    {
      "code": "tha",
      "value": "7.8"
    },
    {
      "code": "hra",
      "value": "85.0"
    },
    {
      "code": "tsf",
      "value": "0.0"
    },
    {
      "code": "tss",
      "value": "0.0"
    },
    {
      "code": "ens",
      "value": "0.0"
    },
    {
      "code": "dvt",
      "value": "0"
    },
    {
      "code": "vvt",
      "value": "4.8"
    },
    {
      "code": "plu",
      "value": "0.1"
    },
    {
      "code": "hct",
      "value": "26.0"
    }
  ]
}

如输出所示,我只需将我的度量转换为键值对(删除 poste 信息)。另外,我需要从显示为“stationname”的 poste 属性映射相关名称,就像我的输出中一样。我还需要将空值转换为零并正确格式化日期。感谢提前!

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

您可以使用以下转换

[
  { // extract the value of "poste" to overwrite
    "operation": "modify-overwrite-beta",
    "spec": {
      "po1": "=split('poste=',@(1,poste))",
      "po2": "=lastElement(@(1,po1))",
      "po3": "=split(',',@(1,po2))",
      "stationname": "=firstElement(@(1,po3))"
    }
  },
  { // get rid of the attributes those start with "po"
    "operation": "remove",
    "spec": {
      "po*": ""
    }
  },
  {
    "operation": "shift",
    "spec": {
      "mtime": "Date", // rename the attribute
      "sta*": "&",
      "sid": "&", // the "sid" attribute will vanish later by separating here
      "*": "data.&"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "Date|stationname": "&",
      "data": {
        "*": { // loop through all the attributes within the "data" array
          "$": "&2[#2].code",
          "@": "&2[#2].value"
        }
      }
    }
  }
]

网站上的 演示 http://jolt-demo.appspot.com/ 是:

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