I’ve been excited about JSON-LD, when it came out as it offered a much cleaner way to do SEO. However It dawned on me later that it is also a geat format to worked with linked data.
it uses a special @id property to assign a unique URL to each resource in a JSON-LD document, giving every data item its own URL.
it links data items together through the values of properties. For example, if you’re describing a person, their “colleague” property can have another person’s URL as its value, creating a web of interconnected data items.
JSON-LD uses the @context property to map the terms used in the document to URLs, conforming data items to common global, organisational and departmental schemas.
One of the best things about JSON-LD is how easy it is to work with. Developers already familiar with JSON syntax will love using it. And get this: JSON-LD is so popular that it’s now embedded in almost half of all web pages. It’s baffling why all organisations aren’t using JSON-LD more widely to share data between their applications!
So, whether you’re publishing Data Products, creating RESTful applications or improving your website’s SEO, JSON-LD is the way to go! Give it a try and let me know your thoughts in the comments below.
person = {"@context": "https://json-ld.org/contexts/person.jsonld","@type": "https://schema.org/Person","@id": "http://dbpedia.org/resource/John_Lennon","https://schema.org/name": "John Lennon","born": "1940-10-09","spouse": "http://dbpedia.org/resource/Cynthia_Lennon"}
1
the @context refrences your model
2
the @type is the type in your model
3
the @id is the yrl for this item
4
the url is how to reference external data say dbpedia.
python
Compacting
from pyld import jsonldimport jsondoc = {"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"},"@type": "https://schema.org/Person",}context = {"name": "http://schema.org/name","homepage": {"@id": "http://schema.org/url", "@type": "@id"},"image": {"@id": "http://schema.org/image", "@type": "@id"}}# compact a document according to a particular context# see: https://json-ld.org/spec/latest/json-ld/#compacted-document-formcompacted = jsonld.compact(doc, context)print(json.dumps(compacted, indent=2))
# expand using URLs# jsonld.expand('http://example.org/doc')
Flattening
# flatten a document# see: https://json-ld.org/spec/latest/json-ld/#flattened-document-formflattened = jsonld.flatten(doc)# all deep-level trees flattened to the top-levelprint(json.dumps(flattened, indent=2))
# normalize a document using the RDF Dataset Normalization Algorithm# (URDNA2015), see: https://json-ld.github.io/normalization/spec/normalized = jsonld.normalize( doc, {'algorithm': 'URDNA2015', 'format': 'application/n-quads'})# normalized is a string that is a canonical representation of the document# that can be used for hashing, comparison, etc.print(json.dumps(normalized, indent=2))