Linked Data Bootcamp

BetaThis is a new course. Quizzes are marked in your browser as you answer.

Module 4 of 9

Serialisation and content negotiation

One graph written as Turtle, N-Triples, RDF/XML, JSON-LD and RDFa, and one URI serving different formats on request.

What you’ll learn

By the end of this module you’ll be able to:

  • read and write Turtle
  • recognise N-Triples, RDF/XML, JSON-LD and RDFa, and convert between them
  • explain how a JSON-LD context turns JSON into triples
  • ask one URI for different formats with the Accept header
  • read the response headers that say what came back

You’ll need a terminal with curl for the last section, and the rdfpipe command, which comes with Python’s rdflib package, for one activity.

One graph, many formats

In Module 3 you wrote triples as rows in a table. The triples are the data; the table was one way to write them down. Writing a graph down as text is called serialising it, and the result is a serialisation.

There are several serialisations of RDF, each suited to a different job. They all carry the same triples, so a graph can be written in one and read back from another with nothing lost. This module writes one small graph in each: the observation of ammoniacal nitrogen in sample 2119744, the sample, and the sampling point it came from.

Turtle

Turtle is the serialisation written for people. It’s the one you’ll read and write most often, and the tables in Module 3 were already close to it.

Start with one triple

A triple in Turtle is the subject, the predicate and the object, separated by spaces and ended with a full stop. URIs go in angle brackets and strings go in double quotes:

<https://environment.data.gov.uk/water-quality/sampling-point/AN-CORBY> <http://www.w3.org/2004/02/skos/core#prefLabel> "CORBY STW FINAL EFFLUENT" .

That line is complete and correct, but long. The rest of Turtle is shorthand for writing lines like it.

Prefixes and a base

A @prefix line declares a prefix, as in Module 2. A @base line sets the address that relative URIs are resolved against, as with the tables in Module 3. Both end with a full stop:

@base <https://environment.data.gov.uk/water-quality/> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .

<sampling-point/AN-CORBY> skos:prefLabel "CORBY STW FINAL EFFLUENT" .

This is the same triple as before. skos:prefLabel expands to the full predicate URI, and <sampling-point/AN-CORBY> is resolved against the base.

Several statements about one subject

Four triples about the same subject repeat it four times:

<sampling-point/AN-CORBY> a sosa:FeatureOfInterest .
<sampling-point/AN-CORBY> a geo:Feature .
<sampling-point/AN-CORBY> skos:notation "AN-CORBY" .
<sampling-point/AN-CORBY> skos:prefLabel "CORBY STW FINAL EFFLUENT" .

Turtle lets you write the subject once. A semicolon ; means “same subject, next predicate”, and a comma , means “same subject and predicate, next object”. a is short for rdf:type:

<sampling-point/AN-CORBY>
    a sosa:FeatureOfInterest, geo:Feature ;
    skos:notation "AN-CORBY" ;
    skos:prefLabel "CORBY STW FINAL EFFLUENT" .

Both versions hold exactly the same four triples. The full stop still ends the statement.

Literals

A literal with a datatype has ^^ and the datatype after the closing quote. A string in a particular language has @ and a language tag:

<sampling-point/AN-CORBY/sampling/2119744>
    sosa:startTime "2025-11-15T08:43:00"^^xsd:dateTime ;
    sosa:resultTime "2025-12-24"^^xsd:date .

ex:W92000004 skos:prefLabel "Wales"@en, "Cymru"@cy .

Numbers can be written without quotes. In Turtle, 0.132 on its own is a decimal, the same as "0.132"^^xsd:decimal.

Blank nodes

A blank node can be written inside square brackets, as part of the statement that points at it. Everything between [ and ] is about the blank node:

<sampling-point/AN-CORBY/sample/2119744/observation/0111>
    sosa:hasResult [
        a qudt:QuantityValue ;
        qudt:upperBound 0.132 ;
        qudt:hasUnit unit:MilliGM-PER-L
    ] .

The same blank node can also be given a label beginning _: and described separately. Both forms hold the same four triples:

