# Values from array to curl post

**URL:** <https://discourse.nodered.org/t/values-from-array-to-curl-post/68077>\
**Category:** General\
**Created:** [22 September 2022 12:31 UTC](https://discourse.nodered.org/t/values-from-array-to-curl-post/68077 "2022-09-22T12:31:20Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![smitterer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smitterer/32/59179_2.png) [@smitterer](https://discourse.nodered.org/u/smitterer)\
**Post date:** [22 September 2022 12:31 UTC](https://discourse.nodered.org/t/values-from-array-to-curl-post/68077/1 "2022-09-22T12:31:20Z")

</div>

Hi,

I need to take values from a json and put them into a post request similar to this:

```auto
curl -i -H "Content-Type: application/json" -X POST -d '{"pv_power_forecast":[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 70, 141.22, 246.18, 513.5, 753.27, 1049.89, 1797.93, 1697.3, 3078.93, 1164.33, 1046.68, 1559.1, 2091.26, 1556.76, 1166.73, 1516.63, 1391.13, 1720.13, 820.75, 804.41, 251.63, 79.25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]}' http://localhost:5000/action/dayahead-optim

```

I need to take the marketprice-values from the following screenshot and place them in the received order (0-23) into the -brackets of the above post request in the same format (so separated with a comma and space).  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/9/49b3dc1f1e9b0af560431cccfd6b0e046b63618c.png)

Prior to putting them into the request, I need to manipulate each value by dividing it by 1000 and then substracting 0.0058.

How would I do that?

Thanks!

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [22 September 2022 13:27 UTC](https://discourse.nodered.org/t/values-from-array-to-curl-post/68077/2 "2022-09-22T13:27:14Z")

</div>

You can use the request node set to POST to replace curl. I think that the default for POST bodies is JSON so you probably don't need the content-type header but you can add it anyway in the node.

To deal with the data, you can use a change node or a function node to make changes to your origin json data. You send it to the request on the `payload` property of the msg.

---

<div class="post-metadata">

**Author:** ![smitterer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smitterer/32/59179_2.png) [@smitterer](https://discourse.nodered.org/u/smitterer)\
**Post date:** [22 September 2022 13:53 UTC](https://discourse.nodered.org/t/values-from-array-to-curl-post/68077/3 "2022-09-22T13:53:27Z")

</div>

I tried using the change node and moving msg.payload.marketprice to msg.prices.  
Did some variations by trying msg.payload[i].marketprice, msg.payload.marketprice and so on.

Is there a way to use a variable to get all these marketprices into one msg?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [22 September 2022 16:27 UTC](https://discourse.nodered.org/t/values-from-array-to-curl-post/68077/4 "2022-09-22T16:27:19Z")

</div>

THat does not match up with your first post. To reproduce the example in the first post, you would need to set msg.payload to JSON containing:

```auto
{
    "pv_power_forecast":
        [
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 70, 141.22, 246.18, 513.5, 753.27, 1049.89, 1797.93, 1697.3, 3078.93, 1164.33, 1046.68, 1559.1, 2091.26, 1556.76, 1166.73, 1516.63, 1391.13, 1720.13, 820.75, 804.41, 251.63, 79.25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
        ]
}

```

> [@smitterer](#):
>
> Is there a way to use a variable to get all these marketprices into one msg?

You need to share more information about your input data. You've only described a single msg input so far. There are several ways to reorganise the data on a single input message either using a change node or a function node. If you need to collate multiple input messages, you will need to tell us what they look like, how many, how quickly they arrive, etc.

---

<div class="post-metadata">

**Author:** ![smitterer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smitterer/32/59179_2.png) [@smitterer](https://discourse.nodered.org/u/smitterer)\
**Post date:** [23 September 2022 06:47 UTC](https://discourse.nodered.org/t/values-from-array-to-curl-post/68077/5 "2022-09-23T06:47:30Z")

</div>

Hi,

here is a sample output I receive (unconverted):

```auto
{
  "object": "list",
  "data": [
    {
      "start_timestamp": 1663912800000,
      "end_timestamp": 1663916400000,
      "marketprice": 415.13,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1663916400000,
      "end_timestamp": 1663920000000,
      "marketprice": 344.95,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1663920000000,
      "end_timestamp": 1663923600000,
      "marketprice": 365.18,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1663923600000,
      "end_timestamp": 1663927200000,
      "marketprice": 354.64,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1663927200000,
      "end_timestamp": 1663930800000,
      "marketprice": 317.72,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1663930800000,
      "end_timestamp": 1663934400000,
      "marketprice": 301.71,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1663934400000,
      "end_timestamp": 1663938000000,
      "marketprice": 305.89,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1663938000000,
      "end_timestamp": 1663941600000,
      "marketprice": 321.9,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1663941600000,
      "end_timestamp": 1663945200000,
      "marketprice": 375,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1663945200000,
      "end_timestamp": 1663948800000,
      "marketprice": 422.09,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1663948800000,
      "end_timestamp": 1663952400000,
      "marketprice": 460.13,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1663952400000,
      "end_timestamp": 1663956000000,
      "marketprice": 420.1,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1663956000000,
      "end_timestamp": 1663959600000,
      "marketprice": 384.94,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1663959600000,
      "end_timestamp": 1663963200000,
      "marketprice": 385.43,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1663963200000,
      "end_timestamp": 1663966800000,
      "marketprice": 369.95,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1663966800000,
      "end_timestamp": 1663970400000,
      "marketprice": 369.91,
      "unit": "Eur/MWh"
    }
  ],
  "url": "/at/v1/marketdata"
}

```

At the end, I need to have all values from "marketprice" in the received order in the POST curl.

So the question is how I would place these marketprice values into in the json.

Thanks

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [23 September 2022 12:05 UTC](https://discourse.nodered.org/t/values-from-array-to-curl-post/68077/6 "2022-09-23T12:05:34Z")

</div>

You can do that with JSONata but it always takes me a lot of time to get my head into the right "mode" to work it out.

THe "easy" way (if you are a coder), would be something like:

```auto
const msg2 = { payload: { pv_power_forecast: [] } }
msg.payload.data.forEach( entry => {
    msg2.payload.pv_power_forecast.push( entry.marketprice )
} )
return msg2

```

Note that the above is untested but should be about right. Double check that you get the prices in the right order though, JavaScript can occasionally be a bit loose about the order of things. If there is an issue (which hopefully there isn't in this case), you can do a sort on the data array using the timestamp.

---

<div class="post-metadata">

**Author:** ![smitterer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smitterer/32/59179_2.png) [@smitterer](https://discourse.nodered.org/u/smitterer)\
**Post date:** [24 September 2022 22:07 UTC](https://discourse.nodered.org/t/values-from-array-to-curl-post/68077/7 "2022-09-24T22:07:17Z")

</div>

Hi!

Thanks, this is getting really close.

Any idea how I would round the values to 2 digits and add a space between each?

Thanks!

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [25 September 2022 17:29 UTC](https://discourse.nodered.org/t/values-from-array-to-curl-post/68077/8 "2022-09-25T17:29:33Z")

</div>

You will find advice on how to fix to 2 digits elsewhere in the forum or a quick search will show you a tutorial.

For the space, why would you need that? You are sending JSON not a string.

---

<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:** [24 November 2022 17:29 UTC](https://discourse.nodered.org/t/values-from-array-to-curl-post/68077/9 "2022-11-24T17:29:35Z")

</div>

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