N8n Get Workflow followed by Update Workflow not working

Describe the problem/error/question

I am trying to update the credentials on a workflow so that I can cycle through several sets of credentials I need to pull the same data for.

So in the beginning of the workflow, I get the contents of the other workflow with a n8n Get Workflow node.

Subsequently, in a code node, I update the credentials to an exact copy of the credentials object in the node like so:

workflow = items[0].json.workflow;
workflow.nodes.forEach(node => {
      if (node.credentials && node.credentials.someOAuth2Api) {
          node.credentials = items[0].json.credentials;
      }
  });
var results = { workflow: {}};
results.workflow.nodes = JSON.stringify(workflow.nodes);
results.workflow.settings = workflow.settings
results.workflow.name = workflow.name;
results.workflow.connections = workflow.connections;
return results;

However, no matter what I do when I try to send the Update Workflow it gives me the following error:

NodeApiError: The 'workflowObject' property must be valid JSON, but cannot be parsed
    at RoutingNode.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-workflow/dist/RoutingNode.js:117:23)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at Workflow.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-workflow/dist/Workflow.js:733:23)
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/WorkflowExecute.js:656:53

I have tried a million different ways to format workflowObject but it fails. I am also not quite sure what that error means. What is the expected format for this endpoint?

Here is the workflow object:

    "connections": {
        "Execute Workflow Trigger": {
            "main": [
                [
                    {
                        "index": 0,
                        "node": "getCompanyInfo",
                        "type": "main"
                    }
                ]
            ]
        }
    },
    "name": "Dev - QBO - GetCompanyInfo",
    "nodes": "[{\"parameters\":{},\"id\":\"someId\",\"name\":\"Execute Workflow Trigger\",\"type\":\"n8n-nodes-base.executeWorkflowTrigger\",\"typeVersion\":1,\"position\":[460,240]},{\"parameters\":{\"url\":\"=https://quickbooks.api.intuit.com/v3/company/{{ $json.QuickbooksCompanyId }}/query\",\"authentication\":\"predefinedCredentialType\",\"nodeCredentialType\":\"quickBooksOAuth2Api\",\"sendQuery\":true,\"queryParameters\":{\"parameters\":[{\"name\":\"query\",\"value\":\"SELECT Metadata.CreateTime from CompanyInfo \"}]},\"options\":{}},\"id\":\"someThatId\",\"name\":\"getCompanyInfo\",\"type\":\"n8n-nodes-base.httpRequest\",\"typeVersion\":4.1,\"position\":[660,240],\"credentials\":{\"quickBooksOAuth2Api\":{\"id\":\"someOtherId\",\"name\":\"Quickbooks - someClient\"}}}]",
    "settings": {
        "executionOrder": "v1"
    },
    "staticData": null
}

and here is the workflow itself:

  • n8n version: Latest
  • Database (default: SQLite): Postgres
  • n8n EXECUTIONS_PROCESS setting (default: own, main): Not sure how to find this out
  • Running n8n via (Docker, npm, n8n cloud, desktop app): kubernetes Google Cloud self hosted

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

Hi @tipsykat, the n8n API is unfortunately really picky about which values it accepts and will throw errors if it gets more than it likes :frowning:

So such a process will require a bit of trial and error. That said, I believe the below will achieve what you have in mind (once you update the workflow ID to match your system of course):

Changes I have made to your data structure are:

  • No longer stringify workflow.nodes
  • Write an empty settings object as the API seems to reject some of them on update (specifically executionOrder for me - perhaps @RicardoE105 can confirm if this is intentional or if I was just being stupid)

The update is now working for me:

Hope this helps!

Thank you, I think I am on my way to getting this to work.

Would settings: {} cause it to update to an empty object or would it disregard it b/c it is empty?

Just wanted to follow up – this worked great. Thanks for the help you guys are awesome.