# Jsonata + moment troubles

**URL:** https://discourse.nodered.org/t/jsonata-moment-troubles/44405
**Category:** General
**Created:** [19 April 2021 13:11 UTC](https://discourse.nodered.org/t/jsonata-moment-troubles/44405 "2021-04-19T13:11:01Z")
**Posts on this page:** 9
**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: [19 April 2021 13:11 UTC](https://discourse.nodered.org/t/jsonata-moment-troubles/44405/1 "2021-04-19T13:11:01Z")

</div>

I am a bit lost with jsonata and moment.

Input data example:

```auto
[
    [1618243200000, 5.45, 5.45, 5.2, 5.2],
    [1618257600000, 4.98, 4.99, 4.96, 4.96],
    [1618272000000, 4.88, 4.88, 4.67, 4.69],
    [1618286400000, 4.74, 4.81, 4.71, 4.81]
...
]

```

Without the use of moment, expression:

```auto
payload.{
    "date": $[0],
    "open": $[1],
    "high": $[2],
    "low": $[3],
    "close": $[4]
}

```

Output:

```auto
{
    "date": 1618243200000,
    "open": 5.45,
    "high": 5.45,
    "low": 5.2,
    "close": 5.2
}

```

All good.

Now I am trying to format the date with moment, expression:

```auto
payload.{
    "date": $moment($[0]).format("D-MMM-YY"),
    "open": $[1],
    "high": $[2],
    "low": $[3],
    "close": $[4]
}

```

Output:

```auto
{
    "date": ["12-Apr-21", "1-Jan-70", "1-Jan-70", "1-Jan-70", "1-Jan-70"],
    "open": 5.45,
    "high": 5.45,
    "low": 5.2,
    "close": 5.2
}

```

What is happening ? Is moment evaluating/referencing differently ?

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [19 April 2021 13:49 UTC](https://discourse.nodered.org/t/jsonata-moment-troubles/44405/2 "2021-04-19T13:49:35Z")

</div>

> [@bakman2](#):
>
> ```auto
> {
> "date": 1618243200000,
> "open": 5.45,
> "high": 5.45,
> "low": 5.2,
> "close": 5.2
> }
> 
> ```
> 
> All good.

Not, as the 4 arrays has become 1 object  
try

```auto
$map(payload , function($v){
    {"date": $moment($v[0]).format("D-MMM-YY"),
    "open": $v[1],
    "high": $v[2],
    "low": $v[3],
    "close": $v[4]}
})

```

and should return 4 objects

```auto
[{"date":"12-Apr-21","open":5.45,"high":5.45,"low":5.2,"close":5.2},
{"date":"12-Apr-21","open":4.98,"high":4.99,"low":4.96,"close":4.96},
{"date":"13-Apr-21","open":4.88,"high":4.88,"low":4.67,"close":4.69},
{"date":"13-Apr-21","open":4.74,"high":4.81,"low":4.71,"close":4.81}]

```

---

<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: [19 April 2021 13:55 UTC](https://discourse.nodered.org/t/jsonata-moment-troubles/44405/3 "2021-04-19T13:55:47Z")

</div>

> Not, as the 4 arrays has become 1 object

Well yes, I just left out the rest of the data, my bad:

 ![Screenshot 2021-04-19 at 16.03.17](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/2/e283d53a14548cea37259b19fe9b341e30692696.jpeg)

I don't understand why the moment function is evaluating all elements in the array

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [19 April 2021 14:18 UTC](https://discourse.nodered.org/t/jsonata-moment-troubles/44405/4 "2021-04-19T14:18:41Z")

</div>

Ah, Now i understand, I miss read your outputs. Seems to be a bug or limitation in JSONata

---

<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: [19 April 2021 17:57 UTC](https://discourse.nodered.org/t/jsonata-moment-troubles/44405/5 "2021-04-19T17:57:15Z")

</div>

Also when trying to only get the first element fails for me

```auto
"date": $moment($[0]).format("D-MMM-YY")[0],

```

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [19 April 2021 18:12 UTC](https://discourse.nodered.org/t/jsonata-moment-troubles/44405/6 "2021-04-19T18:12:53Z")

</div>

> [@bakman2](#):
>
> `$moment($[0]).format("D-MMM-YY")`

does that not return a string?

---

<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: [19 April 2021 18:18 UTC](https://discourse.nodered.org/t/jsonata-moment-troubles/44405/7 "2021-04-19T18:18:31Z")

</div>

With your map function yes. If I try it "my" way, i want to get the first element from the array, but doesnt seem to work. Your method functions properly, thanks.

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [19 April 2021 18:57 UTC](https://discourse.nodered.org/t/jsonata-moment-troubles/44405/8 "2021-04-19T18:57:33Z")

</div>

~~I don't think it will work your way as moments is doing weird things there.~~ If you use `$fromMillis($[0],"[D]-[M]-[Y]")` there expression works as it should. But there is no format for month in text format that I can see.

[edit] @bakman2 here's the answer

```auto
payload.{
    "date": [$moment($[0]).format("D-MMM-YY")][0],
    "open": $[1],
    "high": $[2],
    "low": $[3],
    "close": $[4]
}

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [18 June 2021 18:58 UTC](https://discourse.nodered.org/t/jsonata-moment-troubles/44405/9 "2021-06-18T18:58:00Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
