修改 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": []
      },
      {
        "zone_name": "02 Green",
        "zone_type_name": "Green",
        "conditions": {
          "timestamp": "2024-05-15 10:35:35",
          "num_samples": 12,
          "img_extent": {
            "min_lng": -5.87333,
            "max_lng": -5.872946,
            "min_lat": 54.222294,
            "max_lat": 54.222509
          },
          "moisture_avg": 34.54,
          "moisture_img": "https://api.pogoturfpro.com/api/partner/maya/insight/5278268?attribute=M&key=05127be738e28884c2eba2585968eb687aa0cd20dadc299f5282ff6bf82f5e5b",
          "moisture_avg_color": "#1C5195",
          "moisture_avg_rating": "Critically High",
          "ec_avg": 0.21,
          "ec_img": "https://api.pogoturfpro.com/api/partner/maya/insight/5278268?attribute=E&key=05127be738e28884c2eba2585968eb687aa0cd20dadc299f5282ff6bf82f5e5b",
          "ec_avg_color": "#2D882D",
          "ec_avg_rating": "Optimal",
          "salinity_index_avg": 0.6,
          "salinity_index_img": "https://api.pogoturfpro.com/api/partner/maya/insight/5278268?attribute=S&key=05127be738e28884c2eba2585968eb687aa0cd20dadc299f5282ff6bf82f5e5b",
          "salinity_index_avg_color": "#AF4440",
          "salinity_index_avg_rating": "Critically Low",
          "surface_heat_f_avg": 64.07,
          "surface_heat_f_img": "https://api.pogoturfpro.com/api/partner/maya/insight/5278268?attribute=T&key=05127be738e28884c2eba2585968eb687aa0cd20dadc299f5282ff6bf82f5e5b",
          "surface_heat_f_avg_color": "#C2AE96",
          "surface_heat_f_avg_rating": "Low",
          "soil_temp_f_avg": 61.85,
          "soil_temp_f_img": "https://api.pogoturfpro.com/api/partner/maya/insight/5278268?attribute=ST&key=05127be738e28884c2eba2585968eb687aa0cd20dadc299f5282ff6bf82f5e5b",
          "soil_temp_f_avg_color": "#C2AE96",
          "soil_temp_f_avg_rating": "Low",
          "notes": []
        },
        "pins": []
      }
    ]

预期输出 JSON

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

问题 - 我当前的 JOLT 表达式给出的输出包含一条 JSON 记录中的所有值。我需要为每个记录提供单独的 JSON,如输出 JSON 中所示。下面是我现在的 JOLT 表情

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

时间为时间戳

VWC 是湿度_avg

EC 是 ec_avg

Temp_Soil_F是soil_temp_f_avg

Temp_Soil 为摄氏度(soil_temp_f_avg) - 将土壤温度_f_avg 转换为摄氏度

纬度为 (min_lat+ max_lat) / 2

经度为 (min_lng + max_lng)/2

    [
      {
        "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))",
              "new_attr_Temp_Soil_F": "=(@(1,soil_temp_f_avg))",
              "Temp_Cel": "=doubleSum(@(1,soil_temp_f_avg),-32)",
              "new_attr_Temp_Soil": "=divideAndRound(3,@(1,soil_temp_f_avg),1.8)",
              "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)",
            "conditions": {
              "new_attr_*": "&(0,1)"
            }
          }
        }
      }
    ]
json apache-nifi jolt
1个回答
0
投票

似乎您需要

[#<int>]
类型的标识符,以便在 shift 转换中为 RHS 表达式添加前缀,例如

  {
    "operation": "shift",
    "spec": {
      "*": {
        "new_attr_*": "[#2].&(0,1)",
        "conditions": {
          "new_attr_*": "[#3].&(0,1)"
        }
      }
    }
  }

哪里

  • 2层遍历(一次为
    :
    ,一次为
    {
  • 3级遍历(一次
    :
    ,两次
    {

需要此类类型的标识符

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