How to read JSON data in Python script file using n8n node

I’m building an n8n workflow and need help in how to read json data of previous node in python file using n8nnode. Here’s what I’m trying to achieve:

  1. Receive JSON input from a previous node
  2. Process the data in a Python script file
  3. Return modified data as JSON to the next node

Could someone explain:

  • How to properly access incoming JSON data in python file using n8n node?
  • The correct way to return JSON output for downstream nodes?
  • Any best practices or common pitfalls to avoid?

A simple code example showing this data handoff would be extremely helpful!
thanks!

Access incoming JSON data in Python

n8n passes data to the Python node through the items variable. Each item is a dictionary with a .json key that holds the actual payload.

for item in items:
data = item[‘json’] # this is your input JSON

  1. Process the data

You can modify the JSON however you want using standard Python syntax.

  1. Return modified JSON

You must return a list of dictionaries with a json key, like so:

return [
{
“json”: modified_data
}
for modified_data in processed_list
]

Simple code
output =

for item in items:
data = item[‘json’]

# Add a new field based on score
data['passed'] = data['score'] >= 50

output.append({"json": data})

return output