# Json lookup table:

**URL:** <https://discourse.nodered.org/t/json-lookup-table/44929>\
**Category:** General\
**Created:** [29 April 2021 13:04 UTC](https://discourse.nodered.org/t/json-lookup-table/44929 "2021-04-29T13:04:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![AxelD](https://avatars.discourse-cdn.com/v4/letter/a/b77776/32.png) [@AxelD](https://discourse.nodered.org/u/AxelD)\
**Post date:** [29 April 2021 13:04 UTC](https://discourse.nodered.org/t/json-lookup-table/44929/1 "2021-04-29T13:04:26Z")

</div>

Hey guys!

I need some help with this javascript function. You can see in the first picture that I have a payload with an: id, timestamp and value. But I want to change only the id to a better name then "104". In the second picture u see the payload that I want.

![afbeelding](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/0/80479c6c83b9849c6a67ba87a7c5798462acfa84.png) ===\> ![afbeelding](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/8/e8670cf4cd8a057a3510c882879ac498bbccb1f6.png)

To change from "104" to "Battery\_Current" I have to use a Json payload where i can vind the correspnding name to the number.

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

I have wirten this code but ofcourse it doesn't work because I have no idea how to write something like this.  
What does the code need to do: The payload comes in and stores the id ("104") then it looksup in the TMPmetaTable where id "104" is and then it gives me the corresponding namen. After it found the name the program replaces the current id "104" with the new "Battery\_Current".

```auto
var TMPmetaTable = 
{"Metadata":
{"0":{"name":"drive0_CurrentRPM","id":"101"},
"1":{"name":"drive1_CurrentRPM","id":"102"},
"2":{"name":"Battery_Voltage","id":"103"},
"3":{"name":"Battery_Current","id":"104"}}
};

var dataId = msg.payload.id;
var val = TMPmetaTable.Metadata.indexOf(dataID);

msg.payload.id = TMPmetaTable.Metadata[val].name;

return msg;

```

Thank you for helping and kind regards,  
AxelD

---

<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:** [29 April 2021 13:26 UTC](https://discourse.nodered.org/t/json-lookup-table/44929/2 "2021-04-29T13:26:51Z")

</div>

You can replace the `payload.id` in a change node using following jsonata expression.

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

```auto
/* returns the name from the Metadata table for the id corresponding to the payload.id */
(Metadata~>$spread())[*.id=$$.payload.id].*.name

```

The jsonata expression assumes that the mapping table is provided in `msg.Metadata`.

---

<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:** [29 April 2021 13:31 UTC](https://discourse.nodered.org/t/json-lookup-table/44929/3 "2021-04-29T13:31:37Z")

</div>

Hi AxelD,

i think it would be easier to make your lookup table into an array in order to use the `find` js command on it. [Array.prototype.find()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find)

```auto
var lookup = [
{"name":"drive0_CurrentRPM","id":"101"},
{"name":"drive1_CurrentRPM","id":"102"},
{"name":"Battery_Voltage","id":"103"},
{"name":"Battery_Current","id":"104"}
];

var lookupMatch = lookup.find(el => msg.payload.id === el.id);
node.warn(lookupMatch)

if(lookupMatch) {
    msg.payload.id = lookupMatch.name;
}

return msg;

```

Example flow:

```auto
[{"id":"467d10f5.83222","type":"inject","z":"1e053351.87668d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"id\":\"104\",\"qc\":0,\"qx\":20,\"ts\":\"2021-04-29T12:48:67.694Z\",\"val\":-10.199999809265137}","payloadType":"json","x":240,"y":400,"wires":[["e49ff8a2.733108","fe4555f9.48244"]]},{"id":"e49ff8a2.733108","type":"debug","z":"1e053351.87668d","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":370,"y":320,"wires":[]},{"id":"fe4555f9.48244","type":"function","z":"1e053351.87668d","name":"","func":"var lookup = [\n{\"name\":\"drive0_CurrentRPM\",\"id\":\"101\"},\n{\"name\":\"drive1_CurrentRPM\",\"id\":\"102\"},\n{\"name\":\"Battery_Voltage\",\"id\":\"103\"},\n{\"name\":\"Battery_Current\",\"id\":\"104\"}\n];\n\nvar lookupMatch = lookup.find(el => msg.payload.id === el.id);\nnode.warn(lookupMatch)\n\nif(lookupMatch) {\n msg.payload.id = lookupMatch.name;\n}\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":400,"wires":[["4ccf3900.41f748"]]},{"id":"4ccf3900.41f748","type":"debug","z":"1e053351.87668d","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":630,"y":400,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![AxelD](https://avatars.discourse-cdn.com/v4/letter/a/b77776/32.png) [@AxelD](https://discourse.nodered.org/u/AxelD)\
**Post date:** [29 April 2021 13:45 UTC](https://discourse.nodered.org/t/json-lookup-table/44929/4 "2021-04-29T13:45:07Z")

</div>

@UnborN  
This works very good!  
But I keep getting this warning message?

![afbeelding](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/e/5e1feb91668fbd588e33bfaefbba9e5456874a65.png)

![afbeelding](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/1/4110f25ab3e51eafa43089d586de3c96a35cb8e0.png)

---

<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:** [29 April 2021 13:48 UTC](https://discourse.nodered.org/t/json-lookup-table/44929/5 "2021-04-29T13:48:45Z")

</div>

> [@AxelD](#):
>
> But I keep getting this warning message?

Thats due to the `node.warn(lookupMatch)` line .. that is there simply for debugging.  
It shows in debug tab if there was a lookup match.

If you are sure about the code .. simple comment it out or delete it 😉

---

<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:** [13 May 2021 13:49 UTC](https://discourse.nodered.org/t/json-lookup-table/44929/6 "2021-05-13T13:49:25Z")

</div>

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