(buffer: Buffer | string)
| 466 | * union of all object keys, sanitized via {@link sanitizeJsonHeaders}. |
| 467 | */ |
| 468 | export function parseJsonRows(buffer: Buffer | string): { |
| 469 | headers: string[] |
| 470 | rows: Record<string, unknown>[] |
| 471 | } { |
| 472 | const text = typeof buffer === 'string' ? buffer : buffer.toString('utf-8') |
| 473 | const parsed = JSON.parse(text) |
| 474 | if (!Array.isArray(parsed)) { |
| 475 | throw new Error('JSON file must contain an array of objects') |
| 476 | } |
| 477 | if (parsed.length === 0) { |
| 478 | throw new Error('JSON file contains an empty array') |
| 479 | } |
| 480 | const headerSet = new Set<string>() |
| 481 | for (const row of parsed) { |
| 482 | if (typeof row !== 'object' || row === null || Array.isArray(row)) { |
| 483 | throw new Error('Each element in the JSON array must be a plain object') |
| 484 | } |
| 485 | for (const key of Object.keys(row)) headerSet.add(key) |
| 486 | } |
| 487 | return sanitizeJsonHeaders([...headerSet], parsed) |
| 488 | } |
| 489 | |
| 490 | /** |
| 491 | * Parses a tabular upload (CSV, TSV, or JSON array-of-objects) into a uniform |
no test coverage detected