{
  "name": "ezGPT - Rename JSON Fields",
  "active": false,
  "nodes": [
    {
      "id": "trigger",
      "name": "Manual Trigger",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        200,
        300
      ],
      "parameters": {}
    },
    {
      "id": "sample-rows",
      "name": "Sample Rows",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        430,
        300
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const rows = [\n  {\"order_id\":\"001\",\"client_name\":\"Альфа\",\"sum_minor\":150000,\"active\":true,\"note\":null},\n  {\"order_id\":\"002\",\"client_name\":\"Бета\",\"sum_minor\":0,\"active\":false,\"note\":\"Самовывоз\"},\n  {\"order_id\":\"003\",\"client_name\":\"Гамма\",\"sum_minor\":49900,\"active\":true,\"note\":\"\"}\n];\nreturn rows.map(json => ({ json }));"
      }
    },
    {
      "id": "check-input",
      "name": "Check Input",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        660,
        300
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const items = $input.all();\nif (items.length < 1 || items.length > 20) throw new Error('row_count_invalid');\nconst ids = new Set();\nfor (const { json: row } of items) {\n  if (Object.keys(row).sort().join('|') !== 'active|client_name|note|order_id|sum_minor') {\n    throw new Error('input_fields_invalid');\n  }\n  if (typeof row.order_id !== 'string' || !/^\\d{3}$/.test(row.order_id) || ids.has(row.order_id)) {\n    throw new Error('id_invalid_or_duplicate');\n  }\n  if (typeof row.client_name !== 'string' || !row.client_name.trim() || row.client_name.length > 100) {\n    throw new Error('client_name_invalid');\n  }\n  if (!Number.isSafeInteger(row.sum_minor) || row.sum_minor < 0) throw new Error('sum_minor_invalid');\n  if (typeof row.active !== 'boolean') throw new Error('active_invalid');\n  if (row.note !== null && (typeof row.note !== 'string' || row.note.length > 200)) {\n    throw new Error('note_invalid');\n  }\n  ids.add(row.order_id);\n}\nreturn items.map(item => ({ json: item.json }));"
      }
    },
    {
      "id": "rename",
      "name": "Rename Fields",
      "type": "n8n-nodes-base.renameKeys",
      "typeVersion": 1,
      "position": [
        890,
        300
      ],
      "parameters": {
        "keys": {
          "key": [
            {
              "currentKey": "order_id",
              "newKey": "id"
            },
            {
              "currentKey": "client_name",
              "newKey": "customer"
            },
            {
              "currentKey": "sum_minor",
              "newKey": "amount_minor"
            },
            {
              "currentKey": "active",
              "newKey": "enabled"
            }
          ]
        },
        "additionalOptions": {}
      }
    },
    {
      "id": "check-output",
      "name": "Check Output",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1120,
        300
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const items = $input.all();\nconst source = $('Check Input').all();\nif (items.length !== source.length) throw new Error('output_count_mismatch');\nfor (let i = 0; i < items.length; i++) {\n  const row = items[i].json;\n  const old = source[i].json;\n  const expected = {\n    id: old.order_id, customer: old.client_name,\n    amount_minor: old.sum_minor, enabled: old.active, note: old.note\n  };\n  if (Object.keys(row).sort().join('|') !== 'amount_minor|customer|enabled|id|note') {\n    throw new Error('output_fields_mismatch');\n  }\n  if (Object.entries(expected).some(([key, value]) => row[key] !== value)) {\n    throw new Error('output_value_or_order_mismatch');\n  }\n}\nreturn items.map(item => ({ json: item.json }));"
      }
    }
  ],
  "connections": {
    "Manual Trigger": {
      "main": [
        [
          {
            "node": "Sample Rows",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Sample Rows": {
      "main": [
        [
          {
            "node": "Check Input",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Input": {
      "main": [
        [
          {
            "node": "Rename Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Rename Fields": {
      "main": [
        [
          {
            "node": "Check Output",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "saveDataSuccessExecution": "all",
    "saveDataErrorExecution": "all"
  },
  "pinData": {}
}
