Skip to Content
OpenPreview

CSV, JSON and XML

Show CSV and TSV tables of up to a million rows, JSON as a collapsible tree and XML with highlighting, in React and in the browser.

CSV and TSV

Our own RFC 4180 parser handles quoted fields, embedded newlines and the delimiter (comma, semicolon, tab or pipe), which is detected from the file. Tables over 1,000 rows are windowed: rows are drawn near the view, column widths are fixed, the header stays put, and search covers every row. Up to a million rows open.

Live: people.csv rendered by the library in this page.Open in the playground
Live: 100,000 rows.Open in the playground

JSON

A collapsible tree of objects and arrays. Search finds keys and values.

Live: data.json rendered by the library in this page.Open in the playground

XML

Shown as highlighted code. SVG files are shown as images, and Office and OpenDocument files (which are ZIP files of XML) get their own renderers.

Controller

StateFor CSV, TSV, JSON and XML
page, pageCountNo pages: page is undefined and pageCount is 0.
zoom, scaleNumber zooms scale the table, tree or text; fit modes do not apply.
outline, thumbnailsNone.
search.countCSV: every cell of every row, including rows not drawn yet in large files. JSON: keys and values.
progressWorth showing for large files: a 5 MB CSV over a slow connection takes a while.
canzoom, search, print, download.
Live: useDocViewer on people.csv. Press a command and watch the state change.

Things to know:

  • The delimiter and header-row controls are drawn in the surface, above the table.
  • Files of 512 KB and up are parsed in a Web Worker; status is 'ready' while parsing finishes, and the surface shows progress.

Headless

A data viewer with a download progress bar, search over all rows, text size and download:

examples/headless/data.tsx
'use client';
// A CSV / TSV / JSON viewer: a loading bar while the file downloads, search with match
// navigation, zoom, and download. Tables have no pages, so pageCount is 0.
import { useState } from 'react';
import { DocumentSurface, formatBytes, useDocViewer, useDocumentSearch, type DocSource } from '@openpreview/react';
import '@openpreview/react/styles.css';

export default function DataViewer({ source }: { source: DocSource }) {
  const viewer = useDocViewer({ source });
  const [query, setQuery] = useState('');
  const results = useDocumentSearch(viewer, query);
  const { loaded, total } = viewer.progress;

  return (
    <div className="flex h-full flex-col text-sm">
      <header className="flex flex-wrap items-center gap-3 border-b p-2">
        <strong>{viewer.fileName}</strong>
        {viewer.detection && <span className="uppercase opacity-70">{viewer.detection.ext}</span>}
        {viewer.status === 'loading' && (
          <progress max={total ?? 1} value={total ? loaded : undefined}>
            {formatBytes(loaded)}
          </progress>
        )}
        <input className="rounded border px-2" placeholder="Search all rows" value={query} onChange={(e) => setQuery(e.target.value)} />
        {query && (
          <span>
            {results.current + 1} / {results.count} <button onClick={results.prev}>↑</button> <button onClick={results.next}>↓</button>
          </span>
        )}
        <span className="ml-auto">
          <button onClick={viewer.zoomOut}>A−</button> <button onClick={viewer.zoomIn}>A+</button>
        </span>
        <button onClick={viewer.download}>Download</button>
      </header>
      <DocumentSurface viewer={viewer} className="min-h-0 flex-1" />
    </div>
  );
}
Live: the example above, built only with the public hooks, running on large.csv.

On this page