Skip to main content

A JSON to RDF transformer library that maps arbitrary JSON to RDF using heuristics and ontologies.

Project description

JRT – JSON to RDF Transformer

Tests Pypi Python version

Convert any JSON document to RDF/XML (or other RDF serializations) from the command line or as a library, while automatically leveraging OWL/RDFS ontologies you supply.


Features

  • Ontology‑aware mapping – classes & properties found in your OWL/RDFS ontologies are resolved first; public namespaces (FOAF, DC, …) are used only as fallback.
  • UUID subject strategy – stable UUID‑v5 URIs when an id key is present, random UUID‑v4 otherwise.
  • Heuristics out of the box – automatic rdfs:label, rdfs:comment, list handling, object‑property linking by literal label.
  • Clean Typer CLI – jrt convert input.json --ontology path/ --output out.rdf.
  • Extensible library API – integrate OntologyLoader, OntologyResolver, or GraphBuilder directly in Python code.
  • 100 % PyPI‑ready – MIT‑licensed, tested with pytest, zero runtime dependencies outside rdflib & typer.

Quick start

1 – Install

# PyPI:
pip install jrt

# Or with Poetry:
poetry add jrt

2 – CLI usage

jrt convert data.json \
  --output dist/data.rdf \
  --ontology path/to/ontologies/file_or_directory \
  --base-uri "http://example.org/resource/"
  --format ttl

--ontology can be a single RDF/OWL file or a directory; all .rdf, .owl, .xml, .ttl files are loaded.

Supported output formats (--format) : xml (default), ttl, nt, json‑ld

3 – Library usage

You can find several examples in the dedicated folder

Building

from pathlib import Path
import json
from jrt.ontology import OntologyLoader
from jrt.builder import GraphBuilder

loader = OntologyLoader()
ontologies = loader.load(Path("path/to/ontologies"))

data = json.loads(Path("input.json").read_text())

builder = GraphBuilder(data=data, ontologies=ontologies,
                       base_uri="http://example.org/resource/")

graph = builder.build()
print(graph.serialize(format="turtle"))

Add serialization rules

This library offers the possibility of adding serialization rules to extend its capabilities and avoid the need for additional post-build work.

To do this, use the add_rule method:

from rdflib import Literal, URIRef
from jrt.builder import GraphBuilder
from typing import Union, List

json_data = {
  "id": "thing123",
  "name": "MyThing",
  "custom": "This is a custom value",
  "list": ["key1", "key2", "unknown"],
  "dict": {
    "valid": "This is valid"
  }
}

def dynamic_rule(key, value) -> Union[tuple, List(tuple)]:
  # Apply transformation to elements in value.
  # You can return a tuple (ex: (key, new_value))
  # or a list of triples (ex: [(s1, p1, new_value_1), (s2, p2, new_value_2)])
  ...

static_rule_uri = URIRef(...)
static_rule_literal = Literal(..., datatype=...)

builder = GraphBuilder(data=json_data, ...)

# Add new rules
builder.add_rule('custom', static_rule_literal)
builder.add_rule('dict', static_rule_uri)
builder.add_rule('list', dynamic_rule)

graph = builder.build()

Running the CLI from source

poetry run jrt convert examples/jsons/simple.json --output output.rdf

Development

git clone https://github.com/bloodbee/jrt.git
cd jrt-python
poetry install --with dev

# run tests
pytest -q

Contributing

  1. Fork the repo and create your feature branch (git checkout -b feat/my‑feature).
  2. Commit your changes with clear messages.
  3. Ensure all tests pass (pytest).
  4. Submit a pull request.

License

Released under the MIT License. See LICENSE for the full text.


© 2025 Mathieu Dufour. All trademarks and names are property of their respective owners.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jrt-0.3.1.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jrt-0.3.1-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file jrt-0.3.1.tar.gz.

File metadata

  • Download URL: jrt-0.3.1.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.10.12 Linux/5.15.0-56-generic

File hashes

Hashes for jrt-0.3.1.tar.gz
Algorithm Hash digest
SHA256 0c7157e636e2c8535243422db8cc6fdaf897dd69641ace389fcd074354ba220c
MD5 ddbff306f2555149f5e32ca4d9a5c8d4
BLAKE2b-256 0da5e436e4a26b7d20f4f4a7757850a2b90965a37a8d4028d6559e86023c3fa8

See more details on using hashes here.

File details

Details for the file jrt-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: jrt-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.10.12 Linux/5.15.0-56-generic

File hashes

Hashes for jrt-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 413116700bcba42199054de72aaf40cf7f9f1d90d7d8f9b2211f010234a683f9
MD5 f02296d23214e3917de26c0f4707c5e7
BLAKE2b-256 6b421b2738b45dae28b336a90cc2948c27b0a50e287f088415313d1bdbd13961

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page