How to send Google analytics JSON to http request

I am getting google analytics data and then I want to pass that data to openrouter.ai using an http request to analyze the data.

The issue is that when I pass the array of data to http request, I keep getting invalid JSON format.

This is the expression I am using inside my HTTP request.

{
  "model": "meta-llama/llama-3.1-70b-instruct:free",
  "messages": [
    {
      "role": "user",
      "content": "{{ JSON.stringify($('Aggregate').item.json.data) }}"
    }
  ]
}

The data looks like this:

}

],

"metricValues": [

{

"value": "6"

},

{

"value": "4"

},

{

"value": "1.5"

},

{

"value": "19"

}

]

I’ve tried to wrap the data using JSON.stringify() but it won’t work. I even tried to put in code to aggregate all the data but all the same issue.

For reference, I can do this much easier in make.com. Make combines all the values into text files automatically and then passes it to my http request.


Information on your n8n setup

  • n8n version: 1.63.4
  • Database (default: SQLite): PostgreSQP
  • n8n EXECUTIONS_PROCESS setting (default: own, main): default
  • Running n8n via (Docker, npm, n8n cloud, desktop app): docker
  • Operating system: Google cloud

JSON.stringify() automatically adds quotes around your string, so you don’t need to add your own ones. If that doesn’t solve your problem please share the rendered output that your expression produces. I’ve corrected it here:

{
  "model": "meta-llama/llama-3.1-70b-instruct:free",
  "messages": [
    {
      "role": "user",
      "content": {{ JSON.stringify($('Aggregate').item.json.data) }}
    }
  ]
}

I tried this and now I have new weird issue whereby the node runs infinitely. Removing the double quotes is doing something but I’m not sure what! =)

The node eventually aborts

{
  "errorMessage": "aborted",
  "errorDetails": {},
  "n8nDetails": {
    "n8nVersion": "1.63.4 (Self Hosted)",
    "binaryDataMode": "default",
    "stackTrace": [
      "Error: aborted",
      "    at TLSSocket.socketCloseListener (node:_http_client:464:19)",
      "    at TLSSocket.emit (node:events:531:35)",
      "    at TLSSocket.emit (node:domain:488:12)",
      "    at node:net:339:12",
      "    at TCP.done (node:_tls_wrap:648:7)"
    ]
  }
}