{
  "name": "ezGPT - Import JSON Catalog",
  "active": false,
  "nodes": [
    {
      "id": "manual",
      "name": "Manual Trigger",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        260
      ],
      "parameters": {}
    },
    {
      "id": "download",
      "name": "Download JSON",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        220,
        260
      ],
      "parameters": {
        "method": "GET",
        "url": "https://ezgpt.ru/storage/article-files/catalog-demo.json",
        "authentication": "none",
        "options": {
          "response": {
            "response": {
              "fullResponse": true,
              "neverError": false,
              "responseFormat": "file",
              "outputPropertyName": "document"
            }
          },
          "redirect": {
            "redirect": {
              "followRedirects": false
            }
          },
          "timeout": 15000,
          "allowUnauthorizedCerts": false
        }
      }
    },
    {
      "id": "check-file",
      "name": "Check File",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        440,
        260
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const items = $input.all();\nif (items.length !== 1) throw new Error('one_file_required');\nconst item = items[0];\nconst mime = String(item.json.headers?.['content-type'] ?? '').split(';')[0].trim().toLowerCase();\nif (item.json.statusCode !== 200 || !['application/json', 'application/octet-stream'].includes(mime)) {\n  throw new Error('json_http_response_required');\n}\nif (Object.keys(item.binary ?? {}).join('|') !== 'document') throw new Error('document_required');\nconst bytes = await this.helpers.getBinaryDataBuffer(0, 'document');\nconst text = bytes.toString('utf8');\nif (bytes.length < 1 || bytes.length > 16384 || !Buffer.from(text, 'utf8').equals(bytes) || text.includes('\\u0000')) {\n  throw new Error('small_utf8_json_required');\n}\nreturn [{ json: { source_bytes: bytes.length }, binary: item.binary }];"
      }
    },
    {
      "id": "read",
      "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": "check-dataset",
      "name": "Check Dataset",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        880,
        260
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const items = $input.all();\nif (items.length !== 1) throw new Error('one_dataset_required');\nconst { rows, source_bytes } = items[0].json;\nif (!Number.isInteger(source_bytes) || source_bytes < 1 || !items[0].binary?.document) {\n  throw new Error('source_metadata_required');\n}\nif (!Array.isArray(rows) || rows.length < 1 || rows.length > 20) throw new Error('rows_array_required');\nconst ids = new Set();\nconst validText = (value, max) => typeof value === 'string' && value.isWellFormed() &&\n  value.length <= max && !/[\\u0000-\\u0008\\u000b\\u000c\\u000e-\\u001f\\u007f]/u.test(value);\nfor (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}\nreturn [{ json: { rows, source_bytes } }];"
      }
    },
    {
      "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 items = $input.all();\nconst expected = $('Check Dataset').first().json.rows;\nconst rows = items.map(item => item.json.row);\nif (JSON.stringify(rows) !== JSON.stringify(expected)) throw new Error('split_result_mismatch');\nreturn rows.map(row => ({ json: { ...row, verified: true, external_send: false } }));"
      }
    }
  ],
  "connections": {
    "Manual Trigger": {
      "main": [
        [
          {
            "node": "Download JSON",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download JSON": {
      "main": [
        [
          {
            "node": "Check File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check File": {
      "main": [
        [
          {
            "node": "Read JSON",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read JSON": {
      "main": [
        [
          {
            "node": "Check Dataset",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Dataset": {
      "main": [
        [
          {
            "node": "Split Rows",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Rows": {
      "main": [
        [
          {
            "node": "Check Result",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {}
}
