How to add an attribute to a JSON array in Node-red

Hello,

  1. I have a flow that issues a GET http request and returns a JSON array of stock market data:

Time series data of 5 minute interval candles:
Datetime { 1. open, 2. high, 3. low, 4. close, 5. volume ).

  1. I can see the JSON data in the debug window everything looks good!

  2. I would like to use the close attribute and create a new attribute called
    close_hod (close high of day ).

    If the close is higher than the (close high of day)
    then the close is assigned to the (close high of day).

  3. I would also like to calculate close_stop_loss attribute which is 2% below the
    (close high of day).

  4. I would like to create a new JSON array that contains the original JSON array plus the two additional attributes in the time series data at 5 min intervals:

datetime {

  1. open: 10.00
  2. high: 30.00
  3. low: 10.00
  4. close: 25.00
  5. volume: 1,000,000
  6. close_hod: 25.00
  7. close_stop_loss: 24.50
    }

Any ideas how to do this.

Hi,

I'm coming back to your topic, but it appears that due to your use of the numbered list your JSON array in the post has turned into a formatted list too. Can you confirm your object looks like this?

{
    "open": 10.00,
    "high": 30.00,
    "low": 10.00,
    "close": 25.00,
    "volume": 1000000,
    "close_hod": 25.00,
    "close_stop_loss": 24.50
}

To add additional properties with values, you can use a change node. You can use either a function node, or JSONata in a change node to calculate those values. The JSONata approach would be my personal choice here, but that's up to you.

This part is unclear to me. If your input data only has open , high, low, close, and volume, where does the initial "close high of day" come from? In pseudo code it reads as
if close > hod => close = hod. But I don't see this close_hod in the initial data (after all you specify that it has yet to be created/added), nor something to use as hod.

This can be calculated by taking the close_hod derived from the previous step and multiply it by 0.98. You can do this, again, in a function node or with JSONata. In fact, depending on your answer on how close_hod is derived, it can likely be done in a single JSONata query.

Hello

  1. The input JSON data that was retrieved from AlphaVandtage.com looks like the following attachment of a screen shot of the debug window.

  1. The original input json contains an array of date objects.
    each object contains { Open, High, Low, Close, Volume } as shown in the screen shot.

  2. I would like to calculate the Close_High_of_Day based on the Close attribute for each date array object.

New Attribute: Close_High_Of_Day

If the Close is greater than the Close_High_of_Day
then assign the close to the Close_High_of_Day.

  1. I would also like to calculate the Close_Stop_Loss_Amt based on the Close_HIgh_of_Day attribute.

New Attribute: Close Stop Loss Amount
The Close-Stop-Loss-Amount is defined as 98% of the Close-High-Of-Day.

  1. These two new attributes need to be calculated for each date object in the JSON array.

Hi Vic,

Looking at your incoming data it's more horrible than I had hoped it was. It's very readable, so that's a plus, but it's not the easiest to work with. What you see here are keys with spaces in them. In most topics you see you can refer to parts of an object through dot notation, i.e. msg.payload.data, however, here you need to use the fully written notation: msg.payload["Meta Data"]["1. Information"] for example. Note that this is case sensitive, and you need to surround the entire name with quotes.

Slight correction here, the data you show in the debug window is an object with property "Time Series (5min)", which contains another object with each property a timestamp. This timestamp holds another nested object, which is what you described. The difference between an array of date objects, and an object with date objects is a major one in how you've to solve this.

Bolding added by myself, this first Close_High_of_Day, where/how is this defined? You specify this is an attribute to add by yourself, but where does it initially come from? You describe the action as "if close > Close_High_of_Day, then Close_High_of_Day = close"

Is this an aggregation, or do they have to be added to each object?

Either way, this really sounds like a JSONata solution, or at least to me.
I took the following sample API endpoint from the AlphaVantage site: https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=IBM&interval=5min&apikey=demo
Here's a sandbox to get you started, with the sample data from that link: https://try.jsonata.org/ZgIbTkj0p

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