Registry / serialization / rdf-parser-csvw

rdf-parser-csvw

JSON →
library1.1.0jsnpmunverified

rdf-parser-csvw is a JavaScript library designed to parse CSV (Comma Separated Values) data according to the CSV on the Web (CSVW) W3C recommendation, converting it into RDF/JS Quads. It leverages the RDF/JS Stream interface, allowing for efficient, asynchronous processing of large CSV files by consuming a stream of strings and emitting a stream of parsed RDF quads. The library's current stable version is 1.1.0, with releases typically following a feature-driven cadence rather than strict timeboxes. A key differentiator is its strict adherence to the RDF/JS specification for data factories and stream interfaces, ensuring broad compatibility within the RDF/JS ecosystem. It requires explicit CSVW metadata (as an RDF/JS Dataset) and a base IRI for proper conversion. Options include specifying a custom RDF/JS data factory, an alternative timezone for date/time parsing, and error-handling preferences such as `relaxColumnCount` to ignore column count mismatches or `skipLinesWithError` for debugging noisy datasets, though the latter is advised against for production use.

npm install rdf-parser-csvw
INSTALL
IMPORT
SIG · RDF-PARSER-CSVW
R
rdf-parser-csvw
serializationjavascriptv1.1.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Parser
import { Parser } from 'rdf-parser-csvw'
import Parser from 'rdf-parser-csvw'
The parser is exported as a named class `Parser` in ESM. Do not use default import.
Parser (CommonJS)
const { Parser } = require('rdf-parser-csvw')
const Parser = require('rdf-parser-csvw')
For CommonJS, access the `Parser` class as a named property from the module export.
Parser (Type Import)
import type { Parser } from 'rdf-parser-csvw'
When only importing the type for TypeScript, use `import type` for clarity and bundler optimization.

Demonstrates how to instantiate the `Parser` class, construct a minimal CSVW metadata RDF/JS Dataset, and parse a CSV string into RDF quads using streams.

import { Readable } from 'stream'; import { Parser } from 'rdf-parser-csvw'; import rdf from 'rdf-ext'; // A common RDF/JS implementation for DataFactory and Dataset async function parseCsvw() { const csvString = `Name,Age\nAlice,30\nBob,25\nCharlie,35`; const baseIRI = 'http://example.org/data/'; // Construct a minimal CSVW metadata Dataset using rdf-ext const metadataDataset = rdf.dataset(); const ex = rdf.namedNode(baseIRI); const csvw = rdf.namedNode('http://www.w3.org/ns/csvw#'); const rdfType = rdf.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'); const tableGroup = rdf.blankNode(); const table = rdf.blankNode(); const column1 = rdf.blankNode(); const column2 = rdf.blankNode(); metadataDataset.add(rdf.quad(ex.file, rdfType, csvw.TableGroup)); metadataDataset.add(rdf.quad(ex.file, csvw.table, table)); metadataDataset.add(rdf.quad(table, rdfType, csvw.Table)); metadataDataset.add(rdf.quad(table, csvw.url, rdf.namedNode(`${baseIRI}data.csv`))); // Define columns based on CSV headers metadataDataset.add(rdf.quad(table, csvw.column, column1)); metadataDataset.add(rdf.quad(column1, csvw.name, rdf.literal('Name'))); metadataDataset.add(rdf.quad(column1, csvw.datatype, csvw.string)); metadataDataset.add(rdf.quad(table, csvw.column, column2)); metadataDataset.add(rdf.quad(column2, csvw.name, rdf.literal('Age'))); metadataDataset.add(rdf.quad(column2, csvw.datatype, csvw.integer)); // Instantiate the parser with required options const parser = new Parser({ metadata: metadataDataset, baseIRI: baseIRI, factory: rdf // Use rdf-ext's data factory }); // Create a readable stream from the CSV string const csvStream = Readable.from([csvString]); console.log('Starting CSVW parsing...'); // Import the CSV stream and get a stream of RDF quads const quadStream = parser.import(csvStream); // Consume and log the parsed quads for await (const quad of quadStream) { console.log(quad.toString()); } console.log('Finished parsing.'); } parseCsvw().catch(console.error);
Debug
Known issues
gotchaThe `metadata` option is strictly required in the parser constructor or `.import` method. It must be an RDF/JS Dataset representing the CSV on the Web metadata for your CSV file.
fix
Ensure the `metadata` option is always provided with a valid RDF/JS Dataset containing CSVW definitions.
affects: >=1.0.0
gotchaThe `baseIRI` option is strictly required to create Named Nodes from relative IRIs within the CSV data.
fix
Provide a valid string `baseIRI` in the parser constructor options or the `.import` method options.
affects: >=1.0.0
gotchaThe `skipLinesWithError` option is primarily for debugging purposes and should not be used in production environments, as it can lead to silent data loss or inconsistent graph generation from malformed CSV lines.
fix
Avoid `skipLinesWithError` in production. Instead, pre-process CSV data to ensure correctness or handle errors robustly.
affects: >=1.0.0
gotchaBy default, the parser will throw an error if a row's column count does not match the headers. Use `relaxColumnCount` if you expect and want to tolerate such discrepancies.
fix
Set `relaxColumnCount: true` in the parser options if column count mismatches should not halt parsing.
affects: >=1.0.0
Errors
Common errors & fixes
Error: The metadata option is required
The `metadata` option was not provided during parser instantiation or the `.import` call.
fix
Pass an RDF/JS `Dataset` object containing CSVW metadata to the `metadata` option.
Error: The baseIRI option is required
The `baseIRI` option was not provided, which is essential for resolving relative IRIs.
fix
Provide a string value for `baseIRI` in the parser options, e.g., `new Parser({ baseIRI: 'http://example.org/' })`.
Error: Column count mismatch in row X
A row in the CSV stream has a different number of columns than expected, and `relaxColumnCount` is not enabled.
fix
Either fix the malformed CSV data, or set `relaxColumnCount: true` in the parser options to ignore these errors.
TypeError: parser.import is not a function
The `Parser` class was not correctly instantiated, or `import` was called on the class itself instead of an instance.
fix
Ensure you create an instance of the `Parser` class using `new Parser(options)` before calling `.import()`.
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies
@rdfjs/data-modeloptionalDefault RDF/JS data factory, can be overridden.
@rdfjs/datasetrequiredRequired for creating the `metadata` Dataset, or `rdf-ext` as a common alternative.
rdf-extoptionalCommonly used for RDF/JS data factory and dataset implementation in the ecosystem, often used alongside this parser. Can substitute for @rdfjs/data-model and @rdfjs/dataset.
Agent activity
6 hits · last 30 days
node
6
Resources
rdf-parser-csvw — npm install rdf-parser-csvw · libregistry