<sampling-point/AN-CORBY/sample/2119744/observation/0111>
    sosa:hasResult _:result .

_:result
    a qudt:QuantityValue ;
    qudt:upperBound 0.132 ;
    qudt:hasUnit unit:MilliGM-PER-L .

The whole graph

Put together, with a comment line starting #, here is everything Module 3 said about AN-CORBY, its sample, its sampling and its observation, as one Turtle document:

# The sampling point AN-CORBY, one sample, and one observation
@base <https://environment.data.gov.uk/water-quality/> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix sosa: <http://www.w3.org/ns/sosa/> .
@prefix qudt: <http://qudt.org/schema/qudt/> .
@prefix unit: <http://qudt.org/vocab/unit/> .
@prefix geo: <http://www.opengis.net/ont/geosparql#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix defra-core: <http://environment.data.gov.uk/ontology/core/> .
@prefix determinand: <https://data.example/determinands/Concept/> .
@prefix sampling-point-type: <https://data.example/sampling-point-types/Concept/> .
@prefix area: <https://data.example/areas/Concept/> .

<sampling-point/AN-CORBY>
    a sosa:FeatureOfInterest, geo:Feature ;
    skos:notation "AN-CORBY" ;
    skos:prefLabel "CORBY STW FINAL EFFLUENT" ;
    skos:altLabel "CORBY STW F/E" ;
    defra-core:hasClassification sampling-point-type:SA ;
    geo:sfWithin area:M ;
    geo:hasGeometry <sampling-point/AN-CORBY#Geometry> .

<sampling-point/AN-CORBY/sample/2119744>
    a sosa:Sample ;
    sosa:isSampleOf <sampling-point/AN-CORBY> ;
    sosa:isResultOf <sampling-point/AN-CORBY/sampling/2119744> .

<sampling-point/AN-CORBY/sampling/2119744>
    a sosa:Sampling ;
    sosa:hasFeatureOfInterest <sampling-point/AN-CORBY> ;
    sosa:startTime "2025-11-15T08:43:00"^^xsd:dateTime ;
    sosa:resultTime "2025-12-24"^^xsd:date ;
    sosa:hasResult <sampling-point/AN-CORBY/sample/2119744> .

<sampling-point/AN-CORBY/sample/2119744/observation/0111>
    a sosa:Observation ;
    sosa:hasSample <sampling-point/AN-CORBY/sample/2119744> ;
    sosa:observedProperty determinand:0111 ;
    sosa:phenomenonTime "2025-11-15T08:43:00"^^xsd:dateTime ;
    sosa:hasSimpleResult "<0.132" ;
    sosa:hasResult [
        a qudt:QuantityValue ;
        qudt:upperBound 0.132 ;
        qudt:hasUnit unit:MilliGM-PER-L
    ] .

determinand:0111
    a skos:Concept ;
    skos:notation "0111" ;
    skos:prefLabel "Ammoniacal Nitrogen as N" ;
    skos:altLabel "Ammonia(N)" .
Activity 1

Write Turtle

This activity’s URI: #activity-1

Module 3 described the determinand 0111 with a table of four triples. Write them as Turtle, declaring your prefixes, writing the subject once, and adding a fifth triple that says it’s a skos:Concept.

The prefix determinand: stands for https://data.example/determinands/Concept/, and the concept scheme is dataset:determinands, where dataset: stands for https://data.example/.

Show the answer
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix determinand: <https://data.example/determinands/Concept/> .
@prefix dataset: <https://data.example/> .

determinand:0111
    a skos:Concept ;
    skos:notation "0111" ;
    skos:prefLabel "Ammoniacal Nitrogen as N" ;
    skos:altLabel "Ammonia(N)" ;
    skos:inScheme dataset:determinands .

Each line before the last ends with a semicolon, because the subject carries on. The last ends with a full stop.

Activity 2

Spot the mistakes

This activity’s URI: #activity-2

This Turtle has three mistakes, and a parser would reject it. Find them.

@prefix skos: <http://www.w3.org/2004/02/skos/core#> .

