Using Remote JSON, Parquet, etc. Data Sources With DuckDB WASM


You want to use DuckDB WASM in-browser to work a diverse array of remote datasets as tables in a single database.


Use DuckDB’s httpfs abilities or Observable FileAttachments to wire up many external soruces into one DuckDB WASM databaase instance.


🔵 DuckDB loading…

If you’ve used Observable notebooks then you are almost certainly familiar with FileAttachments. They are way to embed the content of files with a notebook, and any CSV/JSON/Parquet/etc. FileAttachment can be used directly when creating a new DuckDBClient. If you are used to that syntax, it’s possible to keep using it with DuckDB in your own web projects via a small helper function:

import { Library, FileAttachments } from '';

const FileAttachment = FileAttachments((url) =>
  new URL(`${url}`)

// let's see what it creates
await FileAttachment("")
  "name": {
    "value": "",
    "writable": false,
    "enumerable": true,
    "configurable": false
  "_url": {
    "value": "",
    "writable": false,
    "enumerable": false,
    "configurable": false

We can now use FileAttachment to load up each table from the chinook example in the SQLite CLI chapter:

const dbFA = await DuckDBClient().of({
  album: await FileAttachment("").json(),
  artist: await FileAttachment(""),
  customer: await FileAttachment("").csv({typed: true}),
  employee: await FileAttachment("").json(),
  genre: await FileAttachment("").json(),
  invoice_items: await FileAttachment(""),
  invoice: await FileAttachment("").json(),
  mediatype: await FileAttachment("").json(),
  playlist: await FileAttachment("").json(),
  playlisttrack: await FileAttachment(""),
  tracks: await FileAttachment(""),

Note the mixing and matching of JSON, CSV, and Parquet files.

We’ll use the same query from that chapter and run it against the database we just loaded into our browser:

const query = `
  tracks t, 
  invoice_items i
  t.Name AS Track, 
  SUM(i.Quantity) AS "Total Sold"
  t.TrackId = i.TrackId
ORDER BY "Total Sold" DESC

await dbFA.sql`${query}`

It’s also possible to start with an empty in-memory DuckDB instance and use DuckDB’s httpfs to read the files:

const dbHTTP = await DuckDBClient().of({});
const tables = [ "album", "artist", "customer", "employee", "genre", 
                 "invoice_items", "invoice", "mediatype", "playlist", 
                 "playlisttrack", "tracks" ]

for (const tbl of tables) {
  await dbHTTP.query(
    `CREATE TABLE ${tbl} AS (FROM '${tbl}.parquet')`

I strongly suggest using the Parquet format for remote files unless they are small.