ShExJSG

raw JSON →
0.8.2 verified Mon Apr 27 auth: no python

ShExJSG is a Python library that provides an abstract syntax tree (AST) for the ShEx 2.0 schema language, built on PyJSG and Antlr. It is used for parsing, validating, and generating ShEx schemas. Current version is 0.8.2, with a history of adaptation to newer rdflib and Python versions.

pip install shexjsg
error ModuleNotFoundError: No module named 'shexjsg'
cause The package shexjsg is not installed.
fix
Run: pip install shexjsg
error AttributeError: module 'shexjsg' has no attribute 'ShExJ'
cause Incorrect import path; ShExJ is not a direct attribute.
fix
Use: from shexjsg import ShExJ
breaking Version 0.8.0 changed rdflib support from v4 to v5/v6. Code using rdflib 4 may break.
fix Upgrade rdflib to >=5.0.0 or pin shexjsg to 0.7.1.
gotcha ShExJSG depends on PyJSG, which itself generates code. Ensure PyJSG is installed and up-to-date to avoid import errors.
fix Install/upgrade pyjsg: pip install pyjsg --upgrade

Parses a ShEx schema from a JSON string using ShExJSG.

from shexjsg import ShExJ
from rdflib import Graph
import json

# Create a simple schema
schema_json = '''{
  "@context": "http://www.w3.org/ns/shex.jsonld",
  "type": "Schema",
  "shapes": [
    {
      "id": "http://example.org/FooShape",
      "type": "Shape",
      "expression": {
        "type": "TripleConstraint",
        "predicate": "http://example.org/p",
        "valueExpr": {
          "type": "NodeConstraint",
          "datatype": "http://www.w3.org/2001/XMLSchema#string"
        }
      }
    }
  ]
}'''

# Parse and load schema
schema = ShExJ()
schema.loads(schema_json)
print('Schema loaded successfully')
print(json.dumps(schema.as_dict(), indent=2))