determinand:0076
    skos:notation "0076" ;
    skos:prefLabel "Temperature of Water" ,
    skos:altLabel "Temp Water"
Show the answer
  1. The prefix determinand: is used but never declared.
  2. The line with skos:prefLabel ends in a comma, which would make the next line another label. It needs a semicolon.
  3. The last line has no full stop.
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix determinand: <https://data.example/determinands/Concept/> .

determinand:0076
    skos:notation "0076" ;
    skos:prefLabel "Temperature of Water" ;
    skos:altLabel "Temp Water" .

N-Triples

N-Triples is Turtle with every piece of shorthand taken away. Each line is one complete triple, with full URIs and no prefixes, base, semicolons or brackets. The first line you wrote in this module was already N-Triples.

<https://environment.data.gov.uk/water-quality/sampling-point/AN-CORBY> <http://www.w3.org/2004/02/skos/core#prefLabel> "CORBY STW FINAL EFFLUENT" .
<https://environment.data.gov.uk/water-quality/sampling-point/AN-CORBY/sample/2119744> <http://www.w3.org/ns/sosa/isSampleOf> <https://environment.data.gov.uk/water-quality/sampling-point/AN-CORBY> .
<https://environment.data.gov.uk/water-quality/sampling-point/AN-CORBY/sample/2119744/observation/0111> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/ns/sosa/Observation> .
<https://environment.data.gov.uk/water-quality/sampling-point/AN-CORBY/sample/2119744/observation/0111> <http://www.w3.org/ns/sosa/hasSample> <https://environment.data.gov.uk/water-quality/sampling-point/AN-CORBY/sample/2119744> .
<https://environment.data.gov.uk/water-quality/sampling-point/AN-CORBY/sample/2119744/observation/0111> <http://www.w3.org/ns/sosa/observedProperty> <https://data.example/determinands/Concept/0111> .
<https://environment.data.gov.uk/water-quality/sampling-point/AN-CORBY/sample/2119744/observation/0111> <http://www.w3.org/ns/sosa/phenomenonTime> "2025-11-15T08:43:00"^^<http://www.w3.org/2001/XMLSchema#dateTime> .
<https://environment.data.gov.uk/water-quality/sampling-point/AN-CORBY/sample/2119744/observation/0111> <http://www.w3.org/ns/sosa/hasResult> _:result .
_:result <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://qudt.org/schema/qudt/QuantityValue> .
_:result <http://qudt.org/schema/qudt/upperBound> "0.132"^^<http://www.w3.org/2001/XMLSchema#decimal> .
_:result <http://qudt.org/schema/qudt/hasUnit> <http://qudt.org/vocab/unit/MilliGM-PER-L> .

It’s long to read, but it suits programs. Every line stands on its own, so a file can be split, streamed, sorted or compared line by line, and loading millions of triples into a database is simple. The blank node keeps its label, _:result, so the lines that describe it still connect.

RDF/XML

RDF/XML was the first standard way of writing RDF, and you’ll meet it in older published data and in many ontologies. It writes the same triples as XML elements:

<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:skos="http://www.w3.org/2004/02/skos/core#"
    xmlns:sosa="http://www.w3.org/ns/sosa/"
    xmlns:qudt="http://qudt.org/schema/qudt/"
    xml:base="https://environment.data.gov.uk/water-quality/">

  <sosa:Observation rdf:about="sampling-point/AN-CORBY/sample/2119744/observation/0111">
    <sosa:hasSample rdf:resource="sampling-point/AN-CORBY/sample/2119744"/>
    <sosa:observedProperty rdf:resource="https://data.example/determinands/Concept/0111"/>
    <sosa:phenomenonTime rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">2025-11-15T08:43:00</sosa:phenomenonTime>
    <sosa:hasResult>
      <qudt:QuantityValue>
        <qudt:upperBound rdf:datatype="http://www.w3.org/2001/XMLSchema#decimal">0.132</qudt:upperBound>
        <qudt:hasUnit rdf:resource="http://qudt.org/vocab/unit/MilliGM-PER-L"/>
      </qudt:QuantityValue>
    </sosa:hasResult>
  </sosa:Observation>

  <rdf:Description rdf:about="sampling-point/AN-CORBY/sample/2119744">
    <sosa:isSampleOf rdf:resource="sampling-point/AN-CORBY"/>
  </rdf:Description>

  <rdf:Description rdf:about="sampling-point/AN-CORBY">
    <skos:prefLabel>CORBY STW FINAL EFFLUENT</skos:prefLabel>
  </rdf:Description>
