如何使用JOLT将JSON转换为其他JSON格式

qcbq4gxm  于 2023-08-08  发布在  其他
关注(0)|答案(1)|浏览(127)

输入JSON

{
  "version": "2.0",
  "terms_of_service": "2022 CCC",
  "frequency": "hourly",
  "references": {
    "stations": [
      {
        "sid": "46",
        "poste": "Colombo",
        "longitude": "80.29941",
        "latitude": "10.30466",
        "altitude": "100.0"
      }
    ]
  },
  "results": [
    {
      "mtime": "2023-03-14T00:00:00Z",
      "sid": "46",
      "tsa": "8.8",
      "tha": "7.7",
      "hra": "86.3",
      "tsf": null,
      "tss": null,
      "ens": "0.0",
      "dvt": null,
      "vvt": "4.6",
      "plu": "0.1",
      "hct": "60.0"
    }
  ]
}

字符串
输出JSON

{
  "Date": "2023-03-14 01:00:00",
  "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"
    },
    {
      "code": "ens",
      "value": "0"
    },
    {
      "code": "dvt",
      "value": "0"
    },
    {
      "code": "vvt",
      "value": "4.8"
    },
    {
      "code": "plu",
      "value": "0.1"
    },
    {
      "code": "hct",
      "value": "26.0"
    }
  ],
  "stationname": "Colombo"
}


如输出所示,我需要将我的度量值转换为键值对。另外,我需要从键值对中删除sid,但从嵌套在stations数组中的poste属性的值Map相关名称,并在输出中显示“stationname”。我还需要将空值转换为零提前感谢!

lyr7nygr

lyr7nygr1#

您可以使用以下转换

[
  {
    "operation": "shift",
    "spec": {
      "results": {
        "*": {
          "mtime": "Date", // rename the attribute
          "sid": "&", // the "sid" attribute will vanish later by separating here
          "*": "data.&"
        }
      },
      "@references.stations[0].poste": "stationname"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "data": {
        "*": { // loop through all the attributes within the "data" array
          "$": "data[#2].code",
          "@": "data[#2].value"
        }
      }
    }
  },
  { // just to sort the elements in the deisred order
    "operation": "shift",
    "spec": {
      "Date|data": "&",
      "stationname": "&"
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "data": {
        "*": {
          "value": ["=notNull", 0] // else case, eg. If "Not Not Null", eg. If "Null", then zero
        }
      },
      "DPortion1": "=substring(@(1,Date),0,10)",
      "DPortion2": "=substring(@(1,Date),11,19)",
      "Date": "=concat(@(1,DPortion1),' ',@(1,DPortion2))" // overwrite the Date's value by portions 
    }
  },
  { // get rid of the DPortion parameters
    "operation": "remove",
    "spec": {
      "DPortion*": ""
    }
  }
]

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


的数据

相关问题