Tokolosk
Tokolosk

Reputation: 37

JOLT moving from LHS to RHS and flattening

I am trying to convert the following JSON with JOLT but struggling, any help would be appreciated.

{
  "2021-04-14T00:00:00+02:00": {
    "249184": {
      "SRAD": null,
      "T": -50.00000000000001
    },
    "249185": {
      "SRAD": 0.46133333444595337,
      "T": null
    }
  },
  "2021-04-14T00:15:00+02:00": {
    "249184": {
      "SRAD": null,
      "T": -50.00000000000001
    },
    "249185": {
      "SRAD": 0.4593333303928375,
      "T": null
    }
  }
}

Desired output: Note here that the timestamps are repeated for each deviceID along with the SRAD and T values.

{
    "timestamp": "2021-04-14T00:00:00+02:00",
    "deviceID": 249184,
    "SRAD":null,
    "T":-50.00000000000001
},
{
    "timestamp": "2021-04-14T00:00:00+02:00",
    "deviceID": 249185,
    "SRAD":0.46133333444595337,
    "T":null
},
{
    "timestamp": "2021-04-14T00:15:00+02:00",
    "deviceID": 249184,
    "SRAD":null,
    "T":-50.00000000000001
},
{
    "timestamp": "2021-04-14T00:15:00+02:00",
    "deviceID": 249185,
    "SRAD": 0.4593333303928375,
    "T":null
}

I have tried a bunch of things but keep going in circles.

Upvotes: 0

Views: 537

Answers (2)

mattyb
mattyb

Reputation: 12083

Here's an alternate spec with 2 shifts that supports any number of fields inside the deviceId object:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "$(1)": "a[].timestamp",
          "$": "b[].deviceId",
          "@": "c[]"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "a": {
        "*": {
          "timestamp": "[&1].timestamp",
          "@(2,b[&])": "[&1]"
        }
      },
      "b": null,
      "c": {
        "*": {
          "*": "[&1].&"
        }
      }
    }
  }
]

Upvotes: 1

Jagadesh
Jagadesh

Reputation: 2116

[
  // Change null as String, as jolt will not 
  // process null values
  {
    "operation": "modify-default-beta",
    "spec": {
      "*": {
        "*": {
          "SRAD": "null",
          "T": "null"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "SRAD": "&4.SRAD",
          "T": "&4.T",
          "$0": "&4.deviceID",
          "$1": "&4.timestamp"
        }
      }
    }
  }, {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": {
            "@": "&"
          }
        }
      }
    }
  }, {
    "operation": "shift",
    "spec": {
      "*": {
        "0": "[&1].deviceID",
        "1": "[&1].timestamp",
        "2": "[&1].SRAD",
        "3": "[&1].T"
      }
    }
  }
]

You cannot process the JSON having null as value, so null is replaced with "null" using modify-default-beta operation.

Input JSON is grouped by timestamp and deviceid, so deconstruct the json by assigning names to the children nodes and then construct the output JSON.

Upvotes: 1

Related Questions