Home > Software design >  How to create table separate objects out of json using JSONATA
How to create table separate objects out of json using JSONATA

Time:09-29

I want to transform a given JSON structure into another json format using the JSONATA API. Basically, I need to break the hierarchical structure into separate records.

Input JSON:

{
  "2022-09-22": [
    {
      "name": "modules/dynatrace",
      "count": 60
    },
    {
      "name": "modules/dynatrace/monitors/http-monitors/basic",
      "count": 4
    },
    {
      "name": "modules/splunk/hec-token",
      "count": 14
    },
    {
      "name": "modules/aws/lambda/logs_streaming_splunk",
      "count": 29
    }
  ]
}

Output:

[
{
"date" : "2022-09-22",
"name": "modules/dynatrace",
"count": 60
},
{
"date" : "2022-09-22",
"name": "modules/dynatrace/monitors/http-monitors/basic",
"count": 4
},
{
"date" : "2022-09-22",
"name": "modules/splunk/hec-token",
"count": 14
},
{
"date" : "2022-09-22",
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 29
}
]

CodePudding user response:

You can use the $each function to convert object into an array:

$each($$, function($entries, $date) {
  $entries.($merge([{ "date": $date }, $]))
})

Interactive link: https://stedi.link/ZBoBY2F

  • Related