</rdf:RDF>

Each part maps straight onto something you already know:

xmlns:skos="…"
Declares a prefix, like @prefix in Turtle.
xml:base="…"
Sets the base for relative URIs, like @base.
rdf:about="…"
The subject. Every element inside it is a statement about this subject.
<sosa:Observation …>
An element named after a class says the subject has that type, like a sosa:Observation. rdf:Description describes a subject without giving a type.
rdf:resource="…"
The object is a URI.
Text inside an element
The object is a literal, with rdf:datatype giving its datatype.
An element with no rdf:about
A blank node, like [ ] in Turtle.

Converting RDF/XML to Turtle

Because both formats carry the same triples, a program can read one and write the other. Save the XML above as corby.rdf, then run:

pip install rdflib
rdfpipe -o turtle corby.rdf

Installing rdflib also installs rdfpipe, a command that reads RDF in one format and writes it in another. -o turtle chooses the format to write. The format to read is worked out from the file’s .rdf extension, and -i sets it when the name gives no clue.

Here is what it prints:

@prefix qudt: <http://qudt.org/schema/qudt/> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix sosa: <http://www.w3.org/ns/sosa/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

<https://environment.data.gov.uk/water-quality/sampling-point/AN-CORBY/sample/2119744/observation/0111> a sosa:Observation ;
    sosa:hasResult [ a qudt:QuantityValue ;
            qudt:hasUnit <http://qudt.org/vocab/unit/MilliGM-PER-L> ;
            qudt:upperBound 0.132 ] ;
    sosa:hasSample <https://environment.data.gov.uk/water-quality/sampling-point/AN-CORBY/sample/2119744> ;
    sosa:observedProperty <https://data.example/determinands/Concept/0111> ;
    sosa:phenomenonTime "2025-11-15T08:43:00"^^xsd:dateTime .

<https://environment.data.gov.uk/water-quality/sampling-point/AN-CORBY> skos:prefLabel "CORBY STW FINAL EFFLUENT" .

<https://environment.data.gov.uk/water-quality/sampling-point/AN-CORBY/sample/2119744> sosa:isSampleOf <https://environment.data.gov.uk/water-quality/sampling-point/AN-CORBY> .

The Turtle looks different from the version you wrote. The URIs are written out in full because the converter has no base to use, the predicates are in alphabetical order, and the blank node’s brackets are laid out another way. It’s the same graph: the same ten triples, which is what “meaning the same thing” comes down to. Serialisations differ in layout, and the triples are what count.

Activity 3

Convert and compare

This activity’s URI: #activity-3

Convert corby.rdf to N-Triples by changing -o turtle to -o nt in the command above. Count the lines, and compare them with the N-Triples in this module.

Show the answer

You get ten lines, matching the ten triples above. They may come out in a different order, and the blank node gets a generated label in place of _:result, but every triple is there. Turtle, N-Triples and RDF/XML have written down one graph three ways.

You may also see a warning that N-Triples is always written as UTF-8. That’s rdflib telling you how it wrote the file, and the output is correct.

JSON-LD

JSON-LD is RDF written as JSON, and it’s the native format of the RESTful Linked Data APIs on environment.data.gov.uk. It’s built so that a developer who has never heard of RDF can use the response as ordinary JSON, while anyone who wants the triples can get them exactly.

Here is the same graph as JSON-LD, shaped the way the APIs shape their responses: short keys, an id for each thing, and related things nested inside the thing that points to them.

