# JSONata ~\> (transform) question

**URL:** https://discourse.nodered.org/t/jsonata-transform-question/36403
**Category:** General
**Created:** [23 November 2020 14:57 UTC](https://discourse.nodered.org/t/jsonata-transform-question/36403 "2020-11-23T14:57:35Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![bogorad](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bogorad/32/18900_2.png) [@bogorad](https://discourse.nodered.org/u/bogorad)
#### Post date: [23 November 2020 14:57 UTC](https://discourse.nodered.org/t/jsonata-transform-question/36403/1 "2020-11-23T14:57:35Z")

</div>

I'm trying to keep 'last\_seen' values (milliseconds) about several devices in a global hash called 'last\_seen' using MAC addresses as keys. e.g.:

```auto
{
    "11-22-33-44-55-66": {"last_seen": 0,"reported": false},
    "AABBCCDDEEFF": {"last_seen": 0,"reported": false}
}

```

Query works fine,

```auto
$lookup($globalContext('last_seen'),payload)

```

but how do I update values?

Tried a 'change' node with this

```auto
$globalContext('last_seen')~>|payload|{"last_seen": $millis()}|

```

but it doesn't work. What am I missing?

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [23 November 2020 15:26 UTC](https://discourse.nodered.org/t/jsonata-transform-question/36403/2 "2020-11-23T15:26:07Z")

</div>

Must it be JSONata?

It is easily achieved in a function...

```auto
var mac_to_update = msg.payload; //or where ever it comes from 
if(!mac_to_update) return msg; //nothing to update!
var last_seen = global.get('last_seen') || {}; //get or create the global object
if(!last_seen[mac_to_update]) last_seen[mac_to_update] = {}; //handle empty/new mac
last_seen[mac_to_update].last_seen = Date.now(); //set last_seen 
global.set('last_seen',last_seen); //store back in global
return msg;//return to next node

```

---

<div class="post-metadata">

### Author: ![bogorad](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bogorad/32/18900_2.png) [@bogorad](https://discourse.nodered.org/u/bogorad)
#### Post date: [23 November 2020 15:33 UTC](https://discourse.nodered.org/t/jsonata-transform-question/36403/3 "2020-11-23T15:33:22Z")

</div>

Thanks for your code!

I'd very much like it to be JSONata, yes - it's kinda made for this sort of thing. I'm just having a hard time wrapping my head around ~\>

---

<div class="post-metadata">

### Author: ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)
#### Post date: [23 November 2020 16:12 UTC](https://discourse.nodered.org/t/jsonata-transform-question/36403/4 "2020-11-23T16:12:24Z")

</div>

Hi,

Here an example of how it can be done:

- [https://try.jsonata.org/BdRcPZrQp](https://try.jsonata.org/BdRcPZrQp)

```auto
last_seen ~> | $lookup($,$$.new_value.MAC) | { "last_seen" : $millis(),
                                               "reported" : $$.new_value.reported} |

```

So the input of this JSONata expression consists of:

- new\_value : specifying the MAC address that needs to be updated + new reported value.
- last\_seen : is the json string that must be updated.

The input I have used as example:

```auto
{
  "new_value": {
    "MAC": "AABBCCDDEEFF",
    "reported": true
  },
  "last_seen": {
    "11-22-33-44-55-66": {
      "last_seen": 0,
      "reported": false
    },
    "AABBCCDDEEFF": {
      "last_seen": 0,
      "reported": false
    }
  }
}

```

If this doesn't exactly do what you want then please share an example of the input and the expected output for that input.

---

<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: [23 November 2020 16:16 UTC](https://discourse.nodered.org/t/jsonata-transform-question/36403/5 "2020-11-23T16:16:02Z")

</div>

Or it can be done with the $merge function  
e.g.

```auto
[{"id":"2cd1fcf0.4fe704","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload"},{"p":"last_seen","v":"","vt":"date"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"11-22-33-44-55-66","payloadType":"str","x":190,"y":2520,"wires":[["212634ed.7da8fc"]]},{"id":"212634ed.7da8fc","type":"change","z":"8d22ae29.7df6d","name":"","rules":[{"t":"set","p":"last_seen","pt":"global","to":"$merge([$globalContext(\"last_seen\"),{payload:{\"last_seen\":$millis(),\"reported\":true}}])","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":2500,"wires":[["d6dae2d8.e5527"]]},{"id":"d6dae2d8.e5527","type":"debug","z":"8d22ae29.7df6d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":650,"y":2500,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [23 November 2020 17:05 UTC](https://discourse.nodered.org/t/jsonata-transform-question/36403/6 "2020-11-23T17:05:44Z")

</div>

> [@bogorad](#):
>
> I'm just having a hard time wrapping my head around ~\>

If you are having a hard time then whoever inherits the code (or yourself in a couple of years) will probably also have a hard time. That might be considered an argument for doing it in a more easy to understand way (such as a function node).

---

<div class="post-metadata">

### Author: ![bogorad](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bogorad/32/18900_2.png) [@bogorad](https://discourse.nodered.org/u/bogorad)
#### Post date: [26 November 2020 16:01 UTC](https://discourse.nodered.org/t/jsonata-transform-question/36403/7 "2020-11-26T16:01:10Z")

</div>

True, but I'm building an MVP, so whoever has to deal with it can just ask me 😉

---

<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: [10 December 2020 16:01 UTC](https://discourse.nodered.org/t/jsonata-transform-question/36403/8 "2020-12-10T16:01:13Z")

</div>

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