# rdf-parse

> Parses RDF from any serialization

Latest version **5.0.0** (published 2026-01-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install rdf-parse
pnpm add rdf-parse
yarn add rdf-parse
bun add rdf-parse
```

## Health

**Score 60/100 (C)** — status: stable.

Positive: has types; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 5.0.0 |
| Published | 2026-01-09 |
| First published | 2019-04-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 26 |
| Unpacked size | 55.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 68 |
| Author | Ruben Taelman |
| Maintainers | rubensworks |
| Keywords | rdf, parse, stream, rdfjs, linked data |

## Links

- npm: https://www.npmjs.com/package/rdf-parse
- Repository: https://github.com/rubensworks/rdf-parse.js
- Homepage: https://github.com/rubensworks/rdf-parse.js#readme
- Issues: https://github.com/rubensworks/rdf-parse.js/issues
- Funding: https://github.com/sponsors/rubensworks/
- npm.io page: https://npm.io/package/rdf-parse

## Dependencies (26)

- [@rdfjs/types](https://npm.io/package/@rdfjs/types.md) *
- [@comunica/core](https://npm.io/package/@comunica/core.md) ^5.0.0
- [readable-stream](https://npm.io/package/readable-stream.md) ^4.7.0
- [rdf-data-factory](https://npm.io/package/rdf-data-factory.md) ^2.0.2
- [stream-to-string](https://npm.io/package/stream-to-string.md) ^1.2.1
- [@comunica/bus-http](https://npm.io/package/@comunica/bus-http.md) ^5.0.0
- [@comunica/bus-init](https://npm.io/package/@comunica/bus-init.md) ^5.0.0
- [@comunica/bus-rdf-parse](https://npm.io/package/@comunica/bus-rdf-parse.md) ^5.0.0
- [@comunica/mediator-race](https://npm.io/package/@comunica/mediator-race.md) ^5.0.0
- [@comunica/context-entries](https://npm.io/package/@comunica/context-entries.md) ^5.0.0
- [@comunica/mediator-number](https://npm.io/package/@comunica/mediator-number.md) ^5.0.0
- [@comunica/actor-http-fetch](https://npm.io/package/@comunica/actor-http-fetch.md) ^5.0.0
- [@comunica/actor-http-proxy](https://npm.io/package/@comunica/actor-http-proxy.md) ^5.0.0
- [@comunica/actor-rdf-parse-n3](https://npm.io/package/@comunica/actor-rdf-parse-n3.md) ^5.0.0
- [@comunica/bus-rdf-parse-html](https://npm.io/package/@comunica/bus-rdf-parse-html.md) ^5.0.0
- [@comunica/config-query-sparql](https://npm.io/package/@comunica/config-query-sparql.md) ^5.0.0
- [@comunica/actor-rdf-parse-html](https://npm.io/package/@comunica/actor-rdf-parse-html.md) ^5.0.0
- [@comunica/actor-rdf-parse-jsonld](https://npm.io/package/@comunica/actor-rdf-parse-jsonld.md) ^5.0.0
- [@comunica/actor-rdf-parse-rdfxml](https://npm.io/package/@comunica/actor-rdf-parse-rdfxml.md) ^5.0.0
- [@comunica/actor-rdf-parse-shaclc](https://npm.io/package/@comunica/actor-rdf-parse-shaclc.md) ^5.0.0
- [@comunica/mediator-combine-union](https://npm.io/package/@comunica/mediator-combine-union.md) ^5.0.0
- [@comunica/actor-rdf-parse-xml-rdfa](https://npm.io/package/@comunica/actor-rdf-parse-xml-rdfa.md) ^5.0.0
- [@comunica/actor-rdf-parse-html-rdfa](https://npm.io/package/@comunica/actor-rdf-parse-html-rdfa.md) ^5.0.0
- [@comunica/mediator-combine-pipeline](https://npm.io/package/@comunica/mediator-combine-pipeline.md) ^5.0.0
- [@comunica/actor-rdf-parse-html-script](https://npm.io/package/@comunica/actor-rdf-parse-html-script.md) ^5.0.0
- [@comunica/actor-rdf-parse-html-microdata](https://npm.io/package/@comunica/actor-rdf-parse-html-microdata.md) ^5.0.0

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 5.0.0 (latest) — 2026-01-09
- 4.0.0 — 2024-10-15
- 3.0.0 — 2024-07-04
- 2.3.3 — 2024-02-06
- 2.3.2 — 2023-03-30
- 2.3.1 — 2023-02-27
- 2.3.0 — 2023-02-01
- 2.2.0 — 2023-01-31
- 2.1.1 — 2022-11-09
- 2.1.0 — 2022-07-08
- 2.0.0 — 2022-03-02
- 1.9.1 — 2021-08-30
- 1.9.0 — 2021-08-30
- 1.8.1 — 2021-06-14
- 1.8.0 — 2021-04-27
- … 14 more at https://npm.io/package/rdf-parse/versions

## README

# RDF Parse

[![Build status](https://github.com/rubensworks/rdf-parse.js/workflows/CI/badge.svg)](https://github.com/rubensworks/rdf-parse.js/actions?query=workflow%3ACI)
[![Coverage Status](https://coveralls.io/repos/github/rubensworks/rdf-parse.js/badge.svg?branch=master)](https://coveralls.io/github/rubensworks/rdf-parse.js?branch=master)
[![npm version](https://badge.fury.io/js/rdf-parse.svg)](https://www.npmjs.com/package/rdf-parse)

This library parses _RDF streams_ based on _content type_ (or file name)
and outputs [RDF/JS](http://rdf.js.org/)-compliant quads as a stream.

This is useful in situations where you have _RDF in some serialization_,
and you just need the _parsed triples/quads_,
without having to concern yourself with picking the correct parser.

The following RDF serializations are supported:

| **Name**                                                                                                                                                                                 | **Content type** | **Extensions** |
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| ---------------- | ------------- |
| [TriG 1.2](https://www.w3.org/TR/rdf12-trig/)                                                                                                                                            | `application/trig` | `.trig` |
| [N-Quads 1.2](https://www.w3.org/TR/rdf12-n-quads/)                                                                                                                                            | `application/n-quads` | `.nq`, `.nquads` |
| [Turtle 1.2](https://www.w3.org/TR/rdf12-turtle/)                                                                                                                                              | `text/turtle` | `.ttl`, `.turtle` |
| [N-Triples 1.2](https://www.w3.org/TR/rdf12-n-triples/)                                                                                                                                        | `application/n-triples` | `.nt`, `.ntriples` |
| [Notation3](https://www.w3.org/TeamSubmission/n3/)                                                                                                                                       | `text/n3` | `.n3` |
| [JSON-LD 1.1](https://json-ld.org/)                                                                                                                                                      | `application/ld+json`, `application/json` | `.json`, `.jsonld` |
| [RDF/XML 1.2](https://www.w3.org/TR/rdf12-xml/)                                                                                                                                 | `application/rdf+xml` | `.rdf`, `.rdfxml`, `.owl` |
| [RDFa 1.1](https://www.w3.org/TR/rdfa-in-html/) and script RDF data tags [HTML](https://html.spec.whatwg.org/multipage/)/[XHTML](https://www.w3.org/TR/xhtml-rdfa/)                      | `text/html`, `application/xhtml+xml` | `.html`, `.htm`, `.xhtml`, `.xht` |
| [Microdata](https://w3c.github.io/microdata-rdf/)                                                                                                                                        | `text/html`, `application/xhtml+xml` | `.html`, `.htm`, `.xhtml`, `.xht` |
| [RDFa 1.1](https://www.w3.org/TR/2008/REC-SVGTiny12-20081222/metadata.html#MetadataAttributes) in [SVG](https://www.w3.org/TR/SVGTiny12/)/[XML](https://html.spec.whatwg.org/multipage/) | `image/svg+xml`,`application/xml` | `.xml`, `.svg`, `.svgz` |
| [SHACL Compact Syntax](https://w3c.github.io/shacl/shacl-compact-syntax/)                                                                                                                | `text/shaclc` | `.shaclc`, `.shc` |
| [Extended SHACL Compact Syntax](https://github.com/jeswr/shaclcjs#extended-shacl-compact-syntax)                                                                                         | `text/shaclc-ext` | `.shaclce`, `.shce` |

Internally, this library makes use of RDF parsers from the [Comunica framework](https://github.com/comunica/comunica),
which enable streaming processing of RDF.

Internally, the following fully spec-compliant parsers are used:

* [N3.js](https://github.com/rdfjs/n3.js)
* [jsonld-streaming-parser.js](https://github.com/rubensworks/jsonld-streaming-parser.js)
* [microdata-rdf-streaming-parser.js](https://github.com/rubensworks/microdata-rdf-streaming-parser.js)
* [rdfa-streaming-parser.js](https://github.com/rubensworks/rdfa-streaming-parser.js)
* [rdfxml-streaming-parser.js](https://github.com/rdfjs/rdfxml-streaming-parser.js)
* [shaclcjs](https://github.com/jeswr/shaclcjs)

## Installation

```bash
$ npm install rdf-parse
```

or

```bash
$ yarn add rdf-parse
```

This package also works out-of-the-box in browsers via tools such as [webpack](https://webpack.js.org/) and [browserify](http://browserify.org/).

## Require

```typescript
import { rdfParser } from "rdf-parse";
```

_or_

```javascript
const { rdfParser } = require("rdf-parse");
```

## Usage

### Parsing by content type

The `rdfParser.parse` method takes in a text stream containing RDF in any serialization,
and an options object, and outputs an [RDFJS stream](http://rdf.js.org/stream-spec/#stream-interface) that emits RDF quads.

```javascript
const textStream = require('streamify-string')(`
<http://ex.org/s> <http://ex.org/p> <http://ex.org/o1>, <http://ex.org/o2>.
`);

rdfParser.parse(textStream, { contentType: 'text/turtle', baseIRI: 'http://example.org' })
    .on('data', (quad) => console.log(quad))
    .on('error', (error) => console.error(error))
    .on('end', () => console.log('All done!'));
```

### Parsing by file name

Sometimes, the content type of an RDF document may be unknown,
for those cases, this library allows you to provide the path/URL of the RDF document,
using which the extension will be determined.

For example, Turtle documents can be detected using the `.ttl` extension.

```javascript
const textStream = require('streamify-string')(`
<http://ex.org/s> <http://ex.org/p> <http://ex.org/o1>, <http://ex.org/o2>.
`);

rdfParser.parse(textStream, { path: 'http://example.org/myfile.ttl', baseIRI: 'http://example.org' })
    .on('data', (quad) => console.log(quad))
    .on('error', (error) => console.error(error))
    .on('end', () => console.log('All done!'));
```

### Getting all known content types

With `rdfParser.getContentTypes()`, you can retrieve a list of all content types for which a parser is available.
Note that this method returns a promise that can be `await`-ed.

`rdfParser.getContentTypesPrioritized()` returns an object instead,
with content types as keys, and numerical priorities as values.

```javascript
// An array of content types
console.log(await rdfParser.getContentTypes());

// An object of prioritized content types
console.log(await rdfParser.getContentTypesPrioritized());
```

### Handling versions

Since RDF 1.2, serializations support optional [version announcement](https://www.w3.org/TR/rdf12-concepts/#section-version-announcement)
to make parsers fail early on unsupported versions.
By default, in-band version announcements (such as [`VERSION` in Turtle](https://www.w3.org/TR/rdf12-turtle/#sec-version)) will be detected.
Optionally, you can also pass versions out-of-band, for example when they are detected as media type parameter:

```javascript
rdfParser.parse(textStream, { version: '1.2', path: 'http://example.org/myfile.ttl', baseIRI: 'http://example.org' })
```

By default, this library is strict on its supported versions, and will emit an error on unsupported versions.
This behaviour can be disabled by setting `parseUnsupportedVersions` to `false`:

```javascript
rdfParser.parse(textStream, { parseUnsupportedVersions: false, path: 'http://example.org/myfile.ttl', baseIRI: 'http://example.org' })
```

### Obtaining prefixes

Using the `'prefix'` event, you can obtain the prefixes that were available when parsing from documents in formats such as Turtle and TriG.

```javascript
rdfParser.parse(textStream, { contentType: 'text/turtle' })
    .on('prefix', (prefix, iri) => console.log(prefix + ':' + iri))
```

### Obtaining contexts

Using the `'context'` event, you can obtain all contexts (`@context`) when parsing JSON-LD documents.

Multiple contexts can be found, and the context values that are emitted correspond exactly to the context value as included in the JSON-LD document.

```javascript
rdfParser.parse(textStream, { contentType: 'application/ld+json' })
    .on('context', (context) => console.log(context))
```

## License
This software is written by [Ruben Taelman](http://rubensworks.net/).

This code is released under the [MIT license](http://opensource.org/licenses/MIT).

---
_Source: https://npm.io/package/rdf-parse · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
