# JSonata to index and access message data

**URL:** <https://discourse.nodered.org/t/jsonata-to-index-and-access-message-data/73608>\
**Category:** General\
**Created:** [12 January 2023 19:33 UTC](https://discourse.nodered.org/t/jsonata-to-index-and-access-message-data/73608 "2023-01-12T19:33:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gwgorman](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gwgorman/32/9662_2.png) [@gwgorman](https://discourse.nodered.org/u/gwgorman)\
**Post date:** [12 January 2023 19:33 UTC](https://discourse.nodered.org/t/jsonata-to-index-and-access-message-data/73608/1 "2023-01-12T19:33:47Z")

</div>

hi all - more JSonata fun... it should be "easy" but I can't figure it out! In a change node I want to pull a value out of a payload based on the value of the Topic.  
Assume this on Message

```auto
{
    "topic": "ItemInventory",
    "payload": {
        "SKU": 3,
        "Item": "Shorts",
        "ItemPrice": 50,
        "ItemInventory": 10,
        "id": 2
    },
    "row": 2,
    "socketid": "yq3RsjOCodNblk48AAAF",
    "_msgid": "be57da402d5185d6"
}

```

I tried

```auto
$.payload.$$.topic

```

All I get is the value for Topic. I've poked around [jsonata.org](http://jsonata.org) and tried all kinds of wacky ways but so far no joy.

Any help is appreciated!  
/greg

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [12 January 2023 21:48 UTC](https://discourse.nodered.org/t/jsonata-to-index-and-access-message-data/73608/2 "2023-01-12T21:48:31Z")

</div>

Hey @gwgorman

I think you're in need of the `$lookup()` function.

Something like...

```auto
$lookup($.payload, $.topic)

```

---

<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:** [12 January 2023 21:53 UTC](https://discourse.nodered.org/t/jsonata-to-index-and-access-message-data/73608/3 "2023-01-12T21:53:04Z")

</div>

Or  
Set `msg.` `payload`  
To `msg.` `payload[msg.topic]`  
no need for JSONata.

examples of both

```auto
[{"id":"6ef769b95682b5db","type":"inject","z":"da8a6ef0b3c9a5c8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"row","v":"2","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"ItemInventory","payload":"{\"SKU\":3,\"Item\":\"Shorts\",\"ItemPrice\":50,\"ItemInventory\":10,\"id\":2}","payloadType":"json","x":130,"y":3760,"wires":[["71093cd3e5ad67a7","a2765c009d755cd6"]]},{"id":"71093cd3e5ad67a7","type":"change","z":"da8a6ef0b3c9a5c8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload[msg.topic]","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":3760,"wires":[["1f1dd0250f5ca139"]]},{"id":"a2765c009d755cd6","type":"change","z":"da8a6ef0b3c9a5c8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$lookup($$.payload, $$.topic)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":3800,"wires":[["1f1dd0250f5ca139"]]},{"id":"1f1dd0250f5ca139","type":"debug","z":"da8a6ef0b3c9a5c8","name":"debug 226","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":510,"y":3760,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![gwgorman](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gwgorman/32/9662_2.png) [@gwgorman](https://discourse.nodered.org/u/gwgorman)\
**Post date:** [12 January 2023 21:58 UTC](https://discourse.nodered.org/t/jsonata-to-index-and-access-message-data/73608/4 "2023-01-12T21:58:04Z")

</div>

Brilliant! I _knew_ I was missing something!  
Thanks!

and Nick - yes yours worked too. ty

/gg

---

<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:** [26 January 2023 21:58 UTC](https://discourse.nodered.org/t/jsonata-to-index-and-access-message-data/73608/5 "2023-01-26T21:58:27Z")

</div>

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