skip to Main Content

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

2

Answers


  1. [{
    "operation": "shift",
    "spec": {
      "0": {
        "head": "head",
        "foot": "foot"
      },
      "1": {
        "Properties": {
          "*": {
            "Property": {
              "*": {
                "StartTime": {
                  "*": {
                    "Data": {
                      "*": {
                        "GPSY": "[&5].Y",
                        "GPSX": "[&5].X",
                        "Val": "[&5].@(2,Metric)",
                        "Metric": {
                          "temp": "[&4].Temp_F",
                          "mvw": "[&4].mvw",
                          "Hum": "[&4].Hum"
                        },
                        "@(2,head)": "[&4].head",
                        "@(2,foot)": "[&4].foot"
                      }
                    },
                    "@": {
                      "": "[&3].Time"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }},{
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "Temp_C": "=doubleSum(@(1,Temp_F),-32)",
        "Temp": "=divideAndRound(3,@(1,Temp_C),1.8)",
        "Temp_F": "=toDouble",
        "mvw": "=toDouble",
        "Hum": "=toInteger"
      }
    }},{
    "operation": "default",
    "spec": {
      "*": {
        "Road_1": "SD",
        "Road_2": "SF"
      }
    }},{
    "operation": "shift",
    "spec": {
      "*": {
        "Time": "&1.Time",
        "Y": "&1.Y",
        "X": "&1.X",
        "mvw": "&1.mvw",
        "Hum": "&1.Hum",
        "Temp": "&1.Temp",
        "Temp_F": "&1.Temp_F",
        "Road_1": "&1.Road_1",
        "Road_2": "&1.Road_2",
        "head": "&1.head",
        "foot": "&1.foot"
      }
    }},{
    "operation": "cardinality",
    "spec": {
      "*": "ONE"
    }},{
    "operation": "shift",
    "spec": {
      "*": "[]"
    }},{
    "operation": "remove",
    "spec": {
      "*": {
        "wat": "",
        "Temp_C": ""
      }
    }}]
    

    This Jolt spec will produce the desired output JSON with the head and foot attributes included.

    Login or Signup to reply.
  2. 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": {
          "*": "[]"
        }
      }
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search