MCPcopy Index your code
hub / github.com/simstudioai/sim / parseJsonRows

Function parseJsonRows

apps/sim/lib/table/import.ts:468–488  ·  view source on GitHub ↗
(buffer: Buffer | string)

Source from the content-addressed store, hash-verified

466 * union of all object keys, sanitized via {@link sanitizeJsonHeaders}.
467 */
468export 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

Callers 1

parseFileRowsFunction · 0.85

Calls 4

sanitizeJsonHeadersFunction · 0.85
parseMethod · 0.80
toStringMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected