# Need a jsonata magician!

**URL:** https://discourse.nodered.org/t/need-a-jsonata-magician/62714
**Category:** General
**Created:** [16 May 2022 23:17 UTC](https://discourse.nodered.org/t/need-a-jsonata-magician/62714 "2022-05-16T23:17:23Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)
#### Post date: [16 May 2022 23:17 UTC](https://discourse.nodered.org/t/need-a-jsonata-magician/62714/1 "2022-05-16T23:17:23Z")

</div>

Guys,

I am extracting weather data from our BOM in Sydney and then want to use the Min and Max Temperatures (which are presented as an object) to make decisions in relation to home heating etc

Currently - this is what the returned data looks like - which i am storing in Context Data using a change node

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/0/20479d034a497eea9587909a12d23885f7bee637.png)

Here is an excerpt of the flow that stores them all - the main point is that it is a change node that is grabbing a message object that is passed in and then ripping it apart to store the information for later use

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/9/79a84fe7f1bf4214071984e3aa50e4348ae18aba.png)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/b/bb75cefddafc807773c8841a77885de5818d418b.png)

What i would like to do is to store the Min and Max temperatures as pure numbers - rather than with the C and degrees sign

I put together a change node that took 3 steps (3 seperate changes within the one node using Jsonata - but am unable to collaps that down to a single step) and have it operate across all the values

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/d/9d9e5c7b13ff65e9b29f7da3d59bb3cd6fc9ba68.png)

Anybody put me on the right track for how to incorporate this into the main change object ?

Craig

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [16 May 2022 23:59 UTC](https://discourse.nodered.org/t/need-a-jsonata-magician/62714/2 "2022-05-16T23:59:29Z")

</div>

Hello .. you could use in the Change node, that you set the globals, a single line of Jsonata

For Example :

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/b/6bd5a42e246152bd57eb81870ef629e109a3132e.png)

maybe i misunderstood the part of the question

> [@craigcurtin](#):
>
> and have it operate across all the values

Since you are selecting specific elements in the array and renaming them to Today, Tomorrow, 2DaysTime etc  
i dont know if there is an easier way than what you have done already .. except optimizing the jsonata

---

<div class="post-metadata">

### Author: ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)
#### Post date: [17 May 2022 00:04 UTC](https://discourse.nodered.org/t/need-a-jsonata-magician/62714/3 "2022-05-17T00:04:28Z")

</div>

If these are only integer values - use $parseInteger to extract the numeric part

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/4/34a6bc03dbb10a4fbe4db047195bbcff51450bbb.png)

and for sure you can directly assign it to your global value and use the path of the payload object as specified by @UnborN

so you can

`$parseInteger(payload[2].air_temperature_minimum,'0')` in your JSONATA field of the change node.

If the temperature values are not only integers - then you can also use the $replace function in combination with a regular expression. But as I could see - your temperature values are only integers.

---

<div class="post-metadata">

### Author: ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)
#### Post date: [17 May 2022 00:42 UTC](https://discourse.nodered.org/t/need-a-jsonata-magician/62714/4 "2022-05-17T00:42:37Z")

</div>

Thanks Unborn - i will give that another try - i was building that up in a seperate test node and got it down to 2 steps but could not get it to work as a single step as you have documented.

Will try again (now i know it should work) and report back

Craig

---

<div class="post-metadata">

### Author: ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)
#### Post date: [17 May 2022 00:43 UTC](https://discourse.nodered.org/t/need-a-jsonata-magician/62714/5 "2022-05-17T00:43:06Z")

</div>

Thanks Micky - they are only integers - so will try the first one and see how we go and report back

Craig

---

<div class="post-metadata">

### Author: ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)
#### Post date: [17 May 2022 00:55 UTC](https://discourse.nodered.org/t/need-a-jsonata-magician/62714/6 "2022-05-17T00:55:02Z")

</div>

Take care that you use

`$parseInteger(payload[2].air_temperature_minimum,'0')`

only payload and not msg.payload in your JSONATA expressions.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/2/f2bd737fb5ef9f212d6a3a1a5ed36a74e94ec640.png)

---

<div class="post-metadata">

### Author: ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)
#### Post date: [17 May 2022 00:58 UTC](https://discourse.nodered.org/t/need-a-jsonata-magician/62714/7 "2022-05-17T00:58:45Z")

</div>

Can i ask why that is ? I currently have tried it with msg.payload and it appears to be working and doing what i want

Just trying to understand (and thanks for taking them time to post this up)

Craig

---

<div class="post-metadata">

### Author: ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)
#### Post date: [17 May 2022 01:06 UTC](https://discourse.nodered.org/t/need-a-jsonata-magician/62714/8 "2022-05-17T01:06:11Z")

</div>

I read somewhere that msg.property will no longer supported in the future.  
If you have no complicated calculations you can directly address the properties of the msg object.  
In case you are down somewhere in your JSONATA calculations then you should refer to the parent object always with $$.