{
  "@context": {
    "@base": "https://environment.data.gov.uk/water-quality/",
    "sosa": "http://www.w3.org/ns/sosa/",
    "skos": "http://www.w3.org/2004/02/skos/core#",
    "qudt": "http://qudt.org/schema/qudt/",
    "unit": "http://qudt.org/vocab/unit/",
    "xsd": "http://www.w3.org/2001/XMLSchema#",
    "id": "@id",
    "type": "@type",
    "hasSample": { "@id": "sosa:hasSample", "@type": "@id" },
    "isSampleOf": { "@id": "sosa:isSampleOf", "@type": "@id" },
    "observedProperty": { "@id": "sosa:observedProperty", "@type": "@id" },
    "phenomenonTime": { "@id": "sosa:phenomenonTime", "@type": "xsd:dateTime" },
    "hasResult": { "@id": "sosa:hasResult" },
    "upperBound": { "@id": "qudt:upperBound", "@type": "xsd:decimal" },
    "hasUnit": { "@id": "qudt:hasUnit", "@type": "@id" },
    "prefLabel": "skos:prefLabel"
  },
  "id": "sampling-point/AN-CORBY/sample/2119744/observation/0111",
  "type": "sosa:Observation",
  "hasSample": {
    "id": "sampling-point/AN-CORBY/sample/2119744",
    "isSampleOf": {
      "id": "sampling-point/AN-CORBY",
      "prefLabel": "CORBY STW FINAL EFFLUENT"
    }
  },
  "observedProperty": "https://data.example/determinands/Concept/0111",
  "phenomenonTime": "2025-11-15T08:43:00",
  "hasResult": {
    "type": "qudt:QuantityValue",
    "upperBound": "0.132",
    "hasUnit": "unit:MilliGM-PER-L"
  }
}

The context

The @context is what turns this JSON into triples. It maps each short key to a full URI:

  • "prefLabel": "skos:prefLabel" says the key prefLabel means the predicate skos:prefLabel
  • "id": "@id" and "type": "@type" say which keys hold a thing’s URI and its class
  • "@type": "@id" on a key says its value is a URI, so observedProperty links to a concept
  • "@type": "xsd:dateTime" on a key gives its values a datatype, so phenomenonTime is a date and time
  • "@base" sets the base for relative URIs, as in Turtle

The API puts its context in a file of its own and refers to it by URL, so each response stays short. A response from the observation endpoint starts with "@context": "https://environment.data.gov.uk/water-quality/context/wqa_observation_context.json-ld".

Nesting is the graph, shaped as a tree

The sample sits inside the observation, and the sampling point sits inside the sample. Each nested object with an id is a thing with a URI, and the key it sits under is the predicate that links them. The result has no id, so it’s a blank node: the JSON-LD form of [ ].

Ignore every key starting with @ and you have plain JSON with readable keys, which is how most developers will use it. Read the context as well and you have exactly the ten triples from the Turtle.

Activity 4

Read JSON-LD as triples

This activity’s URI: #activity-4

Using the context, write the triple that each of these parts of the JSON-LD gives you, in Turtle:

  1. "phenomenonTime": "2025-11-15T08:43:00" on the observation
  2. "isSampleOf": { "id": "sampling-point/AN-CORBY", … } on the sample
  3. "hasUnit": "unit:MilliGM-PER-L" on the result

Then paste the whole JSON-LD into the JSON-LD Playground and look at the N-Quads tab to check your answers.

Show the answer
<sampling-point/AN-CORBY/sample/2119744/observation/0111>
    sosa:phenomenonTime "2025-11-15T08:43:00"^^xsd:dateTime .

<sampling-point/AN-CORBY/sample/2119744>
    sosa:isSampleOf <sampling-point/AN-CORBY> .

_:result qudt:hasUnit unit:MilliGM-PER-L .

The datatype on the first comes from the context, not the JSON value. The third has a blank node as its subject, because the result has no id. The Playground shows N-Quads, which is N-Triples with an optional fourth part naming a graph; here the fourth part is empty.

RDFa

RDFa writes triples into the attributes of an ordinary web page, so the page a person reads is also data. This course is written that way. Here is a cut-down version of the source of this page:

