Aggregating and inserting JSON is adding extra brackets

I have multiple nodes manipulating JSON which come together in an aggregate node which is inserted into the JSON body of a http request.

The issue appears to be that once the aggregated json is dropped into the JSON body, extra brackets appear and the JSON is invalid.
Error: “JSON parameter need to be an valid JSON”

Here’s a snippet of the failing json:
[{“type”: “text”, “text”: “screenshot.com”}},{“type”: “paragraph”, “content”: [{“type”: “text”, “text”: “Link to result”, “marks”: [{“type”: “strong”}}},{“type”: “paragraph”, “content”: [{“type”: “text”, “text”: “resultlink.com”}}}}

You can see the last line after “content” opens with square bracket, but only closes with a bunch of curly brackets etc.

The JSON generated in the previous node (aggregate) does not have this issue - it only appears to happen when its dropped into the body of the http request node.

Information on your n8n setup

  • n8n version: [email protected] (I tried on 1.58 and 1.60 as well)
  • Database (default: SQLite): default
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app): cloud
  • Operating system: Windows

Hi @Gary1,

Welcome to the community :four_leaf_clover:

You can try wrapping the data in a JSON.stringify() function:

{{ JSON.stringify($json.data[0])}}

CleanShot 2024-09-26 at 10.29.39

2 Likes

JSON.stringify() did produce better results, and here’s the working section:

{{ JSON.stringify($json.data[0]) }},{{ JSON.stringify($json.data[1]) }}

I would like to know why the Aggregate or Merge nodes don’t just return the json as a blob so I can just substitute it - I mean with this I may as well have just inserted the output from the Edit Fields node 2 steps back - I’m just referring to two separate json objects here anyway…

Thanks

Hi @Gary1

You are right, in your use case you wouldn’t need the Aggregate or Merge node.

These nodes are typically used for more complex data structuring like nesting or de-nesting and mapping.

All you needed in your use case was to format the data using JSON.stringifiy($json.data[0]) or alternatively $json.data[0].toJsonString()

1 Like

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