Search code examples
jsonjolt

Addition Attributes into existing JOLT


I have following json input and I need to get 2 additional attributes (head,foot) into the output json using existing Jolt. How I can do that?

JSONs and JOLT scripts are attached.

Input JSON:

[
  {
    "PropertyName": "KER ZONE 1",
    "head": "13084",
    "foot": "48b4e"
  },
  {
    "Properties": [
      {
        "PropertyName": "KER ZONE 1",
        "Property": [
          {
            "StartTime": "2023-04-03",
            "Data": [
              {
                "GPSY": "45.28",
                "GPSX": "-3.24",
                "Val": "11.0",
                "Metric": "mvw"
              },
              {
                "GPSY": "45.28",
                "GPSX": "-3.24",
                "Val": "2231",
                "Metric": "Hum"
              },
              {
                "GPSY": "45.28",
                "GPSX": "-3.24",
                "Val": "0.00",
                "Metric": "wat"
              },
              {
                "GPSY": "45.28",
                "GPSX": "-3.24",
                "Val": "58.82",
                "Metric": "temp"
              },
              {
                "GPSY": "33.27",
                "GPSX": "-4.29",
                "Val": "0.00",
                "Metric": "wat"
              },
              {
                "GPSY": "33.27",
                "GPSX": "-4.29",
                "Val": "9.8",
                "Metric": "mvw"
              },
              {
                "GPSY": "33.27",
                "GPSX": "-4.29",
                "Val": "2206",
                "Metric": "Hum"
              },
              {
                "GPSY": "33.27",
                "GPSX": "-4.29",
                "Val": "58.28",
                "Metric": "temp"
              }
            ]
          }
        ]
      }
    ],
    "CallStatus": "1"
  }
]

Output JSON:

[
  {
    "Hum": "2231",
    "Road_1": "SD",
    "Road_2": "SF",
    "Temp": 14.9,
    "Temp_F": 58.82,
    "Time": "2023-04-03",
    "X": "-3.24",
    "Y": "45.28",
    "mvw": 11,
    "head": "13084",
    "foot": "48b4e"
  },
  {
    "Hum": "2206",
    "Road_1": "SD",
    "Road_2": "SF",
    "Temp": 14.6,
    "Temp_F": 58.28,
    "Time": "2023-04-03",
    "X": "-4.29",
    "Y": "33.27",
    "mvw": 9.8,
    "head": "13084",
    "foot": "48b4e"
  }
]

JOLT:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": {
            "*": {
              "*": {
                "*": {
                  "@2,StartTime": "@1,GPSY.@1,GPSX.Time",
                  "GPS*": "@(1,GPSY).@(1,GPSX).&",
                  "@Val": "@(1,GPSY).@(1,GPSX).@Metric"
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": {
          "Temp_C": "=doubleSum(@(1,temp),-32)",
          "Temp": "=divideAndRound(3,@(1,Temp_C),1.8)",
          "temp": "=toDouble",
          "mvw": "=toDouble",
          "hum": "=toInteger"
        }
      }
    }
  },
  { //Hard coded values
    "operation": "default",
    "spec": {
      "*": {
        "*": {
          "Road_1": "SD",
          "Road_2": "SF"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": "&2.&3.&",
          "GPSY": "&2.&3.Y",
          "GPSX": "&2.&3.X",
          "mvw": "&2.&3.mvw",
          "hum": "&2.&3.Hum",
          "temp": "&2.&3.Temp_F"
        }
      }
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": {
          "*": "ONE"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "[]"
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "*": {
        "wat": "",
        "Temp_C": ""
      }
    }
  }
]

Thank you


Solution

  • You can use the following specs :

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "head|foot": "Outer.&",//pick the desired attributes only and nest them in a common object
            "Properties": {
              "*": {
                "*": {
                  "*": {
                    "*": {
                      "*": {
                        "@Val": "&6.@1,GPSY.@1,GPSX.Val.@Metric", //match all Metric vs. Val in order to prepare for filtering out by "Hum" within the next spec 
                        "@2,StartTime": "&6.@1,GPSY.@1,GPSX.Time",
                        "#SD": "&6.@1,GPSY.@1,GPSX.Road_1",
                        "#SF": "&6.@1,GPSY.@1,GPSX.Road_2",
                        "*PS*": "&6.@1,GPSY.@1,GPSX.&(0,2)"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "Properties": {
            "*": {
              "*": {
                "Val": {
                  "Tmp_C": "=doubleSum(@(1,temp),-32)",
                  "Temp": "=divideAndRound(3,@(1,Tmp_C),1.8)",
                  "mvw": "=toDouble",
                  "Temp_F": "=toDouble(@(1,temp))",
                  "Hum": "=toInteger"
                }
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "P*": {
            "*": {
              "*": {
                "@3,Outer": { "*": "&3_&2.&" },
                "Val": {
                  "Hum|mvw|Tem*": "&3_&2.&"
                },
                "*": {
                  "0": "&3_&2.&1" //pick only one component up from the repeating multiple values from each array
                }
              }
            }
          }
        }
      },
      { //convert to array of objects with no keys 
        "operation": "shift",
        "spec": {
          "*": "[]"
        }
      }
    ]