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)"
    ]
  }
}

OK, looks like I was wrong about JSON.stringify() automatically adding quotes. I think the problem is that your string includes quotes itself, so they need escaping.

This version makes sure the surrounding quotes are added, and also escapes any quotes that are in the content:

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

I solved it. The google analytics was returning an array of objects. Plus parsing it and sending it to HTTP request requires encoding it for URI.

I’ve solved it and published it as a template for everyone else to enjoy!

Great! And thanks for the template, Keith