{
  "name": "ezGPT - Merge Local JSON Catalogs",
  "active": false,
  "nodes": [
    {
      "id": "manual",
      "name": "Manual Trigger",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        260
      ],
      "parameters": {}
    },
    {
      "id": "read",
      "name": "Read Files",
      "type": "n8n-nodes-base.readWriteFile",
      "typeVersion": 1,
      "position": [
        220,
        260
      ],
      "alwaysOutputData": true,
      "parameters": {
        "operation": "read",
        "fileSelector": "/files/inbox/catalog-*.json",
        "options": {
          "dataPropertyName": "document"
        }
      }
    },
    {
      "id": "check-files",
      "name": "Check Files",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        440,
        260
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const items = $input.all();\nconst expected = ['catalog-a.json', 'catalog-b.json'];\nif (items.length !== expected.length) throw new Error('two_catalog_files_required');\nconst names = new Set(), checked = [];\nfor (let index = 0; index < items.length; index++) {\n  const item = items[index];\n  if (Object.keys(item.binary ?? {}).join('|') !== 'document') throw new Error('document_required');\n  const name = item.binary.document.fileName;\n  if (!expected.includes(name) || names.has(name)) throw new Error('catalog_names_invalid');\n  const bytes = await this.helpers.getBinaryDataBuffer(index, 'document');\n  const text = bytes.toString('utf8');\n  if (bytes.length < 1 || bytes.length > 16384 || !Buffer.from(text, 'utf8').equals(bytes) || text.includes('\\u0000')) {\n    throw new Error('small_utf8_json_required');\n  }\n  names.add(name);\n  checked.push({ json: { source_file: name, source_bytes: bytes.length },\n    binary: item.binary, pairedItem: { item: index } });\n}\nreturn checked.sort((a, b) => a.json.source_file.localeCompare(b.json.source_file, 'en'));"
      }
    },
    {
      "id": "parse",
      "name": "Read JSON",
      "type": "n8n-nodes-base.extractFromFile",
      "typeVersion": 1.1,
      "position": [
        660,
        260
      ],
      "parameters": {
        "operation": "fromJson",
        "binaryPropertyName": "document",
        "destinationKey": "rows",
        "options": {
          "encoding": "utf8",
          "stripBOM": true,
          "keepSource": "both"
        }
      }
    },
    {
      "id": "merge-catalogs",
      "name": "Merge Catalogs",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        880,
        260
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const items = $input.all();\nconst expected = ['catalog-a.json', 'catalog-b.json'];\nif (items.length !== expected.length) throw new Error('two_catalogs_required');\nconst ordered = [...items].sort((a, b) => String(a.json.source_file).localeCompare(String(b.json.source_file), 'en'));\nconst ids = new Set(), merged = [];\nconst validText = (value, max) => typeof value === 'string' && value.isWellFormed() &&\n  value.length <= max && !/[\\u0000-\\u0008\\u000b\\u000c\\u000e-\\u001f\\u007f]/u.test(value);\nfor (let index = 0; index < ordered.length; index++) {\n  const item = ordered[index], { rows, source_file, source_bytes } = item.json;\n  if (source_file !== expected[index] || !Number.isInteger(source_bytes) || source_bytes < 1 || !item.binary?.document) {\n    throw new Error('source_metadata_invalid');\n  }\n  if (!Array.isArray(rows) || rows.length < 1 || rows.length > 20) throw new Error('rows_array_required');\n  for (const row of rows) {\n    if (row === null || typeof row !== 'object' || Array.isArray(row) ||\n        Object.keys(row).sort().join('|') !== 'active|id|note|quantity|tags|title') {\n      throw new Error('row_fields_invalid');\n    }\n    if (typeof row.id !== 'string' || !/^\\d{3}$/.test(row.id) || ids.has(row.id)) {\n      throw new Error('id_invalid_or_duplicate');\n    }\n    if (!validText(row.title, 100) || !row.title.trim()) throw new Error('title_invalid');\n    if (!Number.isSafeInteger(row.quantity) || row.quantity < 0) throw new Error('quantity_invalid');\n    if (typeof row.active !== 'boolean') throw new Error('active_invalid');\n    if (row.note !== null && !validText(row.note, 200)) throw new Error('note_invalid');\n    if (!Array.isArray(row.tags) || row.tags.length > 3 ||\n        row.tags.some(tag => !validText(tag, 30) || !tag.trim())) throw new Error('tags_invalid');\n    ids.add(row.id);\n    merged.push({ ...row, source_file });\n  }\n}\nreturn [{ json: { rows: merged, source_files: expected.length } }];"
      }
    },
    {
      "id": "split",
      "name": "Split Rows",
      "type": "n8n-nodes-base.splitOut",
      "typeVersion": 1,
      "position": [
        1100,
        260
      ],
      "alwaysOutputData": true,
      "parameters": {
        "fieldToSplitOut": "rows",
        "include": "noOtherFields",
        "options": {
          "destinationFieldName": "row",
          "includeBinary": false
        }
      }
    },
    {
      "id": "check-result",
      "name": "Check Result",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1320,
        260
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const rows = $input.all().map(item => item.json.row);\nconst expected = $('Merge Catalogs').first().json.rows;\nif (JSON.stringify(rows) !== JSON.stringify(expected)) throw new Error('merged_result_mismatch');\nreturn rows.map(row => ({ json: { ...row, verified: true, external_send: false } }));"
      }
    }
  ],
  "connections": {
    "Manual Trigger": {
      "main": [
        [
          {
            "node": "Read Files",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read Files": {
      "main": [
        [
          {
            "node": "Check Files",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Files": {
      "main": [
        [
          {
            "node": "Read JSON",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read JSON": {
      "main": [
        [
          {
            "node": "Merge Catalogs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge Catalogs": {
      "main": [
        [
          {
            "node": "Split Rows",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Rows": {
      "main": [
        [
          {
            "node": "Check Result",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {}
}
