# Help with JSONata grouping

**URL:** https://discourse.nodered.org/t/help-with-jsonata-grouping/9629
**Category:** General
**Created:** [31 March 2019 10:29 UTC](https://discourse.nodered.org/t/help-with-jsonata-grouping/9629 "2019-03-31T10:29:37Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [31 March 2019 10:29 UTC](https://discourse.nodered.org/t/help-with-jsonata-grouping/9629/1 "2019-03-31T10:29:37Z")

</div>

This is not necessarily related to node-red, but the JSONata knowledge is here.

I have the following object:

```auto
{
	"dates": [{
			"datetime": "2018-06-01 08:14:00"
		},
		{
			"datetime": "2018-06-01 08:16:00"
		},
		{
			"datetime": "2018-06-01 18:14:00"
		},
		{
			"datetime": "2018-06-01 02:14:00"
		},
		{
			"datetime": "2018-06-02 12:14:00"
		},
		{
			"datetime": "2018-06-04 14:14:00"
		}

	]
}

```

I am trying to get output that produces:  
per unique date \> count items in groups between a:08:00-17:00, b:17:00-23:00, c:23:00-08:00

I have something like

```auto
dates{$substring(datetime,0,10):{"a":{$substring(datetime,11,2):$count(datetime)}}}

```

which outputs:

```auto
{
  "2018-06-01": {
    "a": {
      "18": 1,
      "08": 2,
      "02": 1
    }
  },
  "2018-06-02": {
    "a": {
      "12": 1
    }
  },
  "2018-06-04": {
    "a": {
      "14": 1
    }
  }
}

```

I am looking for something like:

```auto
{
  "2018-06-01": {
    "a": 2, 
    "b": 1,
    "c": 1
    }
  },
  "2018-06-02": {
    "a": 1, 
    "b": 0,
    "c": 0
  }, 
  etc

```

Is this possible ? I tried to `$number($substring(datetime,11,2))` but it does not allow me to parse this

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [31 March 2019 12:03 UTC](https://discourse.nodered.org/t/help-with-jsonata-grouping/9629/2 "2019-03-31T12:03:43Z")

</div>

I'm no expert at jsonata but it seems to me that the expression will be complex and in 3 months you may have a hard time figuringout what it is. I would look at a different solution to this issue like doing it in a function node.

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [8 April 2019 16:38 UTC](https://discourse.nodered.org/t/help-with-jsonata-grouping/9629/3 "2019-04-08T16:38:28Z")

</div>

I agree. You likely need a map function and that is probably easier done in actual JavaScript with lots of comments.
