Turn jmespath data to an array

Describe the problem/error/question

I want to add an array object into http-request node, like the below image

I tried with "objects": [{{ $json["data"] }}], and [{{ $jmespath($('Merge').all(),"[].json")}}] but the error is about JSON parameter need to be an valid JSON

Information on your n8n setup

  • n8n version: 1.33.1 (self-hosted)
  • Database (default: SQLite): postgres
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app): docker-compose
  • Operating system: Ubuntu 22

@Le_nAm , use the expression for the whole object, something like this

{{
{
  "version": $json...,
  . . .
  "object": $json.data
}
}}

It seems it wasn’t correct, I’m getting the data from 2 nodes (1st from 3 previous nodes). So the syntax gonna be an issue

This is the data sample (array of object) I want to put into data body

[
{
"version": 
"x2yw23e6gcez",
"name": 
"VIC15_10.10.144.16",
"id": 
"780b67fa-f099-11ee-812c",
"type": 
"networkobject"
},
{
"version": 
"edmzc336zy4lx",
"name": 
"VIC10_10.10.144.16",
"id": 
"811b4b82-f00b-11ee-812c",
"type": 
"networkobject"
}
]

and the body of HTTP-Req should be in json format like this:

{
  "version":  {{ $('3-nodes-back').item.json["version"] }} , 
  "name":"vicGroup", 
  "description":null, 
  "isSystemDefined":false, 
  "id":"28f251fd-ed80-11ee-812c", 
  "objects": {{[ $jmespath($('Merge').all(),"[].json") ]}},
  "type":"networkobjectgroup"
}

the objects attribute must be an array of object. Using jmepath return like this "object": [ {obj1},{obj2} => it missed the ] (closed bracket)

I found the solution

"objects": {{ JSON.stringify($('Merge').all().map((d)=>d.json)) }},

1 Like

@Le_nAm , that solution produces string, not an array (string representation of an array - JSON.stringify()). If that is what you need then it’s fine. If you do need an array, then try what I suggested.

Yes, it produces string and it put square bracket to the result, I don’t know why n8n act it like an array in the request body. I tried your suggestion, but I got invalid synxtax as I commented above. Thanks

@Le_nAm , if $('Merge').all().map( d => d.json) is the array you need, then use it like that (in the object I showed you). Do not use it JSON.stringify(). That is,

{{
{
  "version":  $('3-nodes-back').item.json["version"], 
  "name":"vicGroup", 
  "description":null, 
  "isSystemDefined":false, 
  "id":"28f251fd-ed80-11ee-812c", 
  "objects": $('Merge').all().map( d => d.json),
  "type":"networkobjectgroup"
}
}}
1 Like

Many thanks

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