{
  "id": "zGGBdrGLXA50cO1A",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "From Unstructured Data To Structured Data",
  "tags": [],
  "nodes": [
    {
      "id": "97cce9f4-cb8e-4ea7-872b-e1f8054afc3a",
      "name": "Gmail Trigger",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        -480,
        -224
      ],
      "parameters": {
        "filters": {},
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.3
    },
    {
      "id": "13fffb5b-724c-4adf-8c99-5e16f10a5324",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        -112,
        160
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4.1-mini"
        },
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "90d9983b-b4a6-427f-970c-388bae72fb1f",
      "name": "Structured Output Parser",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "position": [
        128,
        160
      ],
      "parameters": {
        "autoFix": true,
        "schemaType": "manual",
        "inputSchema": "{\n\t\"type\": \"object\",\n\t\"properties\": {\n\t\t\"from\": {\n\t\t\t\"type\": \"string\"\n\t\t},\n\t\t\"to\": {\n\t\t\t\"type\": \"string\"\n\t\t},\n        \"subject\": {\n\t\t\t\"type\": \"string\"\n\t\t},\n        \"summarize\": {\n\t\t\t\"type\": \"string\"\n\t\t}\n\t}\n}"
      },
      "typeVersion": 1.3
    },
    {
      "id": "1165ea37-01f5-4835-b04d-c91395519e04",
      "name": "OpenAI Chat Model1",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        80,
        336
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4.1-mini"
        },
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "481c0914-84c3-4c5f-9247-ec709bf18e53",
      "name": "Insert row",
      "type": "n8n-nodes-base.dataTable",
      "position": [
        288,
        -64
      ],
      "parameters": {
        "columns": {
          "value": {
            "To": "={{ $json.output.to }}",
            "From": "={{ $json.output.from }}",
            "Subject": "={{ $json.output.subject }}",
            "Summary": "={{ $json.output.summarize }}"
          },
          "schema": [
            {
              "id": "From",
              "type": "string",
              "display": true,
              "removed": false,
              "readOnly": false,
              "required": false,
              "displayName": "From",
              "defaultMatch": false
            },
            {
              "id": "Subject",
              "type": "string",
              "display": true,
              "removed": false,
              "readOnly": false,
              "required": false,
              "displayName": "Subject",
              "defaultMatch": false
            },
            {
              "id": "Summary",
              "type": "string",
              "display": true,
              "removed": false,
              "readOnly": false,
              "required": false,
              "displayName": "Summary",
              "defaultMatch": false
            },
            {
              "id": "To",
              "type": "string",
              "display": true,
              "removed": false,
              "readOnly": false,
              "required": false,
              "displayName": "To",
              "defaultMatch": false
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "dataTableId": {
          "__rl": true,
          "mode": "list",
          "value": "LZKeHWCZU60XbKyM",
          "cachedResultUrl": "/projects/mfFxBtuhQcXKlWHw/datatables/LZKeHWCZU60XbKyM",
          "cachedResultName": "Email output parser"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "ca682a7e-9630-4c8b-b8eb-17f557354eb8",
      "name": "Email Trigger (IMAP)",
      "type": "n8n-nodes-base.emailReadImap",
      "position": [
        -480,
        -64
      ],
      "parameters": {
        "options": {}
      },
      "credentials": {
        "imap": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "15c2dbd5-6e23-4920-843a-7ce7a43fdcf5",
      "name": "Microsoft Outlook Trigger",
      "type": "n8n-nodes-base.microsoftOutlookTrigger",
      "position": [
        -480,
        80
      ],
      "parameters": {
        "filters": {},
        "options": {},
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "credentials": {
        "microsoftOutlookOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "04dc1bd7-6ecb-457f-835b-e67b910e30ad",
      "name": "Parsing Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        -64,
        -64
      ],
      "parameters": {
        "text": "={{JSON.stringify($json)}}",
        "options": {
          "systemMessage": "=**Role:**\nYou are a data extraction and parsing agent integrated in an n8n workflow.\n\n**Goal:**\nGiven a JSON input containing one or more emails , your task is to parse and extract structured information from the email content.\n\n**Instructions:**\n\n1. Carefully read the email content provided in the `body` field of the JSON input.\n2. Identify and extract key entities and data points mentioned in the text \n3. Return the extracted data in a **clean, structured JSON format**.\n4. If specific data cannot be found, return the field as `null` instead of omitting it.\n5. Preserve the original structure of the input email list, so that each parsed result corresponds to the same email in the input array.\n6. Do not include any explanations, reasoning, or text outside of JSON.\n\n**Important:**\n\n* Be consistent with field names and data formats (use ISO 8601 for dates, plain strings for text).\n* Never return unstructured text or commentary \u2014 only valid JSON.\n* Output should always be a JSON array of parsed objects."
        },
        "promptType": "define",
        "hasOutputParser": true
      },
      "typeVersion": 2.2
    },
    {
      "id": "b0d03646-57f1-4eb0-8f1c-3c27c3900466",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -512,
        -928
      ],
      "parameters": {
        "color": 3,
        "width": 976,
        "height": 400,
        "content": "## How to Transform Unstructured Email Data into Structured Format Using an AI Agent\n\nThis workflow leverages artificial intelligence to automatically transform **unstructured email data** from multiple sources \u2014 including **Gmail**, **Outlook**, and **IMAP** \u2014 into **structured** and summarized information.\n\nThe workflow retrieves incoming emails and sends their content to an **AI agent** specifically designed to parse and interpret unstructured text. The agent extracts key metadata and insights from each message, including:\n\n* **From** \u2013 the sender\u2019s email address\n* **To** \u2013 the recipient\u2019s email address\n* **Subject** \u2013 the subject line of the email\n* **Summarize** \u2013 a concise summary of the email body generated by the AI\n\nOnce processed, the structured output is stored in a **database table**. This allows for easy indexing, reporting, and integration with other business systems.\n\nBy automating this process, the flow ensures consistent, searchable, and easily digestible information from diverse email sources significantly reducing manual review and improving workflow efficiency."
      },
      "typeVersion": 1
    },
    {
      "id": "17751b90-4db6-45d7-b426-db157e1199db",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -512,
        -512
      ],
      "parameters": {
        "width": 480,
        "height": 176,
        "content": "## STEP 1\nCreata a n8n table with the following fields:\n- From\n- To\n- Subject\n- Summary"
      },
      "typeVersion": 1
    },
    {
      "id": "fc864e0b-f04e-4996-a012-fbe7ad52dc6b",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -16,
        -512
      ],
      "parameters": {
        "width": 480,
        "height": 176,
        "content": "## STEP 2\nConnect triggers from different sources:\n- Gmail\n- Outlook\n- Imap"
      },
      "typeVersion": 1
    },
    {
      "id": "ebabb739-ecb9-450b-abb5-1d27d6a95832",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        112,
        96
      ],
      "parameters": {
        "width": 272,
        "height": 176,
        "content": "Set the structured data from json"
      },
      "typeVersion": 1
    },
    {
      "id": "84b10b19-a1a2-4fac-b791-8ef5aa1160c6",
      "name": "When chat message received",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "position": [
        576,
        -64
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 1.3
    },
    {
      "id": "e846ffd6-7ee5-4486-9f6d-a73f349814b9",
      "name": "Email Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        880,
        -64
      ],
      "parameters": {
        "options": {
          "systemMessage": "You are a helpful assistant.\n\nUse always the \"Emails\" tool to search for the request."
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "c833e8c1-68d5-421c-9f71-0d928b14ad19",
      "name": "Emails",
      "type": "n8n-nodes-base.dataTableTool",
      "position": [
        1056,
        160
      ],
      "parameters": {
        "filters": {
          "conditions": [
            {
              "keyName": "From",
              "keyValue": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('conditions0_Value', `Search the emails sent from`, 'string') }}",
              "condition": "ilike"
            },
            {
              "keyName": "Subject",
              "keyValue": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('conditions1_Value', `Search the emails with these subject`, 'string') }}",
              "condition": "ilike"
            },
            {
              "keyName": "To",
              "keyValue": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('conditions2_Value', `Search the emails sent to`, 'string') }}",
              "condition": "ilike"
            },
            {
              "keyName": "Summary",
              "keyValue": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('conditions3_Value', `Search the emails with these summaries`, 'string') }}",
              "condition": "ilike"
            }
          ]
        },
        "operation": "get",
        "returnAll": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Return_All', ``, 'boolean') }}",
        "dataTableId": {
          "__rl": true,
          "mode": "list",
          "value": "LZKeHWCZU60XbKyM",
          "cachedResultUrl": "/projects/mfFxBtuhQcXKlWHw/datatables/LZKeHWCZU60XbKyM",
          "cachedResultName": "Email output parser"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "16feafab-fa9e-4acb-85b5-cd04e6b621d0",
      "name": "Google Gemini Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        800,
        144
      ],
      "parameters": {
        "options": {}
      },
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "7527b0d6-cf52-4bd1-81d5-ad39002dda1f",
  "connections": {
    "Emails": {
      "ai_tool": [
        [
          {
            "node": "Email Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Gmail Trigger": {
      "main": [
        [
          {
            "node": "Parsing Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parsing Agent": {
      "main": [
        [
          {
            "node": "Insert row",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Parsing Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model1": {
      "ai_languageModel": [
        [
          {
            "node": "Structured Output Parser",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Email Trigger (IMAP)": {
      "main": [
        [
          {
            "node": "Parsing Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Email Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Structured Output Parser": {
      "ai_outputParser": [
        [
          {
            "node": "Parsing Agent",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "Microsoft Outlook Trigger": {
      "main": [
        [
          {
            "node": "Parsing Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When chat message received": {
      "main": [
        [
          {
            "node": "Email Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}