<body vocab="https://schema.org/">
  <main about="" typeof="LearningResource">
    <link property="isPartOf" href="../../">
    <h1 property="name">Serialisation and content negotiation</h1>
    <section id="quiz" property="hasPart" typeof="Quiz" resource="#quiz">
      …
    </section>
  </main>
</body>

And here are the triples it holds, in Turtle:

@base <https://example.org/modules/serialisation-and-content-negotiation/> .
@prefix schema: <https://schema.org/> .

<> a schema:LearningResource ;
    schema:isPartOf <../../> ;
    schema:name "Serialisation and content negotiation" ;
    schema:hasPart <#quiz> .

<#quiz> a schema:Quiz .

vocab sets the vocabulary for unprefixed names, like a default prefix. about sets the subject, and about="" means this page itself. typeof gives a type, and property gives a predicate whose object is the element’s text or link. An RDFa processor reading the page also records one extra triple saying which vocabulary the page uses.

View the source of any page in this course to read the rest: the modules, the activities and each quiz question, with its accepted answer.

Content negotiation

Each thing has one URI, and the same URI can be served in different formats. When a program requests a URI, it sends an Accept header listing the formats it wants. The server sends back the closest match it has and says what it chose in the Content-Type header. This is content negotiation.

Your browser asks for a web page, so opening AN-CORBY’s URI shows you a page. A program can ask the same URI for data.

JSON-LD

curl -H "Accept: application/ld+json" https://environment.data.gov.uk/water-quality/sampling-point/AN-CORBY
{"@context":"https://environment.data.gov.uk/water-quality/context/wqa_samplingpoint_context.json-ld",
 "@type":"hydra:Collection","totalItems":1,
 "member":[{"id":"https://environment.data.gov.uk/water-quality/sampling-point/AN-CORBY",
   "@type":["sosa:FeatureOfInterest","geo:Feature"],
   "altLabel":"CORBY STW F/E","prefLabel":"CORBY STW FINAL EFFLUENT","notation":"AN-CORBY",
   "hasObservations":"https://environment.data.gov.uk/water-quality/sampling-point/AN-CORBY/observation",
   …}]}

The response is a collection with one member: the sampling point, with its URI as id, its two classes, its notation and labels. hasObservations is the URI of its observations, so a program can follow the link to them.

GeoJSON

curl -H "Accept: application/geo+json" https://environment.data.gov.uk/water-quality/sampling-point/AN-CORBY
{"type":"hydra:Collection","member":[{"type":"Feature",
  "geometry":{"coordinates":[-0.665027896,52.490089102],"type":"Point"},
  "properties":{"name":"CORBY STW FINAL EFFLUENT","status":"OPEN",
    "type":"SEWAGE DISCHARGES - FINAL/TREATED EFFLUENT - WATER COMPANY",
    "region":"Anglian","area":"ANGLIAN - LINCS AND NORTHANTS",
    "subArea":"L&W NORTH & SOUTH","notation":"AN-CORBY",
    "isDistributionOf":"https://environment.data.gov.uk/water-quality/sampling-point/AN-CORBY"}}],
 "totalItems":1}

Same URI, same sampling point, now as GeoJSON, which mapping tools read directly. The point is given in longitude and latitude, which GeoJSON always uses, and isDistributionOf carries the sampling point’s URI, so even this format links back to the thing it describes.

CSV, for lists

The list of a sampling point’s observations can also be had as CSV or as JSON Lines, one JSON object per line, which suit spreadsheets and streaming. Add -i to see the response headers as well as the body:

curl -i -H "Accept: text/csv" "https://environment.data.gov.uk/water-quality/sampling-point/AN-CORBY/observation?limit=2"
HTTP/2 200
content-type: text/csv; charset=utf-8
vary: API-Version,Accept,Content-Crs
link: <…/observation?limit=2&skip=0>; rel="first", <…/observation?limit=2&skip=2>; rel="next", …

