Split message, do some conversion and do several http requests

Hi,

I have this sample JSON as input:

{
  "viewer": {
    "home": {
      "id": "uniqueid1",
      "currentSubscription": {
        "id": "uniqueid2",
        "priceInfo": {
          "current": {
            "total": 1.7443,
            "energy": 1.3188,
            "tax": 0.4255,
            "startsAt": "2022-10-18T17:00:00.000+02:00",
            "level": "VERY_EXPENSIVE",
            "currency": "SEK"
          },
          "today": [
            {
              "total": 0.4291,
              "energy": 0.2666,
              "tax": 0.1625,
              "startsAt": "2022-10-18T00:00:00.000+02:00",
              "level": "CHEAP",
              "currency": "SEK"
            },
            {
              "total": 0.4243,
              "energy": 0.2627,
              "tax": 0.1616,
              "startsAt": "2022-10-18T01:00:00.000+02:00",
              "level": "CHEAP",
              "currency": "SEK"
            },
            //... more <clip>
            {
              "total": 0.3157,
              "energy": 0.1758,
              "tax": 0.1399,
              "startsAt": "2022-10-18T23:00:00.000+02:00",
              "level": "VERY_CHEAP",
              "currency": "SEK"
            }
          ],
          "tomorrow": [
            {
              "total": 0.3143,
              "energy": 0.1747,
              "tax": 0.1396,
              "startsAt": "2022-10-19T00:00:00.000+02:00",
              "level": "VERY_CHEAP",
              "currency": "SEK"
            },
            {
              "total": 0.3103,
              "energy": 0.1716,
              "tax": 0.1387,
              "startsAt": "2022-10-19T01:00:00.000+02:00",
              "level": "VERY_CHEAP",
              "currency": "SEK"
            },
            {
              "total": 0.311,
              "energy": 0.1721,
              "tax": 0.1389,
              "startsAt": "2022-10-19T02:00:00.000+02:00",
              "level": "VERY_CHEAP",
              "currency": "SEK"
            },
            //... more <clip>
            {
              "total": 0.365,
              "energy": 0.2153,
              "tax": 0.1497,
              "startsAt": "2022-10-19T23:00:00.000+02:00",
              "level": "VERY_CHEAP",
              "currency": "SEK"
            }
          ]
        }
      }
    }
  }
}

I need to :

  1. Iterate over both arrays today and tomorrow
  2. On each item convert date to Epoch (no issue)
  3. On each item do a HTTP POST request with the data

How can I do that the best/easiest way i Node-RED?

Something like this...

[{"id":"96f97c9a4772bf8b","type":"inject","z":"a974952f9392fa56","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":560,"y":1060,"wires":[["8870054357eceb69"]]},{"id":"8870054357eceb69","type":"template","z":"a974952f9392fa56","name":"your data","field":"payload","fieldType":"msg","format":"json","syntax":"mustache","template":"{\n  \"viewer\": {\n    \"home\": {\n      \"id\": \"uniqueid1\",\n      \"currentSubscription\": {\n        \"id\": \"uniqueid2\",\n        \"priceInfo\": {\n          \"current\": {\n            \"total\": 1.7443,\n            \"energy\": 1.3188,\n            \"tax\": 0.4255,\n            \"startsAt\": \"2022-10-18T17:00:00.000+02:00\",\n            \"level\": \"VERY_EXPENSIVE\",\n            \"currency\": \"SEK\"\n          },\n          \"today\": [\n            {\n              \"total\": 0.4291,\n              \"energy\": 0.2666,\n              \"tax\": 0.1625,\n              \"startsAt\": \"2022-10-18T00:00:00.000+02:00\",\n              \"level\": \"CHEAP\",\n              \"currency\": \"SEK\"\n            },\n            {\n              \"total\": 0.4243,\n              \"energy\": 0.2627,\n              \"tax\": 0.1616,\n              \"startsAt\": \"2022-10-18T01:00:00.000+02:00\",\n              \"level\": \"CHEAP\",\n              \"currency\": \"SEK\"\n            },\n            {\n              \"total\": 0.3157,\n              \"energy\": 0.1758,\n              \"tax\": 0.1399,\n              \"startsAt\": \"2022-10-18T23:00:00.000+02:00\",\n              \"level\": \"VERY_CHEAP\",\n              \"currency\": \"SEK\"\n            }\n          ],\n          \"tomorrow\": [\n            {\n              \"total\": 0.3143,\n              \"energy\": 0.1747,\n              \"tax\": 0.1396,\n              \"startsAt\": \"2022-10-19T00:00:00.000+02:00\",\n              \"level\": \"VERY_CHEAP\",\n              \"currency\": \"SEK\"\n            },\n            {\n              \"total\": 0.3103,\n              \"energy\": 0.1716,\n              \"tax\": 0.1387,\n              \"startsAt\": \"2022-10-19T01:00:00.000+02:00\",\n              \"level\": \"VERY_CHEAP\",\n              \"currency\": \"SEK\"\n            },\n            {\n              \"total\": 0.311,\n              \"energy\": 0.1721,\n              \"tax\": 0.1389,\n              \"startsAt\": \"2022-10-19T02:00:00.000+02:00\",\n              \"level\": \"VERY_CHEAP\",\n              \"currency\": \"SEK\"\n            },\n            {\n              \"total\": 0.365,\n              \"energy\": 0.2153,\n              \"tax\": 0.1497,\n              \"startsAt\": \"2022-10-19T23:00:00.000+02:00\",\n              \"level\": \"VERY_CHEAP\",\n              \"currency\": \"SEK\"\n            }\n          ]\n        }\n      }\n    }\n  }\n}","output":"json","x":710,"y":1060,"wires":[["b9f6375f64e9a9ec"]]},{"id":"b9f6375f64e9a9ec","type":"function","z":"a974952f9392fa56","name":"join arrays","func":"\nconst today = msg.payload.viewer.home.currentSubscription.priceInfo.today\nconst tomorrow = msg.payload.viewer.home.currentSubscription.priceInfo.tomorrow\nmsg.payload = [...today, ...tomorrow]\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":1120,"wires":[["acdbe412348134fa","fbe14ea8bf5167dc"]]},{"id":"acdbe412348134fa","type":"split","z":"a974952f9392fa56","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":740,"y":1120,"wires":[["20710b9c66fb1a5d","b5898d56b6cd9e15"]]},{"id":"fbe14ea8bf5167dc","type":"debug","z":"a974952f9392fa56","name":"debug 118","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":1180,"wires":[]},{"id":"20710b9c66fb1a5d","type":"debug","z":"a974952f9392fa56","name":"debug 119","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":790,"y":1180,"wires":[]},{"id":"b5898d56b6cd9e15","type":"function","z":"a974952f9392fa56","name":"prep URL","func":"\nmsg.url = `http://ip_address/api/v1/${msg.payload.currency}`\nmsg.method = 'post'\nmsg.payload = {\n    total: msg.payload.total,\n    energy: msg.payload.energy,\n    tax: msg.payload.tax,\n    level: msg.payload.level,\n    timestamp: new Date(msg.payload.startsAt).valueOf()\n}\nmsg.headers = {\n    'my-header': 'some-value'\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":900,"y":1120,"wires":[["4b919a74080fdf72"]]},{"id":"4b919a74080fdf72","type":"debug","z":"a974952f9392fa56","name":"to HTTP Req node →","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1100,"y":1120,"wires":[]}]
1 Like

Wow, thanks. Will test it out

Works like a charm (with some tweaks of things I didn't mention :wink: )
Thanks @Steve-Mcl

1 Like

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