Search code examples
jsonata

Transform array of values to array of key value pair


I have a json data which is in the form of key and all values in a array but I need to transform it into a array of key value pairs, here is the data

Source data

 "2022-08-30T06:58:56.573730Z": [
    { "tag": "AC 3 Phase/7957", "value": 161.37313113545272 },
    { "tag": "AC 3 Phase/7956", "value": 285.46869739695853 }
   
  ]
}

Transformation looking for

[
   { "tag": "AC 3 Phase/7957",
    "ts": 2022-08-30T06:58:56.573730Z,
    "value": 161.37313113545272
   },
   { "tag": "AC 3 Phase/7956",
    "ts": 2022-08-30T06:58:56.573730Z,
    "value": 285.46869739695853
   }
]

Solution

  • I would do it like this:

    $each($$, function($entries, $ts) {
      $entries.{
        "tag": tag,
        "ts": $ts,
        "value": value
      }
    }) ~> $reduce($append, [])
    

    Feel free to play with this example on the playground: https://stedi.link/g6qJGcP