# Create array from objects, whilst using names from current objects

**URL:** <https://discourse.nodered.org/t/create-array-from-objects-whilst-using-names-from-current-objects/66719>\
**Category:** General\
**Created:** [21 August 2022 08:30 UTC](https://discourse.nodered.org/t/create-array-from-objects-whilst-using-names-from-current-objects/66719 "2022-08-21T08:30:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![grant1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/grant1/32/49890_2.png) [@grant1](https://discourse.nodered.org/u/grant1)\
**Post date:** [21 August 2022 08:30 UTC](https://discourse.nodered.org/t/create-array-from-objects-whilst-using-names-from-current-objects/66719/1 "2022-08-21T08:30:44Z")

</div>

Good morning. I have a simple HTTP GET request retrieving metal pricing information for a given date range. [Here is the test URL](https://metals-api.com/api/timeseries?access_key=my_API_key&base=USD&symbols=LME-NI&start_date=2022-01-15&end_date=2022-01-17) returning the data that I want (3 days for testing purposes) and here is my flow shown below. What I have is:

```auto
[{"id":"d07a7fc39c33bb35","type":"http request","z":"afad840914e83550","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"https://metals-api.com/api/timeseries?access_key=ns48wfzhxnaciy7u7yv1r4fd9wk7usx7gj50zq5zjo42w0le9yra4e7ldqb6&base=USD&symbols=LME-NI&start_date=2022-01-15&end_date=2022-01-17","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"headers":[],"x":790,"y":2060,"wires":[["64590608867eba88"]]},{"id":"5e0fd3b289f67476","type":"inject","z":"afad840914e83550","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":660,"y":2000,"wires":[["d07a7fc39c33bb35"]]},{"id":"08c2755f20cd76b9","type":"debug","z":"afad840914e83550","name":"Metals_API_Debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1110,"y":2180,"wires":[]},{"id":"64590608867eba88","type":"change","z":"afad840914e83550","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.rates","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":940,"y":2120,"wires":[["08c2755f20cd76b9"]]}]

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/3/537165fb21b20d689b06791494be6844c62ab2c2.png)

and what I want is:

```auto
array[3]
  0: object
     date: 2022-01-15
     LMI-NI: 1.4084257852516
  1: object
     date: 2022-01-16
     LMI-NI: 1.4073423699781 
  2: object
     date: 2022-01-17
     LMI-NI: 1.4074267919475

```

I have tried various JSONata expressions and attempted a function to create the array, but I am still not seeing the way to use the names of Object0, Object1, Object2 (which in this case are dates) to be included in the array as dates.

Thank you in advance for any pointers.

EDIT: My URL returned "The maximum allowed amount of monthly API requests has been reached." because well, I have exceeded the allowed amount! I have deleted the URL above, but still hoping someone can help me out. Below is what was being returned (prior to the message about exceeding the amount)...

```auto
{
"success": true,
"timeseries": true,
"start_date": "2022-01-15",
"end_date": "2022-01-17",
"base": "USD",
"rates": {
"2022-01-15": {
"LME-NI": 1.4084257852516,
"USD": 1
},
"2022-01-16": {
"LME-NI": 1.4073423699781,
"USD": 1
},
"2022-01-17": {
"LME-NI": 1.4074267919475,
"USD": 1
}
}
}

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [21 August 2022 08:51 UTC](https://discourse.nodered.org/t/create-array-from-objects-whilst-using-names-from-current-objects/66719/2 "2022-08-21T08:51:38Z")

</div>

Have a look at the javascript method [Object.entries()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries) which you can use to give you each of the dates in turn and the object for that date. You can interrogate those to build the array you want by creating it initially empty then using `push()` to add each one in.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [21 August 2022 10:05 UTC](https://discourse.nodered.org/t/create-array-from-objects-whilst-using-names-from-current-objects/66719/3 "2022-08-21T10:05:15Z")

</div>

Here is an example of JSONata expression and a Javascript one.

```auto
[{"id":"5e0fd3b289f67476","type":"inject","z":"30af2d3e.d94ea2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{ \"success\": true, \"timeseries\": true, \"start_date\": \"2022-01-15\", \"end_date\": \"2022-01-17\", \"base\": \"USD\", \"rates\": { \"2022-01-15\": { \"LME-NI\": 1.4084257852516, \"USD\": 1 }, \"2022-01-16\": { \"LME-NI\": 1.4073423699781, \"USD\": 1 }, \"2022-01-17\": { \"LME-NI\": 1.4074267919475, \"USD\": 1 } } }","payloadType":"json","x":110,"y":280,"wires":[["64590608867eba88","8e854a58.f8c708"]]},{"id":"64590608867eba88","type":"change","z":"30af2d3e.d94ea2","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$keys($$.payload.rates).{\"date\": $, \"LME-NI\": $lookup($$.payload.rates, $).`LME-NI`}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":280,"wires":[["08c2755f20cd76b9"]]},{"id":"8e854a58.f8c708","type":"function","z":"30af2d3e.d94ea2","name":"","func":"msg.payload = Object.keys(msg.payload.rates).map(key => {\n return {\"date\": key, \"LME-NI\": msg.payload.rates[key][\"LME-NI\"]}\n})\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":340,"wires":[["08c2755f20cd76b9"]]},{"id":"08c2755f20cd76b9","type":"debug","z":"30af2d3e.d94ea2","name":"Metals_API_Debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":570,"y":280,"wires":[]}]

```

JSONata expression

```auto
$keys($$.payload.rates).{"date": $, "LME-NI": $lookup($$.payload.rates, $).`LME-NI`}

```

Javascript

```auto
msg.payload = Object.keys(msg.payload.rates).map(key => {
    return {"date": key, "LME-NI": msg.payload.rates[key]["LME-NI"]}
})
return msg;

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [21 August 2022 13:07 UTC](https://discourse.nodered.org/t/create-array-from-objects-whilst-using-names-from-current-objects/66719/4 "2022-08-21T13:07:39Z")

</div>

Or using Object.entries

```auto
msg.payload = Object.entries(msg.payload.rates).map( e => { 
    return { date: e[0], "LME-INI": e[1]["LME-NI"]} 
    }
)
return msg;

```

---

<div class="post-metadata">

**Author:** ![grant1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/grant1/32/49890_2.png) [@grant1](https://discourse.nodered.org/u/grant1)\
**Post date:** [21 August 2022 20:05 UTC](https://discourse.nodered.org/t/create-array-from-objects-whilst-using-names-from-current-objects/66719/5 "2022-08-21T20:05:45Z")

</div>

Thank you both. I had to move to [www.commodities-api.com](http://www.commodities-api.com) (where I had not used up my monthly allowance of API calls) and while the API response differs slightly...

```auto
{
    "data": {
        "success": true,
        "timeseries": true,
        "start_date": "2022-01-15",
        "end_date": "2022-01-17",
        "base": "USD",
        "rates": {
            "2022-01-15": {
                "NI": 1.4460820044529,
                "USD": 1
            },
            "2022-01-16": {
                "NI": 1.4466022662096,
                "USD": 1
            },
            "2022-01-17": {
                "NI": 1.4463421353313,
                "USD": 1
            }
        },
        "unit": "per ton"
    }
}

```

...I was able to modify each of the methods you suggested, and of course they all work nicely. I also did some more reading on each of these functions to learn more how they are structured and applied.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/b/1b095b51a67f7dd309daa185b426d88ad77c4933.png)

Here is the flow shown above.

```auto
[{"id":"30b4390ec950e1a2","type":"inject","z":"afad840914e83550","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"data\":{\"success\":true,\"timeseries\":true,\"start_date\":\"2022-01-15\",\"end_date\":\"2022-01-17\",\"base\":\"USD\",\"rates\":{\"2022-01-15\":{\"NI\":1.4460820044529,\"USD\":1},\"2022-01-16\":{\"NI\":1.4466022662096,\"USD\":1},\"2022-01-17\":{\"NI\":1.4463421353313,\"USD\":1}},\"unit\":\"per ton\"}}","payloadType":"json","x":170,"y":1800,"wires":[["c4252abd86c2dd8d","2663f3c93dd50de5","db6bf8f8ba4fa973"]]},{"id":"c4252abd86c2dd8d","type":"change","z":"afad840914e83550","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$keys($$.payload.data.rates).{\"date\": $, \"NI\": $lookup($$.payload.data.rates, $).`NI`}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":1800,"wires":[["2110bed57d552e2c"]]},{"id":"2663f3c93dd50de5","type":"function","z":"afad840914e83550","name":"Object.keys() function","func":"msg.payload = Object.keys(msg.payload.data.rates).map(key => {\n return {\"date\": key, \"NI\": msg.payload.data.rates[key][\"NI\"]}\n})\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":400,"y":1860,"wires":[["63a28ba78b7762b7"]]},{"id":"2110bed57d552e2c","type":"debug","z":"afad840914e83550","name":"JSONata debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":620,"y":1800,"wires":[]},{"id":"63a28ba78b7762b7","type":"debug","z":"afad840914e83550","name":"Object.keys() debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":660,"y":1860,"wires":[]},{"id":"db6bf8f8ba4fa973","type":"function","z":"afad840914e83550","name":"Object.entries() function","func":"msg.payload = Object.entries(msg.payload.data.rates).map(e => {\n return { date: e[0], \"NI\": e[1][\"NI\"] }\n}\n)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":390,"y":1940,"wires":[["3f92baf5378d0b77"]]},{"id":"3f92baf5378d0b77","type":"debug","z":"afad840914e83550","name":"Object.entries() debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":640,"y":1940,"wires":[]}]

```

---

<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:** [4 September 2022 20:06 UTC](https://discourse.nodered.org/t/create-array-from-objects-whilst-using-names-from-current-objects/66719/6 "2022-09-04T20:06:08Z")

</div>

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