Newbie question on changing abbreviations to string from array

Hi all! I’ve just starting putting together my first node red project but and stuck.
I managed to make an api call and extracted the data I want but it’s in an abbreviated form.

I want to convert it to a string base on this array? But have no idea how to do it (no programming background)

I’ve looked through all the examples and tutorials and I’m guessing I’ve to use a function node but am getting nowhere.

How do I get an incoming msg payload eg. FG to convert it to the corresponding FOG base on the below?

{
'BR': 'Mist',
'CL': 'Cloudy',
'DR': 'Drizzle',
'FA': 'Fair',
'FG': 'Fog',
'FN': 'Fair (Night)',
'FW': 'Fair & Warm',
'HG': 'Heavy Thundery Showers with Gusty Winds',
'HR': 'Heavy Rain',
'HS': 'Heavy Showers',
'HT': 'Heavy Thundery Showers',
'HZ': 'Hazy',
'LH': 'Slightly Hazy',
'LR': 'Light Rain',
'LS': 'Light Showers',
'OC': 'Overcast',
'PC': 'Partly Cloudy',
'PN': 'Partly Cloudy (Night)',
'PS': 'Passing Showers',
'RA': 'Moderate Rain',
'SH': 'Showers',
'SK': 'Strong Winds, Showers',
'SN': 'Snow',
'SR': 'Strong Winds, Rain',
'SS': 'Snow Showers',
'SU': 'Sunny',
'SW': 'Strong Winds',
'TL': 'Thundery Showers',
'WC': 'Windy, Cloudy',
'WD': 'Windy',
'WF': 'Windy, Fair',
'WR': 'Windy, Rain',
'WS': 'Windy, Showers',
}

What you have is JSON and if you use a JSON node you will end up with a JS object. From that you can use any or all parts of it to generate whatever string you like.

As you are new to node-red, I strongly recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

Edit...

I may have spoken too soon. That is not valid JSON

Where did that come from? Surely not an API? Show us a screenshot of the debug message and your flow.

You would use the object you showed , and use it to look up the string.

[{"id":"b69e6a45.2550a8","type":"inject","z":"5a245aa1.510164","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"HT","payloadType":"str","x":140,"y":2820,"wires":[["80a744b0.ec3e78"]]},{"id":"80a744b0.ec3e78","type":"function","z":"5a245aa1.510164","name":"","func":"// create lookup table this could also be store in context\nvar lookup = { \n'BR': 'Mist',\n'CL': 'Cloudy',\n'DR': 'Drizzle',\n'FA': 'Fair',\n'FG': 'Fog',\n'FN': 'Fair (Night)',\n'FW': 'Fair & Warm',\n'HG': 'Heavy Thundery Showers with Gusty Winds',\n'HR': 'Heavy Rain',\n'HS': 'Heavy Showers',\n'HT': 'Heavy Thundery Showers',\n'HZ': 'Hazy',\n'LH': 'Slightly Hazy',\n'LR': 'Light Rain',\n'LS': 'Light Showers',\n'OC': 'Overcast',\n'PC': 'Partly Cloudy',\n'PN': 'Partly Cloudy (Night)',\n'PS': 'Passing Showers',\n'RA': 'Moderate Rain',\n'SH': 'Showers',\n'SK': 'Strong Winds, Showers',\n'SN': 'Snow',\n'SR': 'Strong Winds, Rain',\n'SS': 'Snow Showers',\n'SU': 'Sunny',\n'SW': 'Strong Winds',\n'TL': 'Thundery Showers',\n'WC': 'Windy, Cloudy',\n'WD': 'Windy',\n'WF': 'Windy, Fair',\n'WR': 'Windy, Rain'\n}\nmsg.payload=lookup[msg.payload];\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":290,"y":2820,"wires":[["f1b82c93.f6263"]]},{"id":"f1b82c93.f6263","type":"debug","z":"5a245aa1.510164","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":510,"y":2820,"wires":[]}]

The lookup object could also be stored in context storage rather than in the function. This could also be done in a change node with JSONata expression, and as of node-red 1.3 you could also do it with out JOSNata.

Steve is correct -- but a simple string substitution to change single quotes to double quotes would make it valid, it seems. Then the modified string could be passed to the json node to turn it into a proper lookup table.

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