Search code examples
jsonapache-nifijolt

Transform a JSON using JOLT expression


Input 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": []
}

Output 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 are hardcoded values

Time is timestamp
VWC is moisture_avg
EC is ec_avg Temp_Soil_F is soil_temp_f_avg
Temp_Soil is celcius(soil_temp_f_avg) - convert soil_temp_f_avg to celsius

Latitude is (min_lat+ max_lat) / 2
Longitude is (min_lng + max_lng) / 2


Solution

  • You can directly copy the hardcoded key-value pairs while performing computations all in the modify spec. Notice to prefix all their key names with a common literal( new_attr_ in this case ) in order to be able to easily pull within the next spec which will be of type shift such as :

    [
      {
        "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)"
          }
        }
      }
    ]
    

    the demo on the site https://jolt-demo.appspot.com/ is :

    enter image description here