Search code examples
jsonapache-nifijolt

Modify JOLT Expression to get Output JSON


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

Expected Output 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"
  }
]

Problem - My Current JOLT expression gives the output with all the values in one JSON record. I need to have separate JSON for each record as in the Output JSON. Below is my current JOLT expression

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

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

Solution

  • Seems that you need identifier of [#<int>] type in order to prefix the RHS expressions within the shift tranformation such as

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

    where

    • 2 levels of traversing the layers ( counted once for :, once for { )
    • 3 levels of traversing the layers ( counted once for :, twice for { )

    needed for such type of identifier in order to reach the level of the outermost indexes.

    or [#2] might be replaced with [&1] and [#3] might be replaced with [&2] as an alternative

    eg.

    • indexes of [#<int>] type identifiers are counted as 1,2,3...

    • while [&<int>] type ones are counted as 0,1,2...

    for their respective traverses