Search code examples
node.jsjsonnode-red

Multiple word object name in JSON, node-red


I have a problem with parsing data from JSON object which's name has more than 1 word, what do I have to do to succesfully call it in the function node right before the other node. What to use instead of the stars to make it work ?

every answer appreciated, thanks

var array[];
for(var i = 0, i < msg.payload.*Time Series (Daily)*.length, i++){
array = msg.payload.*Time Series (Daily)*.*2020-12-10*.*2. high*
}
return array
{
    "Meta Data": {
        "1. Information": "Daily Time Series with Splits and Dividend Events",
        "2. Symbol": "AAPL",
        "3. Last Refreshed": "2020-12-10",
        "4. Output Size": "Compact",
        "5. Time Zone": "US/Eastern"
    },
    "Time Series (Daily)": {
        "2020-12-10": {
            "1. open": "120.5",
            "2. high": "123.87",
            "3. low": "120.15",
            "4. close": "123.24",
            "5. adjusted close": "123.24",
            "6. volume": "81312170",
            "7. dividend amount": "0.0000",
            "8. split coefficient": "1.0"
        }

Solution

  • You can use a string literal inside square brackets to access properties with spaces, reserved words, etc. This can also be used for assignment.

    const myObject = {
      'a key with spaces': true,
      'return': 'this is a property whose key is the word "return"'
    };
    
    myObject['a new prop'] = 'Hello';
    
    console.log(myObject['a key with spaces'], myObject['return']);
    

    For your specific use case:

    array = msg.payload['Time Series (Daily)']['2020-12-10']['2. high']