So $$.payload would also refer to the payload property of the msg object.

---

<div class="post-metadata">

### Author: ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)
#### Post date: [17 May 2022 01:06 UTC](https://discourse.nodered.org/t/need-a-jsonata-magician/62714/9 "2022-05-17T01:06:27Z")

</div>

Instead of trying to manipulate each field inside a separate `change` node rule, I would tend to process the entire array of 7 days into a single `global.Weather` object --

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/7/f7426095bc6f9f347d45f17161399c9b62ca377f.png)

because it looks like each day can use the same expression to do the restructuring:

```auto
(
    $daysTime := function($idx) {(
        $idx = 0 ? "Today" :
            $idx = 1 ? "Tomorrow" :
            $idx & "DaysTime"
    )};

    payload#$i {
        $daysTime($i): {
            "Description": precis,
            "Temperature": {
                "Minimum": $parseInteger(air_temperature_min, '0'),
                "Maximum": $parseInteger(air_temperature_max, '0')
            }
        }
    }
)

```

Only the field name "XDaysTime" requires a function, based on the index in the original weather data array (0 is Today, 1 is Tomorrow, etc).

Love jsonata or hate it, to me that looks pretty readable -- and the output structure is shown right in the nesting of the expression itself. BTW, next time please include the actual data as some JSON we can copy, rather than as a screen capture...

---

<div class="post-metadata">

### Author: ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)
#### Post date: [17 May 2022 01:19 UTC](https://discourse.nodered.org/t/need-a-jsonata-magician/62714/10 "2022-05-17T01:19:56Z")

</div>

Wow - OK that does what you needs doing - but i will need to study it some more to make sure i understand it before i deploy something like that.

Is there a way to comment the code in JSONATA so when i come back to it in two years (as i am sure the Australian BOM will change the data format !) then i will understand what each step is doing ?

Will do re posting code in future - i just thought in this case as you did not have access to the data it would have been of little value.

Craig

---

<div class="post-metadata">

### Author: ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)
#### Post date: [17 May 2022 01:20 UTC](https://discourse.nodered.org/t/need-a-jsonata-magician/62714/11 "2022-05-17T01:20:28Z")

</div>

Aah thanks for that - makes perfect sense so things do not break in the future.

Craig

---

<div class="post-metadata">

### Author: ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)
#### Post date: [17 May 2022 01:29 UTC](https://discourse.nodered.org/t/need-a-jsonata-magician/62714/12 "2022-05-17T01:29:13Z")

</div>

> [@craigcurtin](#):
>
> Is there a way to comment the code in JSONATA

Why yes there is:

```auto
(
    /* field name as a function of array index */
    $daysTime := function($idx) {(
        $idx = 0 ? "Today" :
        $idx = 1 ? "Tomorrow" :
        $idx & "DaysTime"
    )};

    payload#$i { /* iterate over payload array, save index in $i */
        $daysTime($i): { /* use the array index to derive the field name */
            "Description": precis,
            "Temperature": { /* convert temps to integers, ignore units */
                "Minimum": $parseInteger(air_temperature_min, '0'),
                "Maximum": $parseInteger(air_temperature_max, '0')
            }
        }
    }
)

```

_Guess I should have done that in the first place..._

---

<div class="post-metadata">

### Author: ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)
#### Post date: [17 May 2022 01:30 UTC](https://discourse.nodered.org/t/need-a-jsonata-magician/62714/13 "2022-05-17T01:30:31Z")

</div>

You can comment in the node itself or within the JSONATA code,  
see: [Programming constructs · JSONata](https://docs.jsonata.org/programming)

> JSONata expressions can be interleaved with comments using 'C' style comment delimeters. For example,

so you can use

```auto
/* a comment */

```

---

<div class="post-metadata">

### Author: ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)
#### Post date: [17 May 2022 01:31 UTC](https://discourse.nodered.org/t/need-a-jsonata-magician/62714/14 "2022-05-17T01:31:56Z")

</div>

> [@craigcurtin](#):
>
> i just thought in this case as you did not have access to the data it would have been of little value

Right, the flows calling the BOM service would not be useful -- but if you copy the json output structure directly from the debug sidebar and include it in a code block, it's so much easier!

---

<div class="post-metadata">

### Author: ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)
#### Post date: [17 May 2022 03:47 UTC](https://discourse.nodered.org/t/need-a-jsonata-magician/62714/15 "2022-05-17T03:47:06Z")

</div>

> [@shrickus](#):
>
> Right, the flows calling the BOM service would not be useful -- but if you copy the json output structure directly from the debug sidebar and include it in a code block, it's so much easier!

Thanks for Persisting with me on this one - i will use the Jsonata you have supplied and comment it up (will make sure your name is all over it - so i know who to come back to in the future !!)

Craig

---

<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: [31 May 2022 03:47 UTC](https://discourse.nodered.org/t/need-a-jsonata-magician/62714/16 "2022-05-31T03:47:52Z")

</div>

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