/
index.html
2670 lines (2465 loc) · 151 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<title>JSON-LD 1.0 Processing Algorithms and API</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<!--
=== NOTA BENE ===
For the three scripts below, if your spec resides on dev.w3 you can check them
out in the same tree and use relative links so that they'll work offline,
-->
<script type="text/javascript" src="../respec-w3c-common.js" class="remove"></script>
<script type="text/javascript" src="../respec-w3c-extensions.js" class="remove"></script>
<script type="text/javascript" class="remove">
//<![CDATA[
var respecConfig = {
// extend the bibliography entries
"localBiblio": localBibliography,
doRDFa: "1.1",
// specification status (e.g. WD, LCWD, NOTE, etc.). If in doubt use ED.
specStatus: "ED",
// if you wish the publication date to be other than today, set this
//publishDate: "2012-12-25",
copyrightStart: "2010",
// the specification's short name, as in http://www.w3.org/TR/short-name/
shortName: "json-ld-api",
// if there is a previously published draft, uncomment this and set its YYYY-MM-DD date
// and its maturity status
previousPublishDate: "2012-09-30",
previousMaturity: "ED",
previousURI: "http://dvcs.w3.org/hg/json-ld/raw-file/66d980964784/spec/ED/json-ld-api/20120930/index.html",
// if there a publicly available Editor's Draft, this is the link
edDraftURI: "http://json-ld.org/spec/latest/json-ld-api/index.html",
testSuiteURI: "http://json-ld.org/test-suite/",
// if this is a LCWD, uncomment and set the end of its review period
// lcEnd: "2009-08-05",
// if you want to have extra CSS, append them to this list
// it is recommended that the respec.css stylesheet be kept
// extraCSS: [],
issueBase: "https://github.com/json-ld/json-ld.org/issues/",
// editors, add as many as you like
// only "name" is required
editors: [
{ name: "Manu Sporny", url: "http://manu.sporny.org/",
company: "Digital Bazaar", companyURL: "http://digitalbazaar.com/" },
{ name: "Gregg Kellogg", url: "http://greggkellogg.net/",
company: "Kellogg Associates", companyURL: "http://kellogg-assoc.com/" },
{ name: "Markus Lanthaler", url: "http://www.markus-lanthaler.com/",
company: "Graz University of Technology", companyURL: "http://www.tugraz.at/" }
],
// authors, add as many as you like.
// This is optional, uncomment if you have authors as well as editors.
// only "name" is required. Same format as editors.
authors: [
{ name: "Dave Longley", url: "http://digitalbazaar.com/",
company: "Digital Bazaar", companyURL: "http://digitalbazaar.com/"},
{ name: "Manu Sporny", url: "http://digitalbazaar.com/",
company: "Digital Bazaar", companyURL: "http://digitalbazaar.com/" },
{ name: "Gregg Kellogg", url: "http://greggkellogg.net/",
company: "Kellogg Associates", companyURL: "http://kellogg-assoc.com/" },
{ name: "Markus Lanthaler", url: "http://www.markus-lanthaler.com/",
company: "Graz University of Technology", companyURL: "http://www.tugraz.at/" }
],
// name of the WG
wg: "RDF Working Group",
// URI of the public WG page
wgURI: "http://www.w3.org/2011/rdf-wg/",
// name (with the @w3c.org) of the public mailing to which comments are due
wgPublicList: "public-rdf-comments",
// URI of the patent status for this WG, for Rec-track documents
// !!!! IMPORTANT !!!!
// This is important for Rec-track documents, do not copy a patent URI from a random
// document unless you know what you're doing. If in doubt ask your friendly neighbourhood
// Team Contact.
wgPatentURI: "http://www.w3.org/2004/01/pp-impl/46168/status",
maxTocLevel: 3,
preProcess: [ preProc ],
alternateFormats: [ {uri: "diff-20120930.html", label: "diff to previous version"} ],
};
//]]>
</script>
<style type="text/css">
.diff { font-weight:bold; color:#0a3; }
</style>
</head>
<body>
<section id="abstract">
<p>
JSON [[!RFC4627]] has proven to be a highly useful object serialization and
messaging format. JSON-LD [[!JSON-LD]] harmonizes the representation of
Linked Data in JSON by outlining a common JSON representation format for
expressing directed graphs; mixing both Linked Data and non-Linked Data in
a single document. This document outlines an Application Programming
Interface and a set of algorithms for programmatically transforming
JSON-LD documents in order to make them easier to work with in programming
environments like JavaScript, Python, and Ruby.
</p>
</section>
<section id='sotd'>
<p>This document has been under development for over 18 months in the
JSON for Linking Data Community Group. The document has recently been
transferred to the RDF Working Group for review, improvement, and publication
along the Recommendation track. The specification has undergone significant
development, review, and changes during the course of the last 18 months.
</p>
<p>There are several independent
<a href="http://json-ld.org/#impl">interoperable implementations</a> of
this specification. There is
a <a href="https://github.com/json-ld/json-ld.org/tree/master/test-suite">fairly complete test suite</a>
and a <a href="http://json-ld.org/playground/">live JSON-LD editor</a>
that is capable of demonstrating the features described in
this document. While development on implementations, the test suite
and the live editor will continue, they are believed to be mature enough
to be integrated into a non-production system at this point in time with
the expectation that they could be used in a production system within the
next year.
</p>
<p class="issue">
It is important for readers to understand that the scope of this document is
currently under debate and new features may be added to the specification.
Existing features may be modified heavily or removed entirely from the
specification upon further review and feedback from the broader community.
This is a work in progress and publication as a Working Draft
does not require that all Working Group members agree on the content of the
document.
</p>
<p>There are a number of ways that one may participate in the development of
this specification:</p>
<ul>
<li>If you want to make sure that your feedback is formally addressed by
the RDF Working Group, you should send it to public-rdf-comments:
<a href="http://lists.w3.org/Archives/Public/public-rdf-comments/">public-rdf-comments@w3.org</a></li>
<li>Ad-hoc technical discussion primarily occurs on the public community mailing list:
<a href="http://lists.w3.org/Archives/Public/public-linked-json/">public-linked-json@w3.org</a></li>
<li><a href="http://json-ld.org/minutes/">Public JSON-LD Community Group teleconferences</a>
are held on Tuesdays at 1500UTC every week. Participation is open to the
public.</li>
<li>RDF Working Group teleconferences are held on Wednesdays at 1500UTC
every week. Participation is limited to RDF Working Group members.</li>
<li>Specification bugs and issues should be reported in the
<a href="https://github.com/json-ld/json-ld.org/issues">issue tracker</a>
if you do not want to send an e-mail to the public-rdf-comments mailing
list.</li>
<li><a href="https://github.com/json-ld/json-ld.org/tree/master/spec">Source code</a>
for the specification can be found on Github.</li>
<li>The <a href="http://webchat.freenode.net/?channels=json-ld">#json-ld</a>
IRC channel is available for real-time discussion on irc.freenode.net.</li>
</ul>
</section>
<section class="informative">
<h2>Introduction</h2>
<p>
This document is a detailed specification for an Application Programming
Interface for the JSON-LD Syntax. The document is primarily intended for
the following audiences:
</p>
<ul>
<li>Web authors and developers that want a very detailed view of how
a JSON-LD processor and the API operates.</li>
<li>Software developers that want to implement processors and APIs for
JSON-LD.</li>
</ul>
<p>
To understand the basics in this specification you must first be familiar with
JSON, which is detailed in [[!RFC4627]]. You must also understand the
JSON-LD Syntax [[!JSON-LD]], which is the base syntax used by all of the
algorithms in this document. To understand the API and how it is
intended to operate in a programming environment, it is useful to have working
knowledge of the JavaScript programming language [[ECMA-262]] and
WebIDL [[!WEBIDL]]. To understand how JSON-LD maps to RDF, it is helpful to be
familiar with the basic RDF concepts [[!RDF-CONCEPTS]].</p>
</section>
<section class="informative">
<h1>Features</h1>
<p>The JSON-LD Syntax specification [[!JSON-LD]] outlines a language that may be
used to express Linked Data in JSON. Often, it is useful to be able to transform
JSON-LD documents so that they may be easily processed in various programming
environments.</p>
<p>There are four major types of transformation that are discussed in this document:
expansion, compaction, flattening, and RDF conversion.</p>
<section class="informative">
<h2>Expansion</h2>
<p>Software algorithms are easiest to write when the data that they are processing
have a regular form. Since information can be represented by JSON-LD in a
variety of different ways, transforming all of these methods into a uniform
structure allows the developer to simplify their processing code. For example,
note that the following input uses only <tref>term</tref>s and is fairly
compact:</p>
<pre class="example" data-transform="updateExample"
title="Sample JSON-LD document">
<!--
{
"@context": {
"name": "http://xmlns.com/foaf/0.1/name",
"homepage": {
"@id": "http://xmlns.com/foaf/0.1/homepage",
"@type": "@id"
}
},
"@id": "http://me.markus-lanthaler.com/",
"name": "Markus Lanthaler",
"homepage": "http://www.markus-lanthaler.com/"
}
-->
</pre>
<p>The next input example uses one <tref>URL</tref> to express a property, but leaves
the rest of the information untouched.</p>
<pre class="example" data-transform="updateExample"
title="Sample JSON-LD document using a URL instead of a term to express a property">
<!--
{
"@context": {
"homepage": {
"@id": "http://xmlns.com/foaf/0.1/homepage",
"@type": "@id"
}
},
"@id": "http://me.markus-lanthaler.com/",
"****http://xmlns.com/foaf/0.1/name****": "Markus Lanthaler",
"homepage": "http://www.markus-lanthaler.com/"
}
-->
</pre>
<p>While both inputs are valid JSON-LD, writing a program to handle every
permutation of possible inputs can be difficult, especially when the incoming
context could change as well. To ensure that the data can be given a more
uniform structure, JSON-LD introduces the notion of expansion. <tdef>Expansion</tdef>
performs two important operations. The first is to expand all values that represent
<tref title="URL">URLs</tref> to <tref title="absolute URL">absolute URLs</tref>.
The second is to express all values in <tref>expanded form</tref>. Running the
<a href="#expansion-algorithm">Expansion algorithm</a> against the examples provided
above results in the following output:</p>
<pre class="example" data-transform="updateExample"
title="Expanded sample document">
<!--
[
{
"@id": "http://me.markus-lanthaler.com/",
"http://xmlns.com/foaf/0.1/name": [
{ "@value": "Markus Lanthaler" }
],
"http://xmlns.com/foaf/0.1/homepage": [
{ "@id": "http://www.markus-lanthaler.com/" }
]
}
]
-->
</pre>
<p>Note that in the output above all <tref>context</tref> definitions have
been removed, all <tref title="term">terms</tref> and <tref title="prefix">prefixes</tref>
have been expanded to absolute <tref title="URL">URLs</tref>, and all
<tref title="JSON-LD value">JSON-LD values</tref> are expressed in <tref>expanded form</tref>.
While the output is more difficult for a human to read, it is easier for a
software program to process because of its very regular structure.</p>
</section>
<section class="informative">
<h2>Compaction</h2>
<p>While expansion expands a given input as much as possible, compaction performs
the opposite operation: it expresses a given input as succinctly as possible. In contrast
to expansion which is meant to produce something that is easy to process by software
programs, compaction is meant to produce something that is easy to read by software
developers. Compaction uses a developer-supplied <tref>context</tref> to compress
<tref title="URL">URLs</tref> to <tref title="term">terms</tref> or
<tref title="compact URL">compact URLs</tref> and <tref title="JSON-LD value">JSON-LD values</tref>
expressed in <tref>expanded form</tref> to simple values such as
<tref title="string">strings</tref> and <tref title="number">numbers</tref>.</p>
<p>For example, assume the following expanded JSON-LD input document:</p>
<pre class="example" data-transform="updateExample"
title="Expanded sample document">
<!--
[
{
"@id": "http://me.markus-lanthaler.com/",
"http://xmlns.com/foaf/0.1/name": [
{ "@value": "Markus Lanthaler" }
],
"http://xmlns.com/foaf/0.1/homepage": [
{ "@id": "http://www.markus-lanthaler.com/" }
]
}
]
-->
</pre>
<p>Additionally, assume the following developer-supplied JSON-LD <tref>context</tref>:</p>
<pre class="example" data-transform="updateExample"
title="JSON-LD context">
<!--
{
"@context": {
"name": "http://xmlns.com/foaf/0.1/name",
"homepage": {
"@id": "http://xmlns.com/foaf/0.1/homepage",
"@type": "@id"
}
}
}
-->
</pre>
<p>Running the <a href="#compaction-algorithm">Compaction Algorithm</a> given the context
supplied above against the JSON-LD input document provided above would result in the
following output:</p>
<pre class="example" data-transform="updateExample"
title="Compacted sample document">
<!--
{
"@context": {
"name": "http://xmlns.com/foaf/0.1/name",
"homepage": {
"@id": "http://xmlns.com/foaf/0.1/homepage",
"@type": "@id"
}
},
"@id": "http://me.markus-lanthaler.com/",
"name": "Markus Lanthaler",
"homepage": "http://www.markus-lanthaler.com/"
}
-->
</pre>
<p>Note that all <tref title="URL">URLs</tref> have been compacted to
<tref title="term">terms</tref> as specified in the <tref>context</tref>
which consequently has been injected into the output. While compacted
output is most useful to humans, it can often also be used to generate
structures that are easy to program against. Compaction enables developers
to map any expanded document into an application-specific compacted document.
While the context provided above mapped <code>http://xmlns.com/foaf/0.1/name</code>
to <code>name</code>, it could have also have been mapped to any other term
provided by the developer.</p>
</section>
<section class="informative">
<h2>Flattening</h2>
<p>While expansion ensures that a document is in a uniform structure, flattening
goes a step further and ensures that also the shape of the data is deterministic.
In expanded documents properties of a single <tref>node</tref> may still be
spread across a number of different <tref title="JSON object">JSON objects</tref>.
By flattening a document, all properties of a <tref>node</tref> are collected in a
single <tref>JSON object</tref> and all <tref title="blank node">blank nodes</tref>
are labeled with a <tref>blank node identifier</tref>. Often this drastically
simplifies the code to process JSON-LD data.</p>
<p>For example, assume the following JSON-LD input document:</p>
<pre class="example" data-transform="updateExample"
title="Sample JSON-LD document">
<!--
{
"@context": {
"name": "http://xmlns.com/foaf/0.1/name",
"knows": "http://xmlns.com/foaf/0.1/knows"
},
"@id": "http://me.markus-lanthaler.com/",
"name": "Markus Lanthaler",
"knows": [
{
"name": "Manu Sporny",
"knows": {
"@id": "http://greggkellogg.net/foaf#me"
}
},
{
"@id": "http://greggkellogg.net/foaf#me",
"name": "Gregg Kellogg"
}
]
}
-->
</pre>
<p>Running the <a href="#flattening-algorithm">Flattening Algorithm</a>
with a context set to <tref>null</tref> to prevent compaction returns
the following document:</p>
<pre class="example" data-transform="updateExample"
title="Flattened sample document in expanded form">
<!--
[
{
"@id": "http://me.markus-lanthaler.com/",
"http://xmlns.com/foaf/0.1/name": [
{ "@value": "Markus Lanthaler" }
],
"http://xmlns.com/foaf/0.1/knows": [
{ "@id": "_:t0" },
{ "@id": "http://greggkellogg.net/foaf#me" }
]
},
{
"@id": "_:t0",
"http://xmlns.com/foaf/0.1/name": [
{ "@value": "Manu Sporny" }
],
"http://xmlns.com/foaf/0.1/knows": [
{ "@id": "http://greggkellogg.net/foaf#me" }
]
},
{
"@id": "http://greggkellogg.net/foaf#me",
"http://xmlns.com/foaf/0.1/name": [
{ "@value": "Gregg Kellogg" }
]
}
]
-->
</pre>
<p>Note how in the output above all properties of a <tref>node</tref> are collected in a
single <tref>JSON object</tref> and how the <tref>blank node</tref> representing
"Manu Sporny" has been assigned the <tref>blank node identifier</tref>
<code>_:t0</code>.</p>
<p>To make it easier for humans to read such a flattened document can be compacted by
passing a context. Using the same context as the input document, the flattened
and compacted document looks as follows:</p>
<pre class="example" data-transform="updateExample"
title="Flattened and compacted sample document">
<!--
{
"@context": {
"name": "http://xmlns.com/foaf/0.1/name",
"knows": "http://xmlns.com/foaf/0.1/knows"
},
"@graph": [
{
"@id": "http://me.markus-lanthaler.com/",
"name": "Markus Lanthaler",
"knows": [
{ "@id": "_:t0" },
{ "@id": "http://greggkellogg.net/foaf#me" }
]
},
{
"@id": "_:t0",
"name": "Manu Sporny",
"knows": {
"@id": "http://greggkellogg.net/foaf#me"
}
},
{
"@id": "http://greggkellogg.net/foaf#me",
"name": "Gregg Kellogg"
}
]
}
-->
</pre>
<p>Please note that the flattened and compacted result will always explicitly
designate the default by the <code>@graph</code> member in the top-level
<tref>JSON object</tref>. <a href="#compaction">Compaction</a> optimizes that
member away if its value contains just one item.</p>
</section>
<section class="informative">
<h2>RDF Conversion</h2>
<p>JSON-LD can be used to serialize data expressed in RDF as described in
[[RDF-CONCEPTS]]. This ensures that data can be round-tripped from and to
any RDF syntax without any loss in the fidelity of the data.</p>
<p>For example, assume the following RDF input serialized in Turtle [[TURTLE-TR]]:</p>
<pre class="example" data-transform="updateExample"
title="Sample Turtle document">
<!--
<http://me.markus-lanthaler.com/> <http://xmlns.com/foaf/0.1/name> "Markus Lanthaler" .
<http://me.markus-lanthaler.com/> <http://xmlns.com/foaf/0.1/homepage> <http://www.markus-lanthaler.com/> .
-->
</pre>
<p>Using the <a href="#convert-from-rdf-algorithm">Convert from RDF Algorithm</a> a
developer could transform this document into expanded JSON-LD:</p>
<pre class="example" data-transform="updateExample"
title="Sample Turtle document converted to JSON-LD">
<!--
[
{
"@id": "http://me.markus-lanthaler.com/",
"http://xmlns.com/foaf/0.1/name": [
{
"@value": "Markus Lanthaler"
}
],
"http://xmlns.com/foaf/0.1/homepage": [
{
"@id": "http://www.markus-lanthaler.com/"
}
]
}
]
-->
</pre>
<p>Note that the output above could easily be compacted using the technique outlined
in the previous section. It is also possible to transform the JSON-LD document back
to RDF using the <a href="#convert-to-rdf-algorithm">Convert to RDF Algorithm</a>.</p>
</section>
</section>
<section>
<h2>Conformance</h2>
<p>All examples and notes as well as sections marked as non-normative in this
specification are non-normative. Everything else in this specification is
normative.</p>
<p>The keywords MUST, MUST NOT, REQUIRED, SHOULD, SHOULD NOT, RECOMMENDED,
MAY, and OPTIONAL in this specification are to be interpreted as described
in [[!RFC2119]].</p>
<p>There are two classes of products that can claim conformance to this
specification: <tref title="JSON-LD Implementation">JSON-LD Implementations</tref>
and <tref title="JSON-LD Processor">JSON-LD Processors</tref>.</p>
<p>A conforming <tdef>JSON-LD Implementation</tdef> is a system capable of transforming
JSON-LD documents according the algorithms defined in this specification.</p>
<p>A conforming <tdef>JSON-LD Processor</tdef> is a conforming <tref>JSON-LD Implementation</tref>
that exposes the application programming interface (API) defined in this specification.</p>
<p>The algorithms in this specification are generally written with more concern for clarity than
over efficiency. Thus, JSON-LD Implementations and Processors may implement the algorithms
given in this specification in any way desired, so long as the end result is indistinguishable
from the result that would be obtained by the specification's algorithms.</p>
<p>This specification does not define how JSON-LD Implementations or Processors handle
non-conforming input documents. This implies that JSON-LD Implementations or Processors
MUST NOT attempt to correct malformed <tref title="URL">URLs</tref> or language tags;
however, they MAY issue validation warnings. URLs are not modified other than converted
between <tref title="relative URL">relative</tref> and
<tref title="absolute URL">absolute URLs</tref>.</p>
<p class="note">Implementers can partially check their level of conformance to
this specification by successfully passing the test cases of the JSON-LD test
suite [[JSON-LD-TESTS]]. Note, however, that passing all the tests in the test
suite does not imply complete conformance to this specification. It only implies
that the implementation conforms to aspects tested by the test suite.</p>
</section>
<section>
<h3>General Terminology</h3>
<p>This document uses the following terms as defined in JSON [[!RFC4627]]. Refer
to the <em>JSON Grammar</em> section in [[!RFC4627]] for formal definitions.</p>
<dl>
<dt><tdef>JSON object</tdef></dt>
<dd>An object structure is represented as a pair of curly brackets
surrounding zero or more key-value pairs. A key is a
<tref>string</tref>. A single colon comes after each key, separating the
key from the value. A single comma separates a value from a following
key.</dd>
<dt><tdef>array</tdef></dt>
<dd>An array structure is represented as square brackets surrounding zero
or more values (or elements). Elements are separated by commas.
In JSON, an array is an <em>ordered</em> sequence of zero or more values.
While JSON-LD uses the same array representation as JSON,
the collection is <em>unordered</em> by default. While order is
preserved in regular JSON arrays, it is not in regular JSON-LD arrays
unless specific markup is provided (see <a href="#sets-and-lists"></a>).</dd>
<dt><tdef>string</tdef></dt>
<dd>A string is a sequence of zero or more Unicode characters,
wrapped in double quotes, using backslash escapes (if necessary). A
character is represented as a single character string.</dd>
<dt><tdef>number</tdef></dt>
<dd>A number is similar to that used in most programming languages, except
that the octal and hexadecimal formats are not used and that leading
zeros are not allowed.</dd>
<dt><tdef>true</tdef> and <tdef>false</tdef></dt>
<dd>Values that are used to express one of two possible boolean states.</dd>
<dt><tdef>null</tdef></dt>
<dd>The <tref>null</tref> value. A key-value pair in the
<code>@context</code> where the value, or the <code>@id</code> of the
value, is <tref>null</tref> explicitly decouples a term's association
with an URL. A key-value pair in the body of a JSON-LD document whose
value is <tref>null</tref> has the same meaning as if the key-value pair
was not defined. If <code>@value</code>, <code>@list</code>, or
<code>@set</code> is set to <tref>null</tref> in expanded form, then
the entire <tref>JSON object</tref> is ignored.</dd>
</dl>
<p>Furthermore, the following terminology is used throughout this document:</p>
<dl>
<dt><tdef>keyword</tdef></dt>
<dd>A JSON key that is specific to JSON-LD, specified in the JSON-LD Syntax specification [[!JSON-LD]]
in the section titled <cite><a href="../json-ld-syntax/#syntax-tokens-and-keywords">Syntax Tokens and Keywords</a></cite>.</dd>
<dt><tdef>context</tdef></dt>
<dd>A a set of rules for interpreting a JSON-LD document as specified in
<cite><a href="../json-ld-syntax/#the-context">The Context</a></cite> of the [[JSON-LD]] specification.</dd>
<dt><tdef>JSON-LD document</tdef></dt>
<dd>A <tref>JSON-LD document</tref> is a serialization of a collection of
<tref title="JSON-LD graph">JSON-LD graphs</tref> and comprises exactly one
<tref>default graph</tref> and zero or more <tref title="named graph">named graphs</tref>.</dd>
<dt><tdef>named graph</tdef></dt>
<dd>A named graph is a pair consisting of an <tref>URL</tref> or <tref>blank node</tref>
(the <tdef>graph name</tdef>) and a <tref>JSON-LD graph</tref>.</dd>
<dt><tdef>default graph</tdef></dt>
<dd>The default graph is the only graph in a JSON-LD document which has no <tref>graph name</tref>.</dd>
<dt><tdef>JSON-LD graph</tdef></dt>
<dd>A labeled directed graph, i.e., a set of <tref title="node">nodes</tref> connected by <tref title="edge">edges</tref>,
as specified in the <cite><a href="../json-ld-syntax/#data-model">Data Model</a></cite> section of the JSON-LD syntax
specification [[!JSON-LD]].</dd>
<dt><tdef>edge</tdef></dt>
<dd>Every <tref>edge</tref> has a direction associated with it and is labeled with
an <tref>URL</tref> or a <tref>blank node identifier</tref>. Within the JSON-LD syntax
these edge labels are called <tdef title="property">properties</tdef>. Whenever possible, an
<tref>edge</tref> SHOULD be labeled with an <tref>URL</tref>.</dd>
<dt><tdef>node</tdef></dt>
<dd>Every <tref>node</tref> is an <tref>URL</tref>, a <tref>blank node</tref>,
a <tref>JSON-LD value</tref>, or a <tref>list</tref>.</dd>
<dt><tdef>URL</tdef></dt>
<dd>Throughout this specification, the term <tref>URL</tref> means
<abbr title="Internationalized Resource Identifier">IRI</abbr> as defined in [[!RFC3987]]. The reason we
use the term <tref>URL</tref> instead is because it is more familiar to Web developers.</dd>
<dt><tdef>absolute URL</tdef></dt>
<dd>An absolute URL is an absolute IRI reference as defined in [[!RFC3987]].</dd>
<dt><tdef>relative URL</tdef></dt>
<dd>A relative URL is a relative IRI reference as defined in [[!RFC3987]]. In the case of
of JSON-LD the base URL is the location of the document.</dd>
<dt><tdef>blank node</tdef></dt>
<dd>A <tref>node</tref> in a <tref>JSON-LD graph</tref> that does not contain a de-referenceable
identifier because it is either ephemeral in nature or does not contain information that needs to be
linked to from outside of the JSON-LD graph.</dd>
<dt><tdef>blank node identifier</tdef></dt>
<dd>A blank node identifier is a string that can be used as an identifier for a <tref>blank node</tref> within
the scope of a JSON-LD document. Blank node identifiers begin with <code>_:</code>.</dd>
<dt><tdef>JSON-LD value</tdef></dt>
<dd>A <tref>JSON-LD value</tref> is a <tref>string</tref>, a <tref>number</tref>,
<tref>true</tref> or <tref>false</tref>, a <tref>typed value</tref>, or a
<tref>language-tagged string</tref>.</dd>
<dt><tdef>typed value</tdef></dt>
<dd>A <tref>typed value</tref> consists of a value, which is a string, and a type, which is an <tref>URL</tref>.</dd>
<dt><tdef>language-tagged string</tdef></dt>
<dd>A <tref>language-tagged string</tref> consists of a string and a non-empty language
tag as defined by [[BCP47]]. The language tag must be well-formed according to section
<a href="http://tools.ietf.org/html/bcp47#section-2.2.9">2.2.9</a> of [[BCP47]], and MUST
be normalized to lowercase.</dd>
<dt><tdef>list</tdef></dt>
<dd>A <tref>list</tref> is an ordered sequence of <tref title="URL">URLs</tref>,
<tref title="blank node">blank nodes</tref>, and
<tref title="JSON-LD value">JSON-LD values</tref>.</dd>
</dl>
</section>
<section>
<h1>Algorithms</h1>
<p>All algorithms described in this section are intended to operate on
language-native data structures. That is, the serialization to a text-based
JSON document isn't required as input or output to any of these algorithms and
language-native data structures MUST be used where applicable.</p>
<section>
<h2>Algorithm Terms</h2>
<dl>
<dt><tdef>active graph</tdef></dt>
<dd>The name of the currently active graph that the processor should use when
processing.</dd>
<dt><tdef>active subject</tdef></dt>
<dd>The currently active subject that the processor should use when
processing.</dd>
<dt><tdef>active property</tdef></dt>
<dd>The currently active property that the processor should use when
processing. The active property is represented in the original lexical form, which
is used for finding type mappings in the <tref>active context</tref>.</dd>
<dt><tdef>active object</tdef></dt>
<dd>The currently active object that the processor should use when
processing.</dd>
<dt><tdef>active context</tdef></dt>
<dd>A context that is used to resolve <tref>term</tref>s while the processing
algorithm is running. The <tref>active context</tref> is the context
contained within the <tref>processor state</tref>.</dd>
<dt><tdef>local context</tdef></dt>
<dd>A context that is specified within a <tref>JSON object</tref>,
specified via the <code>@context</code> <tref>keyword</tref>.</dd>
<dt><tdef>processor state</tdef></dt>
<dd>The <tref>processor state</tref>, which includes the <tref>active
context</tref>, <tref>active subject</tref>, and
<tref>active property</tref>. The <tref>processor state</tref> is managed
as a stack with elements from the previous <tref>processor state</tref>
copied into a new <tref>processor state</tref> when entering a new
<tref>JSON object</tref>.</dd>
<dt><tdef>JSON-LD input</tdef></dt>
<dd>The JSON-LD data structure that is provided as input to the algorithm.</dd>
<dt><tdef>JSON-LD output</tdef></dt>
<dd>The JSON-LD data structure that is produced as output by the algorithm.</dd>
<dt><tdef>term</tdef></dt>
<dd>A <tref>term</tref> is a short word defined in a context that MAY be expanded to an <tref>URL</tref></dd>
<dt><tdef>compact URL</tdef></dt>
<dd>A compact URL is has the form of <tdef>prefix</tdef>:<em>suffix</em> and is used as a way
of expressing an URL without needing to define separate <tref>term</tref> definitions for
each URL contained within a common vocabulary identified by <tref>prefix</tref>.</dd>
<dt><tdef>node object</tdef></dt>
<dd>A <tref>node object</tref> represents zero or more properties of a
<tref>node</tref> in the <tref>JSON-LD graph</tref> serialized by the
JSON-LD document. A <tref>JSON object</tref> is a <tref>node object</tref>
if it exists outside of the JSON-LD <tref>context</tref> and:
<ul>
<li>it does not contain the <code>@value</code>, <code>@list</code>,
or <code>@set</code> keywords, or</li>
<li>it is not the top-level <tref>JSON object</tref> in the JSON-LD document containing
the <code>@graph</code> keyword.</li>
</ul>
</dd>
<dt><tdef>list object</tdef></dt>
<dd>A <tref>list object</tref> is a <tref>JSON object</tref> that has a <code>@list</code>
member.</dd>
<dt><tdef>scalar</tdef></dt>
<dd>A scalar is either a JSON <tref>string</tref>, <tref>number</tref>, <tref>true</tref>,
or <tref>false</tref>.</dd>
<dt><tdef>quad</tdef></dt>
<dd>An <em>RDF triple</em> as specified by [[RDF-CONCEPTS]] augmented with a a fourth component,
a <tref>graph name</tref>.</dd>
</dl>
</section>
<section>
<h2>Expansion Algorithm</h2>
<p>The algorithm takes three input variables: an <tref>active context</tref>,
an <tref>active property</tref>, and an <em>element</em> to be expanded. To
begin, the <tref>active context</tref> is set to the result of performing, <a
href="#context-processing">Context Processing</a> on the passed
<code class="idlMemberName"><a href="#widl-JsonLdOptions-expandContext">expandContext</a></code>,
or empty if <code class="idlMemberName"><a href="#widl-JsonLdOptions-expandContext">expandContext</a></code>
is <tref>null</tref>, <tref>active property</tref> is set to <tref>null</tref>, and
<em>element</em> is set to the <tref>JSON-LD input</tref>. This algorithm expects the
<tref>JSON-LD input</tref> to be a well-formed JSON-LD document as defined in [[!JSON-LD]].</p>
<ol class="algorithm">
<li>If <em>element</em> is a <tref>scalar</tref>, expand it according to the
<a href="#value-expansion">Value Expansion</a> algorithm, passing copies of the
<tref>active context</tref> and <tref>active property</tref> and return. If the
<tref>active property</tref> equals <tref>null</tref> or <code>@graph</code>
set <em>element</em> to <tref>null</tref> instead and return.</li>
<li>If <em>element</em> is <tref>null</tref>, return.</li>
<li>If <em>element</em> is an <tref>array</tref>,
<ol class="algorithm">
<li>initialize an empty array <em>result</em>.</li>
<li>Expand each <em>item</em> by recursively using this algorithm, passing copies of
the <tref>active context</tref> and <tref>active property</tref>.</li>
<li>If the <tref title="active property">active property's</tref>
<tref>container mapping</tref> is set to <code>@list</code> and the expanded
<em>item</em> is an <tref>array</tref> or a <tref>list object</tref> trigger
a <code class="error">list of lists detected</code> error.</li>
<li>If the expanded <em>item</em> is <tref>null</tref>, drop it.</li>
<li>Otherwise, if the expanded <em>item</em> is an <tref>array</tref>, merge its
entries with <em>result's</em> entries.</li>
<li>Otherwise, append <em>item</em> to <em>result</em>.</li>
<li>Finally, set <em>element</em> to <em>result</em> and return.</li>
</ol>
</li>
<li>Otherwise, <em>element</em> must be an object.
<ol class="algorithm">
<li>If <em>element</em> has a <code>@context</code> member, update the
<tref>active context</tref> according to the steps outlined in
<a href="#context-processing">Context Processing</a> and remove the
<code>@context</code> member.</li>
<li>Initialize an empty <tref>JSON object</tref> <em>result</em> and</li>
<li>then process each <em>property</em> and <em>value</em> in <em>element</em>
ordered by <em>property</em> as follows:
<ol class="algorithm">
<li>If the <tref>active context</tref> contains a <tref>property generator</tref> for
<em>property</em> set <em>expanded property</em> to its <tref title="URL">URLs</tref>,
otherwise set it to the result of expanding <em>property</em> according to the steps
outlined in <a href="#url-expansion">URL Expansion</a> (passing <code>true</code> for
the <em>vocabRelative</em> flag).</li>
<li>If <em>expanded property</em> is a <tref>keyword</tref>, process it as
follows:
<ol class="algorithm">
<li>If <em>expanded property</em> equals <code>@id</code>, set the <code>@id</code>
member of <em>result</em> to the result of expanding <em>value</em>
according the <a href="#url-expansion">URL Expansion algorithm</a> (passing <code>true</code>
for the <em>documentRelative</em> flag). If <em>value</em> is not a <tref>string</tref>
trigger an <code class="error">invalid @id value</code> error.</li>
<li>If <em>expanded property</em> equals <code>@type</code>, set the <code>@type</code>
member of <em>result</em> to the result of expanding <em>value</em>
according the <a href="#url-expansion">URL Expansion algorithm</a> (passing <code>true</code>
for both the <em>documentRelative</em> and the <em>vocabRelative</em> flag). If <em>value</em>
is neither a <tref>string</tref> nor an <tref>array</tref> of
<tref title="string">strings</tref> trigger an <code class="error">invalid type value</code>
error. Empty <tref title="array">arrays</tref> are ignored.</li>
<li>If <em>expanded property</em> equals <code>@value</code>, set the <code>@value</code>
member of <em>result</em> to <em>value</em>. If <em>value</em> is neither a <tref>scalar</tref>
nor <tref>null</tref> trigger an <code class="error">invalid value object value</code> error.</li>
<li>If <em>expanded property</em> equals <code>@language</code>, set the <code>@language</code>
member of <em>result</em> to the lowercased <em>value</em>. If <em>value</em> is not
a <tref>string</tref>, trigger an <code class="error">invalid language value</code> error.</li>
<li>If <em>expanded property</em> equals <code>@annotation</code>, set the <code>@annotation</code>
member of <em>result</em> to <em>value</em>. If <em>value</em> is not a <tref>string</tref>
trigger an <code class="error">invalid @annotation value</code> error.</li>
<li>If <em>expanded property</em> equals <code>@set</code> set the <em>expanded property</em>
member of <em>result</em> to the result of expanding <em>value</em> by
recursively using this algorithm, passing copies of the <tref>active context</tref> and
<tref>active property</tref>.</li>
<li>If <em>expanded property</em> equals <code>@list</code>, set the <em>expanded property</em>
member of <em>result</em> to the result of expanding <em>value</em> by
recursively using this algorithm, passing copies of the <tref>active context</tref> and
<tref>active property</tref>; if <tref>active property</tref> equals <tref>null</tref> or
<code>@graph</code>, pass <code>@list</code> as <tref>active property</tref> instead.</li>
<li>If <em>expanded property</em> equals <code>@graph</code>, set the <code>@graph</code>
member of <em>result</em> to the result of expanding <em>value</em> by
recursively using this algorithm, passing copies of the <tref>active context</tref> and
<code>@graph</code> as <tref>active property</tref>.</li>
<li>Continue with the next <em>property</em>-<em>value</em> pair from <em>element</em>.</li>
</ol>
</li>
<li>If <em>expanded property</em> is neither a <tref>blank node identifier</tref> nor an
<tref>absolute URL</tref>,i.e., it is <tref>null</tref> or does not contain a colon
(<code>:</code>), continue with the next member from <em>element</em>.</li>
<li>Otherwise, if <em>property's</em> <tref>container mapping</tref> is set to
<code>@language</code>
<ol class="algorithm">
<li>Initialize a new empty <tref>array</tref> <em>language map values</em>.</li>
<li>Process each <em>key</em>-<em>val</em> pair of <em>value</em> ordered by
<em>key</em> as follows:
<ol class="algorithm">
<li>If <em>val</em> is not an array, transform it to one.</li>
<li>For each item of <em>val</em>, construct a new <tref>JSON object</tref>
consisting of two members: <code>@value</code> set to the currently
processed item and <code>@language</code> set to the lowercased <em>key</em>.
If <em>val</em> is not a <tref>string</tref>, trigger a
<code class="error">language map invalid value</code> error. Otherwise append
the object to <em>language map values</em>.</li>
</ol>
</li>
<li>Set <em>value</em> to <em>language map values</em>.</li>
</ol>
</li>
<li>Otherwise, if <em>property's</em> <tref>container mapping</tref> is set to
<code>@annotation</code>
<ol class="algorithm">
<li>Initialize a new empty <tref>array</tref> <em>annotation map values</em>.</li>
<li>Process each <em>key</em>-<em>val</em> pair of <em>value</em> ordered by
<em>key</em> as follows:
<ol class="algorithm">
<li>If <em>val</em> is not an array, transform it to one.</li>
<li>Expand <em>val</em> by recursively using this algorithm, passing copies of
the <tref>active context</tref> and <em>property</em> as
<tref>active property</tref>.</li>
<li>Add to each item of <em>val</em> a member <code>@annotation</code> set to
<em>key</em> if no such member exists yet and append the resulting
<tref>JSON object</tref> to <em>annotation map values</em>.</li>
</ol>
</li>
<li>Set <em>value</em> to <em>annotation map values</em>.</li>
</ol>
</li>
<li>Otherwise, expand <em>value</em> by recursively using this algorithm, passing
copies of the <tref>active context</tref> and <em>property</em> as
<tref>active property</tref>.</li>
<li>If the expanded <em>value</em> equals <tref>null</tref>, continue with the next
<em>property</em>-<em>value</em> pair from <em>element</em>.</li>
<li>If <em>property's</em> <tref>container mapping</tref> is set to <code>@list</code> and
<em>value</em> is either not an <tref>JSON object</tref> or a <tref>JSON object</tref>
without an <code>@list</code> member, replace <em>value</em> with a
<tref>JSON object</tref> with an <code>@list</code> member whose value is set to
<em>value</em> (wrapped in an <tref>array</tref> if it is not already one).</li>
<li>If <em>expanded property</em> is an <tref>array</tref>,
<ol class="algorithm">
<li>label all <tref title="blank node">blank nodes</tref> in <em>value</em> with
<tref title="blank node identifier">blank node identifiers</tref> by using the
<a href="#label-blank-nodes-algorithm">Label Blank Nodes Algorithm</a>.</li>
<li>Then, for each <em>url</em> of <em>expanded property</em> merge a copy
of <em>value</em> into the <em>url</em> member of the <em>result</em>
<tref>JSON object</tref>.</li>
</ol>
</li>
<li>Otherwise, merge <em>value</em> into the <em>url</em> member of the <em>result</em>
<tref>JSON object</tref>.</li>
</ol>
</li>
<li>Set <em>element</em> to <em>result</em> and <em>numProperties</em> to the number of members
of <em>result</em>.</li>
<li>If <em>element</em> has an <code>@annotation</code> member, decrease <em>numProperties</em>
by 1.</li>
<li>If the <tref>active property</tref> equals <tref>null</tref> or <code>@graph</code> and
<em>element</em> has an <code>@value</code> member, <em>numProperties</em> equals <code>0</code>,
or <em>element</em> consists of only an <code>@id</code> member, set <em>element</em> to
<tref>null</tref> and return.</li>
<li>If <em>element</em> has an <code>@value</code> member,
<ol class="algorithm">
<li>decrease <em>numProperties</em> by 1.</li>
<li>If <em>element</em> has an <code>@language</code> member, decrease <em>numProperties</em>
by 1 and check that the value of the <code>@value</code> member is a string. If not,
trigger an <code class="error">invalid language tagged string</code> error.</li>
<li>Otherwise, if <em>element</em> has an <code>@type</code> member, decrease
<em>numProperties</em> by 1 and check that the value of the <code>@type</code> member is a
string. If not, trigger an <code class="error">invalid typed value</code> error.</li>
<li>If <em>numProperties</em> is greater than 0, trigger an
<code class="error">invalid value object</code> error.</li>
<li>If the value of the <code>@value</code> member equals <tref>null</tref>, set
<em>element</em> to <tref>null</tref>.</li>
<li>Return.</li>
</ol>
</li>
<li>If <em>element</em> has an <code>@type</code> member whose value is not an <tref>array</tref>,
transform it to an <tref>array</tref>.</li>
<li>If <em>element</em> has an <code>@list</code> or <code>@set</code> member and
<em>numProperties</em> is greater than 1, trigger an
<code class="error">invalid set or list object</code> error.</li>
<li>Otherwise, if <em>element</em> has an <code>@set</code> member, set <em>element</em> to
the value of that member.</li>
<li>Otherwise, if <em>element</em> has just an <code>@language</code> member, set <em>element</em>
to null.</li>
</ol>
</li>
</ol>
<p>If, after the algorithm outlined above is run, the resulting <em>element</em> is an
<tref>JSON object</tref> with just a <code>@graph</code> member, <em>element</em> is set to
the value of <code>@graph</code>'s value. Finally, if <em>element</em> is a <tref>JSON object</tref>,
it is wrapped into an <tref>array</tref>, if <em>element</em> equals <tref>null</tref>, an empty
<tref>array</tref> is returned.</p>
</section>
<section>
<h2 id="context">Context Processing</h2>
<p>Processing of JSON-LD data structure is managed recursively. During processing, each
rule is applied using information provided by the <tref>active context</tref>.</p>
<p>The <tref>active context</tref> contains the active <tdef title="term definition">term definitions</tdef>
which specify how properties and values have to be interpreted as well as the current
<tdef>vocabulary mapping</tdef> and the <tdef>default language</tdef>. Each <tref>term definition</tref> consists
of an <tdef>URL mapping</tdef> and optionally a <tdef>type mapping</tdef> from terms to datatypes or
<tdef>language mapping</tdef> from terms to language codes, and a <tdef>container mapping</tdef>. If an
<tref>URL mapping</tref> maps a term to multiple <tref="URL">URLs</tref> it is said to be a
<tdef>property generator</tdef>.</p>
<p>If a <tref>local context</tref> is encountered, information from the <tref>local context</tref>
is merged into the <tref>active context</tref>. A <tref>local context</tref> is identified within
a <tref>JSON object</tref> having a <code>@context</code> member with a <tref>string</tref>,
<tref>array</tref> or a <tref>JSON object</tref> value.</p>
<p>This algorithm specifies how the <tref>active context</tref> is updated with a
<tref>local context</tref>. The algorithm takes three input variables: an <tref>active context</tref>,
a <tref>local context</tref>, and an array of already included remote contexts <em>remoteContexts</em>. To
begin, <em>remoteContexts</em> is initialized to an empty array.</p>
<p>All calls of the <a href="#url-expansion">URL Expansion algorithm</a> pass the value specified in the
algorithm along with the <tref>active context</tref>, the currently being processed <tref>local context</tref>,
and <code>true</code> for the <em>vocabRelative</em> flag.</p>
<ol class="algorithm">
<li>If the <tref>local context</tref> is not an array, transform it to one.</li>
<li>Process each item <em>context</em> of the <tref>local context</tref> as follows:
<ol class="algorithm">
<li>If <em>context</em> equals <tref>null</tref>, reset the <tref>active context</tref>