JOLT transformation to flatten the entire json document with help of

1.1k views Asked by At

i have below json :

     {
    "deveui": "1000",
    "vars": [
        { "name": "CfgNo",
           "value":"255" },
        {
            "name":"Status","value":{
                "lampS": "unknown(0xff)",
                "lampL": "255"
            }
        },
        { "name" : "Volt", "value": "255" },
        {
            "name" : "gps", "value": {
                "lat": "12.93",
                "lon": "77.69"
             }
        },
        {"name" : "status", "value":"up"},
        {"name" : "last_status_change","value": 1503 }
    ]
} 

expected output is :

{[{  "deveui": "1000",
  "CfgNo": "255",
  "lampS": "unknown(0xff)",
  "lampL": "255",
  "Volt": "255",
  "lat": "12.93",
  "lon": "77.69",
  "status": "up",
  "last_status_change": 1503 
}]}

is it possible to convert into expected output format. if so please help me to design spec.

1

There are 1 answers

0
Milo S On

It looks like it should be simple, but Shift can't match something differently if it is a map or a scalar. In this case, "value" in your input is sometimes a scalar, and sometime a nested map.

If you know the "names" that will be multi values, then Jolt can help.

Spec

[
  {
    // Shift can't vary its behavior between 
    //  the "value" key in the input being a 
    //  scalar or a map.
    // Thus the only way to do the desired flattening
    //  is if we know the names that will 
    //  be multi-valued.
    "operation": "shift",
    "spec": {
      // pass deveui thru
      "deveui": "deveui",
      "vars": {
        "*": { // array index of vars[]
          "name": {
            // match value of name Status or gps
            "Status|gps": {
              // We know these entries have "value" that is map.
              // For these, go back up the tree, 3 levels (0,1,2)
              // and come back down to the "value" entry.
              "@(2,value)": {
                // for each key in the value, pass it thru
                // to the output.
                "*": "&"
              }
            },
            "*": {
              // for all other values of "name" that are not
              //  Status or gps, just go back up the tree
              //  and grab the scalear "value" and write it
              //  to the output at the matched value of name.
              "@(2,value)": "&1"
            }
          }
        }
      }
    }
  }
]