Struggling again with Set

Hi all,

I’m again trying to transform some datas extracted from an API with set, but it seems I need a function to do so, but I don’t know how to do.

Here is a mockup workflow with some datas, and I expect a table with time and power as column values.

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n    {\n      json: {\n\"propertyName\": {\n\"26400\": 0,\n\"26700\": 0,\n\"27000\": 0,\n\"27300\": 0,\n\"27600\": 0,\n\"27900\": 0,\n\"28200\": 4.377222222222223,\n\"28500\": 5.471111111111111,\n\"28800\": 6.425277777777778,\n\"29100\": 7.099444444444444,\n\"29400\": 7.826666666666666,\n\"29700\": 8.352777777777778,\n\"30000\": 9.030555555555555,\n\"30300\": 9.58,\n\"30600\": 10.292222222222222,\n\"30900\": 10.724722222222223,\n\"31200\": 11.520555555555555,\n\"31500\": 12.000833333333333,\n\"31800\": 12.913055555555555,\n\"32100\": 14.123888888888889,\n\"32400\": 14.573333333333334,\n\"32700\": 15.518611111111111,\n\"33000\": 16.241666666666667,\n\"33300\": 18.358333333333334,\n\"33600\": 19.5725,\n\"33900\": 20.325,\n\"34200\": 21.240555555555556,\n\"34500\": 23.6625,\n\"34800\": 24.251666666666665,\n\"35100\": 29.297777777777778,\n\"35400\": 33.26777777777778,\n\"35700\": 40.958888888888886,\n\"36000\": 46.028055555555554,\n\"36300\": 50.00138888888889,\n\"36600\": 54.01611111111111,\n\"36900\": 59.30416666666667\n}\n}\n     }  \n  ]\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ],
      "notesInFlow": true,
      "notes": "HTTP Request Mockup"
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "time",
              "value": "={{Object.keys($json[\"propertyName\"]).join(', ')}}"
            },
            {
              "name": "power",
              "value": "={{Object.values($json[\"propertyName\"]).join(', ')}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        630,
        310
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Is there a way to do it without code ? If not, what would be the right function ?

Thanks a lot for your help !

Hey @mcc37,

You will need to write a function to transform the data. In the sample data you provided, are the Key values the time? Eg. for 26400: 0, is 26400 the time and 0 the power?

Yes, 26400 is time and 0 power.

It’s a list of power value every 5 minutes between a start date and a end date.

I wonder if the new Item List helper node that @maxT has been working on would help.

Hey @mcc37,

Using the mock data you provided, I’ve written this function that might be helpful.

const data = Object.keys(items[0].json.propertyName).map(i => ({json:{time:i, power:items[0].json.propertyName[i]}}));
return data;
2 Likes

Thanks a lot @harshil1712 , it works perfectly !

Not so easy to wrote those functions, would you have a recommandations of web pointers to learn basics of javascript / json so I would be able to build it ?

@mcc37 as much as some people don’t like them the W3Schools javascript page is great for reference, Mozilla have a great reference page with some tutorials as well.

Another handy reference is here on the community pages, The search box at the top is great for looking at older questions and might help with some functions as chances are someone has wanted to do the same thing before.

4 Likes

Thanks for mentioning me @jon - just checked the usecase and the V1 of Item List helper node would not be able to perform this specific mapping. Will ensure this usecase is recorded when we look to add functionality to the node!

2 Likes

Thanks Jon for those links !

Thanks @maxT. This node seems to be very interesting, I regularly have this type of use case and each API has its own way of sharing series of datas.

That’s good to know. If you could, it would be super helpful if you could DM me a few examples cases. Best format would be simply an “input json” and an expected “output json”. Then we can use these cases directly when designing new operations for the node. Thanks :pray: