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

**URL:** <https://discourse.nodered.org/t/how-to-add-an-attribute-to-a-json-array-in-node-red/24769>\
**Category:** General\
**Created:** [13 April 2020 15:42 UTC](https://discourse.nodered.org/t/how-to-add-an-attribute-to-a-json-array-in-node-red/24769 "2020-04-13T15:42:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nedstrader](https://avatars.discourse-cdn.com/v4/letter/n/4491bb/32.png) [@nedstrader](https://discourse.nodered.org/u/nedstrader)\
**Post date:** [13 April 2020 15:42 UTC](https://discourse.nodered.org/t/how-to-add-an-attribute-to-a-json-array-in-node-red/24769/1 "2020-04-13T15:42:43Z")

</div>

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 ).

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.

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [13 April 2020 15:49 UTC](https://discourse.nodered.org/t/how-to-add-an-attribute-to-a-json-array-in-node-red/24769/2 "2020-04-13T15:49:42Z")

</div>

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?

```auto
{
    "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.

> **[JSONata Documentation · JSONata](https://docs.jsonata.org/overview.html)**
>
> \## Introduction

> [@nedstrader](#):
>
> If the close is higher than the (close high of day)  
> then the close is assigned to the (close high of day).

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`.

> [@nedstrader](#):
>
> I would also like to calculate **close\_stop\_loss** attribute which is 2% below the  
> (close high of day).

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.

---

<div class="post-metadata">

**Author:** ![nedstrader](https://avatars.discourse-cdn.com/v4/letter/n/4491bb/32.png) [@nedstrader](https://discourse.nodered.org/u/nedstrader)\
**Post date:** [13 April 2020 18:34 UTC](https://discourse.nodered.org/t/how-to-add-an-attribute-to-a-json-array-in-node-red/24769/3 "2020-04-13T18:34:23Z")

</div>

Hello

1. The input JSON data that was retrieved from [AlphaVandtage.com](http://AlphaVandtage.com) looks like the following attachment of a screen shot of the debug window.

 ![Node-RED - Google Chrome 4_13_2020 2_22_09 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/6/a66e6e3c8413de2efe37fa87154afdc278e274d2.png)

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.

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [13 April 2020 19:59 UTC](https://discourse.nodered.org/t/how-to-add-an-attribute-to-a-json-array-in-node-red/24769/4 "2020-04-13T19:59:24Z")

</div>

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.

> [@nedstrader](#):
>
> The original input json contains an array of date objects.

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.

> [@nedstrader](#):
>
> If the Close is greater than the **Close\_High\_of\_Day**  
> then assign the close to the Close\_High\_of\_Day.

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`"

> [@nedstrader](#):
>
> These two new attributes need to be calculated for each date object in the JSON array.

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](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](https://try.jsonata.org/ZgIbTkj0p)

---

<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:** [12 June 2020 19:59 UTC](https://discourse.nodered.org/t/how-to-add-an-attribute-to-a-json-array-in-node-red/24769/5 "2020-06-12T19:59:24Z")

</div>

Closed on 12 June 2020 19:59 UTC

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