id,samplingPoint.notation,samplingPoint.prefLabel,…,determinand.notation,determinand.prefLabel,result,unit
https://environment.data.gov.uk/water-quality/sampling-point/AN-CORBY/sample/687968/observation/0076,AN-CORBY,CORBY STW FINAL EFFLUENT,…,0076,Temperature of Water,15,CELSIUS
https://environment.data.gov.uk/water-quality/sampling-point/AN-CORBY/sample/687968/observation/0085,AN-CORBY,CORBY STW FINAL EFFLUENT,…,0085,BOD : 5 Day ATU,<3,MILLIGRAM PER LITRE

Three headers are worth reading:

  • content-type says which format came back
  • vary includes Accept, which tells caches that this URI gives different responses for different Accept headers
  • link gives the URIs of the first, next and last pages, so a program can walk through thousands of observations a page at a time

Even in CSV, the first column is each observation’s URI. The key travels with the data in every format.

Asking for a format that isn’t offered

curl -i -H "Accept: text/csv" https://environment.data.gov.uk/water-quality/sampling-point/AN-CORBY
HTTP/2 400
content-type: application/json

{"detail":"Requested serialization format not allowed. Accept header: text/csv. Allowed: ['application/ld+json', 'application/geo+json']"}

Each endpoint offers the formats that suit it. A single sampling point is offered as JSON-LD or GeoJSON, and the error says so, so you know what to ask for instead.

File extensions

Some services also give each format an address of its own by adding an extension, such as AN-CORBY.ttl or AN-CORBY.json. That can be handy for a download link, but it creates a second URI for the same sampling point, one that names a file format as well as a thing. The Water Quality service keeps one URI per thing and lets the Accept header choose the format.

Activity 5

Negotiate

This activity’s URI: #activity-5

Using curl -i, make four requests and note the status and content-type of each:

  1. the observation https://environment.data.gov.uk/water-quality/sampling-point/AN-CORBY/sample/2119744/observation/0111, asking for application/ld+json
  2. the same observation, asking for text/csv
  3. the list https://environment.data.gov.uk/water-quality/sampling-point/AN-CORBY/observation?limit=5, asking for text/csv
  4. the same list, asking for application/x-jsonlines
Show the answer
  1. 200 and application/ld+json, with a result of less than 0.132 milligrams per litre.
  2. 400, with a message saying a single observation is offered as application/ld+json.
  3. 200 and text/csv: five rows, one observation each, with a link header pointing to the next page.
  4. 200 and application/x-jsonlines: the same five observations, one JSON object per line.

Each endpoint offers the formats that suit what it holds. A single observation is one small graph, so it comes as JSON-LD; a list of thousands suits formats that can be read a line at a time.

Check your understanding

Choose an answer to see whether it’s right. You can change your answer as often as you like.

Question 1 of 5In Turtle, what does a semicolon ; at the end of a line mean?

Correct

Not quite

Question 2 of 5An RDF/XML file and a Turtle file both convert to the same ten lines of N-Triples. What does that tell you?

Correct

Not quite

Question 3 of 5In a JSON-LD response, what tells a program that the key prefLabel means skos:prefLabel?

Correct

Not quite

Question 4 of 5How does a program ask AN-CORBY’s URI for GeoJSON?

Correct

Not quite

Question 5 of 5Why does a response that depends on the Accept header include Accept in its vary header?

Correct

Not quite

You’ve answered of 5 questions and got right.

Module 4 complete

You answered every question correctly

Summary

In this module you learned that:

  • a serialisation is a way of writing a graph down, and every serialisation of a graph holds the same triples
  • Turtle is written for people, with prefixes, a base, ;, ,, a and [ ] as shorthand
  • N-Triples is one full triple per line, for programs and bulk data
  • RDF/XML writes the same triples as XML, and converts to Turtle with a single command
  • JSON-LD is ordinary JSON plus a context that maps its keys to URIs, and it’s the native format of the APIs
  • RDFa puts triples into a web page’s attributes, which is how this course describes itself
  • one URI can serve several formats, chosen with the Accept header and confirmed by content-type

Module 5 reads the vocabularies behind graphs like this one, so you can describe things of your own with them.