# @frogcat/ttl2jsonld

> Turtle to JSON-LD converter for node.js and browser, no library dependencies

Latest version **0.0.10** (published 2025-01-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install @frogcat/ttl2jsonld
pnpm add @frogcat/ttl2jsonld
yarn add @frogcat/ttl2jsonld
bun add @frogcat/ttl2jsonld
```

Provides the command `ttl2jsonld`.

## Health

**Score 35/100 (D)** — status: stable.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 0.0.10 |
| Published | 2025-01-15 |
| First published | 2019-06-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 140.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 33 |
| Author | frogcat |
| Maintainers | frogcat |
| Keywords | rdf, turtle, json-ld, javascript |

## Links

- npm: https://www.npmjs.com/package/@frogcat/ttl2jsonld
- Repository: https://github.com/frogcat/ttl2jsonld
- Homepage: https://github.com/frogcat/ttl2jsonld#readme
- Issues: https://github.com/frogcat/ttl2jsonld/issues
- npm.io page: https://npm.io/package/@frogcat/ttl2jsonld

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.0.10 (latest) — 2025-01-15
- 0.0.9 — 2023-01-26
- 0.0.8 — 2022-03-24
- 0.0.7 — 2022-02-17
- 0.0.6 — 2020-07-17
- 0.0.5 — 2019-10-22
- 0.0.4 — 2019-06-07
- 0.0.3 — 2019-06-03
- 0.0.2 — 2019-06-03
- 0.0.1 — 2019-06-03

## README

# @frogcat/ttl2jsonld

Turtle to JSON-LD converter for node.js and browser, no library dependencies.

## Demo

Online demo is available [here](https://frogcat.github.io/ttl2jsonld/demo/).

With this converter, you can obtain `Output JSON-LD` from `Input Turtle`, as shown in [JSON-LD 1.0 Specification](https://www.w3.org/TR/json-ld/#turtle) .

### Input Turtle

```example66.ttl
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

<http://manu.sporny.org/about#manu> a foaf:Person;
  foaf:name "Manu Sporny";
  foaf:homepage <http://manu.sporny.org/> .
```

### Output JSON-LD

```exampl67.json
{
  "@context": {
    "foaf": "http://xmlns.com/foaf/0.1/"
  },
  "@id": "http://manu.sporny.org/about#manu",
  "@type": "foaf:Person",
  "foaf:name": "Manu Sporny",
  "foaf:homepage": { "@id": "http://manu.sporny.org/" }
}
```

## Install

### node.js

```a.sh
$ npm install @frogcat/ttl2jsonld
```

### browser

```a.html
<script src="https://frogcat.github.io/ttl2jsonld/ttl2jsonld.js"></script>
```

## Usage

### node.js

You can write your own code.

```example.js
const ttl2jsonld = require('@frogcat/ttl2jsonld').parse;

const ttl = `@prefix foaf: <http://xmlns.com/foaf/0.1/> .

<http://manu.sporny.org/about#manu> a foaf:Person;
  foaf:name "Manu Sporny";
  foaf:homepage <http://manu.sporny.org/> .
`;

const jsonld = ttl2jsonld(ttl);

console.log(JSON.stringify(jsonld,null,2));

```

Command line interface is also available.

```cli.sh
$ npm install @frogcat/ttl2jsonld
$ ttl2jsonld {input_turtle} > {output_jsonld}
or
$ cat {input_turtle} | ttl2jsonld > {output_jsonld}
```

### browser

This converter is exported to `ttl2jsonld` global object. Call `ttl2jsonld.parse(ttl)` to perform conversion.

```example.html
<!-- include ttl2jsonld.js -->
<script src="https://frogcat.github.io/ttl2jsonld/ttl2jsonld.js"></script>

<!-- run script -->
<script>
const ttl = `@prefix foaf: <http://xmlns.com/foaf/0.1/> .

<http://manu.sporny.org/about#manu> a foaf:Person;
  foaf:name "Manu Sporny";
  foaf:homepage <http://manu.sporny.org/> .
`;

const jsonld = ttl2jsonld.parse(ttl);

console.log(JSON.stringify(jsonld,null,2));
</script>

```

## API

Given `turtle_string`, `ttl2jsonld.parse` returns JSON Object.

```a.js
var json_object = ttl2jsonld.parse(turtle_string);
```

When you want to pass baseIRI, give second argument like this.

```a.js
var json_object = ttl2jsonld.parse(turtle_string, {
  baseIRI : "http://example.org/"
});
```

## For developers

### building

- Build with [PEG.js](https://pegjs.org/).
- Main code is `spec/ttl2jsonld.pegjs`.
- When you edit code, run `npm run build` to generate `ttl2jsonld.js`.

### testing

- Tested with [rdf-test-suite](https://www.npmjs.com/package/rdf-test-suite) and <https://w3c.github.io/rdf-tests/rdf/rdf11/rdf-turtle/manifest.ttl> .
- Currently 298 / 301 tests succeeded.
- Run `npm run spec-turtle` to start rdr-test-suite.
- Other miscellaneous tests are located in `test/test.js`, `npm run test` to run.

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