# How to extract a property with a change node

**URL:** <https://discourse.nodered.org/t/how-to-extract-a-property-with-a-change-node/70345>\
**Category:** General\
**Created:** [9 November 2022 18:39 UTC](https://discourse.nodered.org/t/how-to-extract-a-property-with-a-change-node/70345 "2022-11-09T18:39:43Z")\
**Posts on this page:** 6\
**Page:** 1

<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:** [9 November 2022 18:39 UTC](https://discourse.nodered.org/t/how-to-extract-a-property-with-a-change-node/70345/1 "2022-11-09T18:39:43Z")

</div>

How do I extract the property `payload.common.states["2"]` from an object, referring to the val .

```auto
{"common":{"states":{"0":"idle","2":"water leak detected"}},"val":2}

```

A function node with the following code is working:

```auto
msg.payload = msg.payload.common.states[msg.payload.val];
return msg;

```

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

A Change Node with JSONATA does not work.

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

is payload.val a string then I get always the object:

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

is payload.val a number I get undefined.

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

Is there a possibility to use a change node to set the payload to the common.states property of payload.val?

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [9 November 2022 19:27 UTC](https://discourse.nodered.org/t/how-to-extract-a-property-with-a-change-node/70345/2 "2022-11-09T19:27:29Z")

</div>

Try `$lookup(payload.common.states, $string($lookup(payload, "val")))`

---

<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:** [9 November 2022 19:32 UTC](https://discourse.nodered.org/t/how-to-extract-a-property-with-a-change-node/70345/3 "2022-11-09T19:32:44Z")

</div>

> [@Buckskin](#):
>
> `$lookup(payload.common.states, $string($lookup(payload, "val")))`

Ok - $lookup is working. Thank you - the second part can be shortened.

```auto
 $lookup(payload.common.states, $string(payload.val))

```

I was a little bit surprised that there is not a simpler solution.

---

<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:** [9 November 2022 20:09 UTC](https://discourse.nodered.org/t/how-to-extract-a-property-with-a-change-node/70345/4 "2022-11-09T20:09:56Z")

</div>

You do not need to call JSONata to do this, just  
set `msg.` `payload`  
to `msg.` `payload.common.states[msg.payload.val]`  
e.g.

```auto
[{"id":"49546b30e8e5fb6e","type":"change","z":"452103ea51141731","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.common.states[msg.payload.val]","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":3440,"wires":[["b79c522c6df54913"]]},{"id":"01baa9592fe584f0","type":"inject","z":"452103ea51141731","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"common\":{\"states\":{\"0\":\"idle\",\"2\":\"leak\"}},\"val\":2}","payloadType":"json","x":150,"y":3440,"wires":[["49546b30e8e5fb6e"]]},{"id":"b79c522c6df54913","type":"debug","z":"452103ea51141731","name":"debug 110","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":530,"y":3380,"wires":[]}]

```

Works if `val` is a string or number.

---

<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:** [9 November 2022 20:23 UTC](https://discourse.nodered.org/t/how-to-extract-a-property-with-a-change-node/70345/5 "2022-11-09T20:23:29Z")

</div>

Many - many thanks. Great solution!!!

---

<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:** [23 November 2022 20:24 UTC](https://discourse.nodered.org/t/how-to-extract-a-property-with-a-change-node/70345/6 "2022-11-23T20:24:29Z")

</div>

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