Building a custom json with SET?

Hi all,

Thanks to you help yesterday, I’ve been able to build a pretty cool flow to request a Solar Measurement API at Solcast.

At the end of my workflow, I have a table with a lot of line of that kind in a nice json format:

[
{
"period_end": "2021-08-25T22:05:00.000Z",
"period": "PT5M",
"total_power": 0
},
{
"period_end": "2021-08-25T22:10:00.000Z",
"period": "PT5M",
"total_power": 0
},
{
"period_end": "2021-08-25T22:15:00.000Z",
"period": "PT5M",
"total_power": 0
}
]

But, I need to request the API with this :

{
  "measurements": [
{
"period_end": "2021-08-25T22:05:00.000Z",
"period": "PT5M",
"total_power": 0
},
{
"period_end": "2021-08-25T22:10:00.000Z",
"period": "PT5M",
"total_power": 0
},
{
"period_end": "2021-08-25T22:15:00.000Z",
"period": "PT5M",
"total_power": 0
}
]
}

Is there a way to do it without code ? Otherwise, what would be the right function to produce the right json whatever the number of line of the table ?

Thanks a lot for your help.

A feed-back that would be cool to find on the documentation, it’s how to do maths using SET node. I did find the answer on previous community question, but it would make sens to find it in documentations.

Next week probably we are going to release a new node called Item List Helper which is going to make uses cases like this one really easy to solve.

In the meantime, you can use a function node as shown below:

return [
  {
    json: {
      measurements: items.map(item => item.json)
    }
  }
]
Example workflow
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n{\n json: {\n   \"period_end\": \"2021-08-25T22:05:00.000Z\",\n\"period\": \"PT5M\",\n\"total_power\": 0\n }\n},\n{\n  json: {\n\"period_end\": \"2021-08-25T22:10:00.000Z\",\n\"period\": \"PT5M\",\n\"total_power\": 0\n  }\n},\n{  \n  json: {\n\"period_end\": \"2021-08-25T22:15:00.000Z\",\n\"period\": \"PT5M\",\n\"total_power\": 0\n}\n  }\n]\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        500,
        300
      ],
      "notesInFlow": true,
      "notes": "Mockup Input"
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      measurements: items.map(item => item.json)\n    }\n  }\n]"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        720,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "Function1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like

Thanks @RicardoE105, it works fine !

I will be pretty happy to test this new node.

Hi again,

It does not work when I request the API, I did find the issue with postman.

The function @RicardoE105 did suggest start the json with [].

So if I take an example, the expected body for the API request is :

{
"measurements": [
{
"period_end": "2021-08-26T05:25:00.000Z",
"period": "PT5M",
"total_power": 0
},
{
"period_end": "2021-08-26T05:30:00.000Z",
"period": "PT5M",
"total_power": 0
}
]
}

Actually, I have this format with the function :

[
{
"measurements": [
{
"period_end": "2021-08-26T05:25:00.000Z",
"period": "PT5M",
"total_power": 0
},
{
"period_end": "2021-08-26T05:30:00.000Z",
"period": "PT5M",
"total_power": 0
}
]
}
]

What should I change in the function code ? Or do to request the API with the right body ?

Thanks a lot for your help

Just reference the array in the HTTP node.

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n{\n json: {\n   \"period_end\": \"2021-08-25T22:05:00.000Z\",\n\"period\": \"PT5M\",\n\"total_power\": 0\n }\n},\n{\n  json: {\n\"period_end\": \"2021-08-25T22:10:00.000Z\",\n\"period\": \"PT5M\",\n\"total_power\": 0\n  }\n},\n{  \n  json: {\n\"period_end\": \"2021-08-25T22:15:00.000Z\",\n\"period\": \"PT5M\",\n\"total_power\": 0\n}\n  }\n]\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        500,
        300
      ],
      "notesInFlow": true,
      "notes": "Mockup Input"
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      measurements: items.map(item => item.json)\n    }\n  }\n]"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        720,
        300
      ]
    },
    {
      "parameters": {
        "requestMethod": "POST",
        "options": {},
        "bodyParametersUi": {
          "parameter": [
            {
              "name": "measurements",
              "value": "={{$node[\"Function1\"].json[\"measurements\"]}}"
            }
          ]
        }
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        920,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "Function1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function1": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like

Thanks a lot @RicardoE105. I made it work !

Now I can send my PV energy produced of the day before to Solcast API to compare if their prediction as good or not.

An overview of the flow :

And the result on solcast Website :

A bit of industrialization to do to make it run once a day at night and to be notified of execution error, and it will be all good.

N8N is really great ! If you achieve to enhance how to manipulate data without function, it will be amazing.

Hey @mcc37!

This looks amazing! We are making final changes to the new Items node which would make data transformation easy :wink:

1 Like

I will be happy to check this node !

1 Like