How to perform group-by over JSON data using JSONATA

Ashish Sharma

I have a JSON structure given below. I want to perform a couple of transformations on this data. I came to know about JSONATA and it looks fantastic to me, but since I am new, I am struggling a little bit around transformation. I want to calculate the total of count (sum) for each module date for the given date. The expected result is given below.

Input:

{
    "id": "6332acbfe13e6063dcb740ef",
    "record": [
        {
            "date": "2022-09-22",
            "entries": [
                {
                    "repo": "SRE-MAC-PDM-TEAM",
                    "prod": [
                        {
                            "name": "modules/dynatrace",
                            "count": 16
                        }
                    ]
                },
                {
                    "repo": "SRE-MAC-IBO-BONUS-PAYMENTS-REPO",
                    "prod": [
                        {
                            "name": "modules/dynatrace/monitors/http-monitors/basic",
                            "count": 3
                        }
                    ]
                },
                {
                    "repo": "SRE-MAC-ANAPostPurchase",
                    "prod": [
                        {
                            "name": "modules/dynatrace",
                            "count": 17
                        }
                    ]
                },
                {
                    "repo": "SRE-MAC-ANA-SubscriptionsRewards",
                    "preprod": [
                        {
                            "name": "modules/splunk/hec-token",
                            "count": 8
                        },
                        {
                            "name": "modules/aws/lambda/logs_streaming_splunk",
                            "count": 8
                        }
                    ],
                    "prod": [
                        {
                            "name": "modules/dynatrace/alerting_profiles",
                            "count": 1
                        },
                        {
                            "name": "modules/dynatrace/monitors/http-monitors/basic",
                            "count": 1
                        },
                        {
                            "name": "modules/dynatrace/notifications/splunk-on-call",
                            "count": 1
                        },
                        {
                            "name": "modules/splunk/hec-token",
                            "count": 1
                        },
                        {
                            "name": "modules/aws/lambda/logs_streaming_splunk",
                            "count": 1
                        },
                        {
                            "name": "modules/dynatrace",
                            "count": 4
                        }
                    ]
                },
                {
                    "repo": "SRE-MAC-CSP-APPLICATION",
                    "preprod": [
                        {
                            "name": "modules/aws/lambda/logs_streaming_splunk",
                            "count": 9
                        }
                    ]
                },
                {
                    "repo": "SRE-MAC-IBOIP-REPO",
                    "prod": [
                        {
                            "name": "modules/dynatrace",
                            "count": 6
                        }
                    ]
                },
                {
                    "repo": "SRE-MAC-IBOLIFE-REPO",
                    "preprod": [
                        {
                            "name": "modules/dynatrace",
                            "count": 1
                        },
                        {
                            "name": "modules/splunk/hec-token",
                            "count": 2
                        },
                        {
                            "name": "modules/aws/lambda/logs_streaming_splunk",
                            "count": 3
                        }
                    ],
                    "prod": [
                        {
                            "name": "modules/dynatrace",
                            "count": 5
                        },
                        {
                            "name": "modules/splunk/hec-token",
                            "count": 1
                        },
                        {
                            "name": "modules/aws/lambda/logs_streaming_splunk",
                            "count": 1
                        }
                    ]
                },
                {
                    "repo": "SRE-MAC-PL-APPLICATION",
                    "preprod": [
                        {
                            "name": "modules/splunk/hec-token",
                            "count": 1
                        },
                        {
                            "name": "modules/aws/lambda/logs_streaming_splunk",
                            "count": 4
                        }
                    ],
                    "prod": [
                        {
                            "name": "modules/splunk/hec-token",
                            "count": 1
                        },
                        {
                            "name": "modules/aws/lambda/logs_streaming_splunk",
                            "count": 2
                        }
                    ]
                },
                {
                    "repo": "SRE-MAC-TPS-APPLICATION",
                    "preprod": [
                        {
                            "name": "modules/aws/lambda/logs_streaming_splunk",
                            "count": 1
                        }
                    ],
                    "prod": [
                        {
                            "name": "modules/dynatrace/alerting_profiles",
                            "count": 1
                        },
                        {
                            "name": "modules/dynatrace/notifications/splunk-on-call",
                            "count": 1
                        },
                        {
                            "name": "modules/dynatrace/calculated_service_metric",
                            "count": 1
                        },
                        {
                            "name": "modules/dynatrace/anomalies/custom/metric_id",
                            "count": 1
                        },
                        {
                            "name": "modules/dynatrace",
                            "count": 11
                        }
                    ]
                }
            ]
        },
        {
            "date": "2022-09-19",
            "entries": [
                {
                    "repo": "SRE-MAC-PDM-TEAM",
                    "prod": [
                        {
                            "name": "modules/dynatrace",
                            "count": 14
                        }
                    ]
                },
                {
                    "repo": "SRE-MAC-IBO-BONUS-PAYMENTS-REPO",
                    "prod": [
                        {
                            "name": "modules/dynatrace/monitors/http-monitors/basic",
                            "count": 3
                        }
                    ]
                },
                {
                    "repo": "SRE-MAC-ANAPostPurchase",
                    "prod": [
                        {
                            "name": "modules/dynatrace",
                            "count": 15
                        }
                    ]
                },
                {
                    "repo": "SRE-MAC-ANA-SubscriptionsRewards",
                    "preprod": [
                        {
                            "name": "modules/splunk/hec-token",
                            "count": 5
                        },
                        {
                            "name": "modules/aws/lambda/logs_streaming_splunk",
                            "count": 5
                        }
                    ],
                    "prod": [
                        {
                            "name": "modules/dynatrace/alerting_profiles",
                            "count": 1
                        },
                        {
                            "name": "modules/dynatrace/monitors/http-monitors/basic",
                            "count": 1
                        },
                        {
                            "name": "modules/dynatrace/notifications/splunk-on-call",
                            "count": 1
                        },
                        {
                            "name": "modules/splunk/hec-token",
                            "count": 1
                        },
                        {
                            "name": "modules/aws/lambda/logs_streaming_splunk",
                            "count": 1
                        },
                        {
                            "name": "modules/dynatrace",
                            "count": 3
                        }
                    ]
                },
                {
                    "repo": "SRE-MAC-CSP-APPLICATION",
                    "preprod": [
                        {
                            "name": "modules/aws/lambda/logs_streaming_splunk",
                            "count": 6
                        }
                    ]
                },
                {
                    "repo": "SRE-MAC-IBOIP-REPO",
                    "prod": [
                        {
                            "name": "modules/dynatrace",
                            "count": 6
                        }
                    ]
                },
                {
                    "repo": "SRE-MAC-IBOLIFE-REPO",
                    "preprod": [
                        {
                            "name": "modules/dynatrace",
                            "count": 1
                        },
                        {
                            "name": "modules/splunk/hec-token",
                            "count": 2
                        },
                        {
                            "name": "modules/aws/lambda/logs_streaming_splunk",
                            "count": 3
                        }
                    ],
                    "prod": [
                        {
                            "name": "modules/dynatrace",
                            "count": 4
                        },
                        {
                            "name": "modules/splunk/hec-token",
                            "count": 1
                        },
                        {
                            "name": "modules/aws/lambda/logs_streaming_splunk",
                            "count": 1
                        }
                    ]
                },
                {
                    "repo": "SRE-MAC-PL-APPLICATION",
                    "preprod": [
                        {
                            "name": "modules/splunk/hec-token",
                            "count": 1
                        },
                        {
                            "name": "modules/aws/lambda/logs_streaming_splunk",
                            "count": 3
                        }
                    ],
                    "prod": [
                        {
                            "name": "modules/splunk/hec-token",
                            "count": 1
                        },
                        {
                            "name": "modules/aws/lambda/logs_streaming_splunk",
                            "count": 2
                        }
                    ]
                },
                {
                    "repo": "SRE-MAC-TPS-APPLICATION",
                    "preprod": [
                        {
                            "name": "modules/aws/lambda/logs_streaming_splunk",
                            "count": 1
                        }
                    ],
                    "prod": [
                        {
                            "name": "modules/dynatrace/alerting_profiles",
                            "count": 1
                        },
                        {
                            "name": "modules/dynatrace/notifications/splunk-on-call",
                            "count": 1
                        },
                        {
                            "name": "modules/dynatrace/calculated_service_metric",
                            "count": 1
                        },
                        {
                            "name": "modules/dynatrace/anomalies/custom/metric_id",
                            "count": 1
                        },
                        {
                            "name": "modules/dynatrace",
                            "count": 9
                        }
                    ]
                }
            ]
        },
        {
            "date": "2022-09-12",
            "entries": [
                {
                    "repo": "SRE-MAC-PDM-TEAM",
                    "prod": [
                        {
                            "name": "modules/dynatrace",
                            "count": 4
                        }
                    ]
                },
                {
                    "repo": "SRE-MAC-IBO-BONUS-PAYMENTS-REPO",
                    "prod": [
                        {
                            "name": "modules/dynatrace/monitors/http-monitors/basic",
                            "count": 2
                        }
                    ]
                },
                {
                    "repo": "SRE-MAC-ANAPostPurchase",
                    "prod": [
                        {
                            "name": "modules/dynatrace",
                            "count": 3
                        }
                    ]
                },
                {
                    "repo": "SRE-MAC-ANA-SubscriptionsRewards",
                    "preprod": [
                        {
                            "name": "modules/splunk/hec-token",
                            "count": 3
                        },
                        {
                            "name": "modules/aws/lambda/logs_streaming_splunk",
                            "count": 4
                        }
                    ],
                    "prod": [
                        {
                            "name": "modules/dynatrace/alerting_profiles",
                            "count": 1
                        },
                        {
                            "name": "modules/dynatrace/monitors/http-monitors/basic",
                            "count": 1
                        },
                        {
                            "name": "modules/dynatrace/notifications/splunk-on-call",
                            "count": 1
                        },
                        {
                            "name": "modules/splunk/hec-token",
                            "count": 1
                        },
                        {
                            "name": "modules/aws/lambda/logs_streaming_splunk",
                            "count": 1
                        },
                        {
                            "name": "modules/dynatrace",
                            "count": 2
                        }
                    ]
                },
                {
                    "repo": "SRE-MAC-CSP-APPLICATION",
                    "preprod": [
                        {
                            "name": "modules/aws/lambda/logs_streaming_splunk",
                            "count": 4
                        }
                    ]
                },
                {
                    "repo": "SRE-MAC-IBOIP-REPO",
                    "prod": [
                        {
                            "name": "modules/dynatrace",
                            "count": 3
                        }
                    ]
                },
                {
                    "repo": "SRE-MAC-IBOLIFE-REPO",
                    "preprod": [
                        {
                            "name": "modules/dynatrace",
                            "count": 1
                        },
                        {
                            "name": "modules/splunk/hec-token",
                            "count": 2
                        },
                        {
                            "name": "modules/aws/lambda/logs_streaming_splunk",
                            "count": 2
                        }
                    ],
                    "prod": [
                        {
                            "name": "modules/dynatrace",
                            "count": 3
                        },
                        {
                            "name": "modules/splunk/hec-token",
                            "count": 1
                        },
                        {
                            "name": "modules/aws/lambda/logs_streaming_splunk",
                            "count": 1
                        }
                    ]
                },
                {
                    "repo": "SRE-MAC-PL-APPLICATION",
                    "preprod": [
                        {
                            "name": "modules/splunk/hec-token",
                            "count": 1
                        },
                        {
                            "name": "modules/aws/lambda/logs_streaming_splunk",
                            "count": 2
                        }
                    ],
                    "prod": [
                        {
                            "name": "modules/splunk/hec-token",
                            "count": 1
                        },
                        {
                            "name": "modules/aws/lambda/logs_streaming_splunk",
                            "count": 2
                        }
                    ]
                },
                {
                    "repo": "SRE-MAC-TPS-APPLICATION",
                    "preprod": [
                        {
                            "name": "modules/aws/lambda/logs_streaming_splunk",
                            "count": 1
                        }
                    ],
                    "prod": [
                        {
                            "name": "modules/dynatrace/alerting_profiles",
                            "count": 1
                        },
                        {
                            "name": "modules/dynatrace/notifications/splunk-on-call",
                            "count": 1
                        },
                        {
                            "name": "modules/dynatrace/calculated_service_metric",
                            "count": 1
                        },
                        {
                            "name": "modules/dynatrace/anomalies/custom/metric_id",
                            "count": 1
                        },
                        {
                            "name": "modules/dynatrace",
                            "count": 7
                        }
                    ]
                }
            ]
        },
        {
            "date": "2022-09-05",
            "entries": [
                {
                    "repo": "SRE-MAC-PDM-TEAM",
                    "prod": [
                        {
                            "name": "modules/dynatrace",
                            "count": 12
                        }
                    ]
                },
                {
                    "repo": "SRE-MAC-IBO-BONUS-PAYMENTS-REPO",
                    "prod": [
                        {
                            "name": "modules/dynatrace/monitors/http-monitors/basic",
                            "count": 1
                        }
                    ]
                },
                {
                    "repo": "SRE-MAC-ANAPostPurchase",
                    "prod": [
                        {
                            "name": "modules/dynatrace",
                            "count": 11
                        }
                    ]
                },
                {
                    "repo": "SRE-MAC-ANA-SubscriptionsRewards",
                    "preprod": [
                        {
                            "name": "modules/splunk/hec-token",
                            "count": 2
                        },
                        {
                            "name": "modules/aws/lambda/logs_streaming_splunk",
                            "count": 5
                        }
                    ],
                    "prod": [
                        {
                            "name": "modules/dynatrace/alerting_profiles",
                            "count": 1
                        },
                        {
                            "name": "modules/dynatrace/monitors/http-monitors/basic",
                            "count": 1
                        },
                        {
                            "name": "modules/dynatrace/notifications/splunk-on-call",
                            "count": 1
                        },
                        {
                            "name": "modules/splunk/hec-token",
                            "count": 1
                        },
                        {
                            "name": "modules/aws/lambda/logs_streaming_splunk",
                            "count": 1
                        },
                        {
                            "name": "modules/dynatrace",
                            "count": 1
                        }
                    ]
                },
                {
                    "repo": "SRE-MAC-CSP-APPLICATION",
                    "preprod": [
                        {
                            "name": "modules/aws/lambda/logs_streaming_splunk",
                            "count": 5
                        }
                    ]
                },
                {
                    "repo": "SRE-MAC-IBOIP-REPO",
                    "prod": [
                        {
                            "name": "modules/dynatrace",
                            "count": 3
                        }
                    ]
                },
                {
                    "repo": "SRE-MAC-IBOLIFE-REPO",
                    "preprod": [
                        {
                            "name": "modules/dynatrace",
                            "count": 1
                        },
                        {
                            "name": "modules/splunk/hec-token",
                            "count": 1
                        },
                        {
                            "name": "modules/aws/lambda/logs_streaming_splunk",
                            "count": 1
                        }
                    ],
                    "prod": [
                        {
                            "name": "modules/dynatrace",
                            "count": 2
                        },
                        {
                            "name": "modules/splunk/hec-token",
                            "count": 1
                        },
                        {
                            "name": "modules/aws/lambda/logs_streaming_splunk",
                            "count": 1
                        }
                    ]
                },
                {
                    "repo": "SRE-MAC-PL-APPLICATION",
                    "preprod": [
                        {
                            "name": "modules/splunk/hec-token",
                            "count": 1
                        },
                        {
                            "name": "modules/aws/lambda/logs_streaming_splunk",
                            "count": 1
                        }
                    ],
                    "prod": [
                        {
                            "name": "modules/splunk/hec-token",
                            "count": 1
                        },
                        {
                            "name": "modules/aws/lambda/logs_streaming_splunk",
                            "count": 1
                        }
                    ]
                },
                {
                    "repo": "SRE-MAC-TPS-APPLICATION",
                    "preprod": [
                        {
                            "name": "modules/aws/lambda/logs_streaming_splunk",
                            "count": 1
                        }
                    ],
                    "prod": [
                        {
                            "name": "modules/dynatrace/alerting_profiles",
                            "count": 1
                        },
                        {
                            "name": "modules/dynatrace/notifications/splunk-on-call",
                            "count": 1
                        },
                        {
                            "name": "modules/dynatrace/calculated_service_metric",
                            "count": 1
                        },
                        {
                            "name": "modules/dynatrace/anomalies/custom/metric_id",
                            "count": 1
                        },
                        {
                            "name": "modules/dynatrace",
                            "count": 2
                        }
                    ]
                }
            ]
        }
    ],
    "metadata": {
        "name": "",
        "readCountRemaining": 98,
        "timeToExpire": 86389,
        "createdAt": "2022-09-27T07:56:47.003Z"
    }
}

Expected Output:

[
"2022-09-22" : [
{
"name" : "modules/dynatrace",
"count": 11
},
{
"name" : "modules/dynatrace",
"count": 4
},
{
"name" : "modules/splunk/hec-token",
"count": 14
}
....
],
"2022-09-19" : [
{
"name" : "modules/dynatrace",
"count": 52
},
.....
]
.
.
.
]
mralex

This should do the grouping for you:

(
  record{
    date: $reduce(entries.prod.name, function($acc, $item) {
      $merge([$acc, { 
        $item: $count(entries.prod.name[$=$item])
      }])
    }, {}) ~> $each(function($value, $key) {
      { "name": $key, "count": $value }
    })
  }
)

You can check it out live here: https://stedi.link/LiOttdB

UPDATE - even shorter solution:

(
  record{
    date: entries.*{ $.name: $sum($.count) }
      ~> $each(function($c, $n) {{ "name": $n, "count": $c }})
  }
)

https://stedi.link/pQiCnJo

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to group the JSON object by a key using JSONata

How to perform a text search over JSON data in Flutter?

How to perform join over date ranges using data.table?

How to create table separate objects out of json using JSONATA

How to append an array nested inside JSON object using JSONata

JSONata (or JS) - group and sum JSON array / objects

How to use JSONata to change JSON response

How to use JSONata to change JSON response restructure

How do I group a group result with a list in JSONata?

How to perform group function over an outer join query?

Group Values in JSONata

Group by for json data using jquery

With Prisma, how to perform a distinct over a specific field of a json column

How to perform set operations by Oracle SQL over static DATA sets?

How to group Json data based on Month and plot it using google chart

How to Group JSON data using identicel key value?

Using Pandas, how to group a JSON data by day and count the occurrences?

Processing a JSON file using JSONata in Node.JS returns undefined

How to write JSONata query to remove duplicate values from some tags in json and to get unique value of each object using jsaonata

How to perform a dry-run using rsync over ssh?

JSON transformation with JSONata

How to perform group by in python?

How to mutate a data frame to perform a function where the reference group changes?

How to Perform addition of a group of data to another in the Same table and Column?

Django: Perform GROUP BY over a view queryset

how to get objects which param contains substring in value using JSONata?

How can I add to an existing object using JSONata?

How to update single object property using JSONata transform operator

count matches using JSONata