Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs/examples/compact.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import json

from pyld import jsonld

doc = {
"http://schema.org/name": "Manu Sporny",
"http://schema.org/url": {"@id": "http://manu.sporny.org/"},
"http://schema.org/image": {
"@id": "http://manu.sporny.org/images/manu.png"
},
}

context = {
"name": "http://schema.org/name",
"homepage": {"@id": "http://schema.org/url", "@type": "@id"},
"image": {"@id": "http://schema.org/image", "@type": "@id"},
}

compacted = jsonld.compact(doc, context)

print(json.dumps(compacted, indent=2))
18 changes: 18 additions & 0 deletions docs/examples/expand.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import json

from pyld import jsonld

doc = {
"@context": {
"name": "http://schema.org/name",
"homepage": {"@id": "http://schema.org/url", "@type": "@id"},
"image": {"@id": "http://schema.org/image", "@type": "@id"},
},
"image": "http://manu.sporny.org/images/manu.png",
"homepage": "http://manu.sporny.org/",
"name": "Manu Sporny",
}

expanded = jsonld.expand(doc)

print(json.dumps(expanded, indent=2))
15 changes: 15 additions & 0 deletions docs/examples/flatten.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import json

from pyld import jsonld

doc = {
"http://schema.org/name": "Manu Sporny",
"http://schema.org/url": {"@id": "http://manu.sporny.org/"},
"http://schema.org/image": {
"@id": "http://manu.sporny.org/images/manu.png"
},
}

flattened = jsonld.flatten(doc)

print(json.dumps(flattened, indent=2))
26 changes: 26 additions & 0 deletions docs/examples/frame.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import json

from pyld import jsonld

doc = {
"@id": "http://example.com/people/manu",
"@type": "http://schema.org/Person",
"http://schema.org/name": "Manu Sporny",
"http://schema.org/url": {"@id": "http://manu.sporny.org/"},
"http://schema.org/image": {
"@id": "http://manu.sporny.org/images/manu.png"
},
}

frame = {
"@context": {
"name": "http://schema.org/name",
"homepage": {"@id": "http://schema.org/url", "@type": "@id"},
"image": {"@id": "http://schema.org/image", "@type": "@id"},
},
"@type": "http://schema.org/Person",
}

framed = jsonld.frame(doc, frame)

print(json.dumps(framed, indent=2))
12 changes: 12 additions & 0 deletions docs/examples/from_rdf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import json

from pyld import jsonld

nquads = (
'<http://dbpedia.org/resource/Earth> '
'<http://schema.org/name> "Earth" .\n'
)

doc = jsonld.from_rdf(nquads, {"format": "application/n-quads"})

print(json.dumps(doc, indent=2))
17 changes: 17 additions & 0 deletions docs/examples/normalize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from pyld import jsonld

doc = {
"@type": "http://schema.org/Person",
"http://schema.org/name": "Manu Sporny",
"http://schema.org/url": {"@id": "http://manu.sporny.org/"},
"http://schema.org/image": {
"@id": "http://manu.sporny.org/images/manu.png"
},
}

normalized = jsonld.normalize(
doc,
{"algorithm": "URDNA2015", "format": "application/n-quads"},
)

print(normalized)
File renamed without changes.
51 changes: 50 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,56 @@ applications add meaning to existing JSON documents with in-band or out-of-band
contexts, while keeping the document shape practical for web APIs, JavaScript,
and JSON document stores.

{{ example('earth.py') }}
## :material-lightning-bolt: Quick Examples
Comment thread
mielvds marked this conversation as resolved.

=== "jsonld.compact()"

Compacts a JSON-LD document with a context, replacing full IRIs with shorter
terms where possible.

{{ example('compact.py', 'json') }}

=== "jsonld.expand()"

Expands a compacted JSON-LD document into full IRI-based form and removes the
context.

{{ example('expand.py', 'json') }}

=== "jsonld.flatten()"

Flattens nested JSON-LD into a top-level node map so each node can be processed
independently.

{{ example('flatten.py', 'json') }}

=== "jsonld.frame()"

Frames expanded JSON-LD into a predictable tree shape that matches a supplied
frame.

{{ example('frame.py', 'json') }}

=== "jsonld.to_rdf()"

Converts a JSON-LD document into RDF statements in a requested serialization
format.

{{ example('to_rdf.py') }}

=== "jsonld.from_rdf()"

Converts RDF statements into JSON-LD so the data can be processed with the
JSON-LD API.

{{ example('from_rdf.py', 'json') }}

=== "jsonld.normalize()"

Normalizes JSON-LD into canonical RDF statements for stable comparison,
hashing, or signing.

{{ example('normalize.py') }}

## :fontawesome-solid-people-line: Maintainers

Expand Down
5 changes: 5 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ markdown_extensions:
- pymdownx.highlight:
anchor_linenums: true
- pymdownx.superfences
- pymdownx.tabbed:
alternate_style: true
slugify: !!python/object/apply:pymdownx.slugs.slugify
kwds:
case: lower
- toc:
permalink: true

Expand Down
Loading