Rename, sort and delte JSON objects

Hello dear community,

I am currently working on a workflow that will later be used to migrate data from one system to another via a CSV file. (Currently the software is not integrated)

In the first step “all” data from a system (e.g. hubspot) has to be exported (JSON). In the next step the data has to be partially renamed, not needed objects and lines deleted and re-sorted In the last step, the CSV export takes place and is sent, e.g. via Slack.

What currently works:

  • get all hubspot data (get all)
  • Renaming individual objects on the first level

What doesn’t work:

(1) the sorting
(2) renaming objects from the second level onwards
(3) the deletion of unnecessary!

Bildschirmfoto 2020-08-05 um 10.07.36|690x473 objects (unused lines)

What might be a possible solution to the problem described above?

{
  "nodes": [
    {
      "parameters": {
        "operation": "toFile",
        "fileFormat": "csv",
        "options": {
          "fileName": ""
        }
      },
      "name": "Spreadsheet File1",
      "type": "n8n-nodes-base.spreadsheetFile",
      "typeVersion": 1,
      "position": [
        1000,
        300
      ]
    },
    {
      "parameters": {
        "keys": {
          "key": [
            {
              "currentKey": "=properties",
              "newKey": "ExampleChange"
            }
          ]
        }
      },
      "name": "Rename Keys",
      "type": "n8n-nodes-base.renameKeys",
      "typeVersion": 1,
      "position": [
        800,
        300
      ]
    },
    {
      "parameters": {
        "resource": "contact",
        "operation": "getAll",
        "additionalFields": {}
      },
      "name": "Hubspot get all",
      "type": "n8n-nodes-base.hubspot",
      "typeVersion": 1,
      "position": [
        400,
        300
      ],
      "credentials": {
        "hubspotApi": "Hubspot Sandbox //HD"
      }
    },
    {
      "parameters": {
        "functionCode": "{delete form-submissions;}\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        600,
        300
      ]
    },
    {
      "parameters": {
        "channel": "n8ntestchat",
        "attachments": [
          {
            "fallback": "="
          }
        ],
        "otherOptions": {}
      },
      "name": "Slack",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 1,
      "position": [
        1200,
        300
      ],
      "credentials": {
        "slackApi": "billwerk"
      }
    }
  ],
  "connections": {
    "Spreadsheet File1": {
      "main": [
        [
          {
            "node": "Slack",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Rename Keys": {
      "main": [
        [
          {
            "node": "Spreadsheet File1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Hubspot get all": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "Rename Keys",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

It looks like you should have another look at the documentation here about how the Function-Node works exactly. That will make it easier for you in the future:
https://docs.n8n.io/nodes/n8n-nodes-base.function/

Here an example how to do what you have planned. It is not complete I did just add the first few properties. So what you would have to do is to keep on adding only the properties you want and that in the format you want them:

{
  "nodes": [
    {
      "parameters": {
        "resource": "contact",
        "operation": "getAll",
        "limit": 10,
        "additionalFields": {}
      },
      "name": "Hubspot get all",
      "type": "n8n-nodes-base.hubspot",
      "typeVersion": 1,
      "position": [
        220,
        550
      ],
      "credentials": {
        "hubspotApi": "ricardo"
      }
    },
    {
      "parameters": {
        "functionCode": "const newItems = [];\n\nlet newItem, item;\n\nitems.forEach(item => {\n  item = item.json;\n  \n  newItem = {};\n  newItem.addedAt = item.addedAt;\n  newItem['portal-id'] = item['portal-id'];\n  newItem['canonical-vid'] = item['canonical-vid'];\n  \n  // TODO: Keep on going from here by adding everything as you want it\n    \n  newItems.push({ json: newItem });\n});\n\nreturn newItems;"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        550
      ]
    }
  ],
  "connections": {
    "Hubspot get all": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like