make landing page less JS-focused; add Python to JMESPath

This commit is contained in:
Deborah Barnard 2023-10-12 11:35:29 +01:00
parent d1724ebe65
commit 7b25db10db
7 changed files with 138 additions and 46 deletions

View File

@ -0,0 +1,16 @@
Nodes can process multiple items.
For example, if you set the Trello node to `Create-Card`, and create an expression that sets `Name` using a property called `name-input-value` from the incoming data, the node creates a card for each item, always choosing the `name-input-value` of the current item.
For example, this input will create two cards. One named `test1` the other one named `test2`:
```json
[
{
name-input-value: "test1"
},
{
name-input-value: "test2"
}
]
```

View File

@ -29,9 +29,6 @@ The Code node supports:
If you self-host n8n, you can import and use built-in and external npm modules in the Code node. To learn how to enable external modules, refer the [Configuration](/hosting/configuration/#use-built-in-and-external-modules-in-the-code-node) guide.
### Use AI in the Code node
--8<-- "_snippets/code-examples/ai-how-to.md"
## Python

View File

@ -21,6 +21,9 @@ n8n supports two libraries:
- [Luxon](https://github.com/moment/luxon/){:target=_blank .external-link}, for working with data and time.
- [JMESPath](https://jmespath.org/){:target=_blank .external-link}, for querying JSON.
!!! note "No Python support"
Expressions must use JavaScript.
!!! note "Data in n8n"
When writing expressions, it's helpful to understand data structure and behavior in n8n. Refer to [Data](/data/) for more information on working with data in your workflows.

View File

@ -7,7 +7,7 @@ contentType: overview
n8n is a low-code tool. This means you can do a lot without code, then add code when needed.
## JavaScript in your workflows
## Code in your workflows
There are two places in your workflows where you can use code:
@ -21,12 +21,13 @@ There are two places in your workflows where you can use code:
- __Code node__
The Code node allows you to add JavaScript to your workflow.
The Code node allows you to add JavaScript or Python to your workflow.
[:octicons-arrow-right-24: Code node](/code/code-node/)
</div>
## Other technical resources
These are features that are relevant to technical users.

View File

@ -8,16 +8,21 @@ contentType: howto
[JMESPath](https://jmespath.org/){:target=_blank .external-link} is a query language for JSON, allowing you to extract and transform elements from a JSON document. For full details of how to use JMESPath, refer to the [JMESPath documentation](https://jmespath.org/tutorial.html){:target=_blank .external-link}.
## The `$jmespath()` method
n8n provides a custom method, `$jmespath()`. It allows you to perform a search on a JSON object using the JMESPath query language.
## The `jmespath()` method
n8n provides a custom method, `jmespath()`. It allows you to perform a search on a JSON object using the JMESPath query language.
The basic syntax is:
```js
$jmespath(object, searchString)
```
=== "JavaScript"
```js
$jmespath(object, searchString)
```
=== "Python"
```python
_jmespath(object, searchString)
```
To help understand what the method does, here is the equivalent longer JavaScript:
@ -42,6 +47,8 @@ jmespath.search(object, searchString);
This section provides examples for some common operations. More examples, and detailed guidance, are available in [JMESPath's own documentation](https://jmespath.org/tutorial.html){:target=_blank .external-link}.
When trying out these examples, you need to set the Code node **Mode** to **Run Once for Each Item**.
### Apply a JMESPath expression to a collection of elements with projections
From the [JMESPath projections documentation](https://jmespath.org/tutorial.html#projections){:target=_blank .external-link}:
@ -102,17 +109,18 @@ Given this JSON from a webhook node:
Retrieve a [list](https://jmespath.org/tutorial.html#list-and-slice-projections){:target=_blank .external-link} of all the people's first names:
=== "Expressions"
=== "Expressions (JavaScript)"
```js
{{$jmespath($json.body.people, "[*].first" )}}
// Returns ["James", "Jacob", "Jayden"]
```
=== "Code node"
=== "Code node (JavaScript)"
```js
$jmespath($json.body.people, "[*].first" )
let firstNames = $jmespath($json.body.people, "[*].first" )
return {firstNames};
/* Returns:
[
{
@ -124,20 +132,38 @@ Retrieve a [list](https://jmespath.org/tutorial.html#list-and-slice-projections)
}
]
*/
```
```
=== "Code node (Python)"
```python
firstNames = _jmespath(_json.body.people, "[*].first" )
return {"firstNames":firstNames}
"""
Returns:
[
{
"firstNames": [
"James",
"Jacob",
"Jayden"
]
}
]
"""
```
Get a [slice](https://jmespath.org/tutorial.html#list-and-slice-projections){:target=_blank .external-link} of the first names:
=== "Expressions"
=== "Expressions (JavaScript)"
```js
{{$jmespath($json.body.people, "[:2].first")}}
// Returns ["James", "Jacob"]
```
=== "Code node"
=== "Code node (JavaScript)"
```js
$jmespath($json.body.people, "[:2].first")
let firstTwoNames = $jmespath($json.body.people, "[:2].first");
return {firstTwoNames};
/* Returns:
[
{
@ -150,26 +176,63 @@ Get a [slice](https://jmespath.org/tutorial.html#list-and-slice-projections){:ta
]
*/
```
=== "Code node (Python)"
```python
firstTwoNames = _jmespath(_json.body.people, "[:2].first" )
return {"firstTwoNames":firstTwoNames}
"""
Returns:
[
{
"firstTwoNames": [
"James",
"Jacob"
]
}
]
"""
```
Get a list of the dogs' ages using [object projections](https://jmespath.org/tutorial.html#object-projections){:target=_blank .external-link}:
=== "Expressions"
=== "Expressions (JavaScript)"
```js
{{$jmespath($json.body.dogs, "*.age")}}
// Returns [7,5]
```
=== "Code node"
=== "Code node (JavaScript)"
```js
$jmespath($json.body.dogs, "*.age")
let dogsAges = $jmespath($json.body.dogs, "*.age");
return {dogsAges};
/* Returns:
[
7,
5
{
"dogsAges": [
7,
5
]
}
]
*/
```
```
=== "Code node (Python)"
```python
dogsAges = _jmespath(_json.body.dogs, "*.age")
return {"dogsAges": dogsAges}
"""
Returns:
[
{
"dogsAges": [
7,
5
]
}
]
"""
```
### Select multiple elements and create a new list or object
@ -221,7 +284,7 @@ Given this JSON from a webhook node:
Use multiselect list to get the first and last names and create new lists containing both names:
=== "Expressions"
=== "Expressions (JavaScript)"
[[% raw %]]
```js
@ -230,14 +293,15 @@ Use multiselect list to get the first and last names and create new lists contai
```
[[% endraw %]]
=== "Code node"
=== "Code node (JavaScript)"
```js
$jmespath($json.body.people, "[].[first, last]")
let newList = $jmespath($json.body.people, "[].[first, last]");
return {newList};
/* Returns:
[
{
"fullNames": [
"newList": [
[
"James",
"Green"
@ -255,8 +319,34 @@ Use multiselect list to get the first and last names and create new lists contai
]
*/
```
=== "Code node (Python)"
```python
newList = _jmespath(_json.body.people, "[].[first, last]")
return {"newList":newList}
"""
Returns:
[
{
"newList": [
[
"James",
"Green"
],
[
"Jacob",
"Jones"
],
[
"Jayden",
"Smith"
]
]
}
]
"""
```
### An alternative to arrow functions
### An alternative to arrow functions in expressions
For example, generate some input data by returning the below code from the Code node:

View File

@ -6,6 +6,6 @@ contentType: explanation
## Function
A function is a block of code designed to perform a certain task. In n8n, you can write custom JavaScript code snippets to add, remove, and update the data you receive from a node.
A function is a block of code designed to perform a certain task. In n8n, you can write custom JavaScript or Python code snippets to add, remove, and update the data you receive from a node.
The [Code](/integrations/builtin/core-nodes/n8n-nodes-base.code/) node gives you access to the incoming data and you can manipulate it. With this node you can implement any function you want using JavaScript code.

View File

@ -5,19 +5,4 @@ description: How nodes process data items.
# Data flow within nodes
Nodes can process multiple items.
For example, if you set the Trello node to `Create-Card`, and create an expression that sets `Name` using a property called `name-input-value` from the incoming data, the node creates a card for each item, always choosing the `name-input-value` of the current item.
For example, this input will create two cards. One named `test1` the other one named `test2`:
```json
[
{
name-input-value: "test1"
},
{
name-input-value: "test2"
}
]
```
--8<-- "_snippets/flow-logic/data-flow-nodes.md"