JSonata to index and access message data

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

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

I tried

$.payload.$$.topic

All I get is the value for Topic. I've poked around jsonata.org and tried all kinds of wacky ways but so far no joy.

Any help is appreciated!
/greg

Hey @gwgorman

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

Something like...

$lookup($.payload, $.topic)

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

examples of both

[{"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":[]}]

Brilliant! I knew I was missing something!
Thanks!

and Nick - yes yours worked too. ty

/gg

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