Create array from objects, whilst using names from current objects

Good morning. I have a simple HTTP GET request retrieving metal pricing information for a given date range. Here is the test URL returning the data that I want (3 days for testing purposes) and here is my flow shown below. What I have is:

[{"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"]]}]

and what I want is:

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

{
"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
}
}
}

Have a look at the javascript method 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.

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

[{"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

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

Javascript

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

Or using Object.entries

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

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

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

Here is the flow shown above.

[{"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":[]}]
1 Like

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