使用 JOLT 表达式转换 JSON

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

输入JSON

 {
  "zone_name" : "01 green",
  "zone_type_name" : "Green",
  "conditions" : {
    "timestamp" : "2024-05-15 10:23:52",
    "num_samples" : 12,
    "img_extent" : {
      "min_lng" : -5.877219,
      "max_lng" : -5.876672,
      "min_lat" : 54.220024,
      "max_lat" : 54.220338
    },
    "moisture_avg" : 33.97,
    "moisture_img" : "https://api.pogoturfpro.com/api/partner/maya/insight/5278277?attribute=M&key=05127be738e28884c2eba2585968eb687aa0cd20dadc299f5282ff6bf82f5e5b",
    "moisture_avg_color" : "#4EC3DA",
    "moisture_avg_rating" : "High",
    "ec_avg" : 0.2,
    "ec_img" : "https://api.pogoturfpro.com/api/partner/maya/insight/5278277?attribute=E&key=05127be738e28884c2eba2585968eb687aa0cd20dadc299f5282ff6bf82f5e5b",
    "ec_avg_color" : "#C2AE96",
    "ec_avg_rating" : "Low",
    "salinity_index_avg" : 0.59,
    "salinity_index_img" : "https://api.pogoturfpro.com/api/partner/maya/insight/5278277?attribute=S&key=05127be738e28884c2eba2585968eb687aa0cd20dadc299f5282ff6bf82f5e5b",
    "salinity_index_avg_color" : "#AF4440",
    "salinity_index_avg_rating" : "Critically Low",
    "surface_heat_f_avg" : 66.13,
    "surface_heat_f_img" : "https://api.pogoturfpro.com/api/partner/maya/insight/5278277?attribute=T&key=05127be738e28884c2eba2585968eb687aa0cd20dadc299f5282ff6bf82f5e5b",
    "surface_heat_f_avg_color" : "#2D882D",
    "surface_heat_f_avg_rating" : "Optimal",
    "soil_temp_f_avg" : 62.72,
    "soil_temp_f_img" : "https://api.pogoturfpro.com/api/partner/maya/insight/5278277?attribute=ST&key=05127be738e28884c2eba2585968eb687aa0cd20dadc299f5282ff6bf82f5e5b",
    "soil_temp_f_avg_color" : "#C2AE96",
    "soil_temp_f_avg_rating" : "Low",
    "notes" : [ ]
  },
  "pins" : [ ]
}

输出JSON

    {
"idgroup": "group(7750ceae-0528-44cb-9aeb-8865602d7f6f)",
"iduser": "user(84ca8fa3-199f-4a3e-9c86-699edce12846)"
"Time": "2024-05-15 10:23:52",
"VWC": 33.97
"EC": 0.2
"Temp_Soil": 17.06
"Temp_Soil_F": 62.72
"Latitude": -5.876945
"Longitude": 54.220181
"Rod_Length": "S"
"Soil_type": "D"
"VWC_Mode": "V"
}

idgroup、iduser、Rod_Length、Soil_type 和 VWC_Mode 是硬编码值

时间为时间戳
VWC 是湿度_avg
EC 是 ec_avg Temp_Soil_F是soil_temp_f_avg
Temp_Soil 为 celcius(soil_temp_f_avg) - 将 salt_temp_f_avg 转换为 celcius

纬度为 (min_lat+ max_lat) / 2
经度为 (min_lng + max_lng) / 2

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

您可以直接复制硬编码的键值对,同时执行modify规范中的所有计算。请注意,在所有键名称前添加一个通用文字(在本例中为

new_attr_
)前缀,以便能够轻松地拉入下一个类型为 shift 的规范,例如:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "new_attr_idgroup": "group(7750ceae-0528-44cb-9aeb-8865602d7f6f)",
      "new_attr_iduser": "user(84ca8fa3-199f-4a3e-9c86-699edce12846)",
      "conditions": {
        "new_attr_Time": "=(@(1,timestamp))",
        "new_attr_VMC": "=(@(1,moisture_avg))",
        "new_attr_EC": "=(@(1,ec_avg))",
        "Temp_Soil_F": "=(@(1,soil_temp_f_avg))",
        "nnew_attr_Latitude": "=doubleSum(@(1,img_extent.min_lat),@(1,img_extent.max_lat))",
        "new_attr_Latitude": "=divideAndRound(6,@(1,nnew_attr_Latitude),2)",
        "nnew_attr_Longitude": "=doubleSum(@(1,img_extent.min_lng),@(1,img_extent.max_lng))",
        "new_attr_Longitude": "=divideAndRound(6,@(1,nnew_attr_Longitude),2)"
      },
      "new_attr_Rod_Length": "S",
      "new_attr_Soil_type": "D",
      "new_attr_VWC_Mode": "V"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "new_attr_*": "&(0,1)",//extract the 1st replacement for he asterisk of the current(0th) level 
      "conditions": {
        "new_attr_*": "&(0,1)"
      }
    }
  }
]

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

enter image description here

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