forked from json-ld/json-ld.org
/
index.html
2288 lines (2078 loc) · 113 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>The JSON-LD API 1.0</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" class="remove">
//<![CDATA[
var preProc = {
apply: function(c) {
// extend the bibliography entries
berjon.biblio["MICRODATA"] = "<cite><a href=\"http://www.w3.org/TR/2012/WD-microdata-20120329/\">HTML Microdata</a></cite> Ian Hickson Editor. World Wide Web Consortium (work in progress). 29 March 2012. This edition of the HTML Microdata specification is http://www.w3.org/TR/2012/WD-microdata-20120329/. The <a href=\"http://www.w3.org/TR/microdata/\">latest edition of HTML Microdata</a> is available at http://www.w3.org/TR/microdata/";
berjon.biblio["JSON-LD"] = "<cite><a href=\"http://json-ld.org/spec/ED/json-ld-syntax/20120522/\">The JSON-LD Syntax</a></cite> Manu Sporny, Gregg Kellogg, Markus Lanthaler Editors. World Wide Web Consortium (work in progress). 22 May 2012. Editor's Draft. This edition of the JSON-LD Syntax specification is http://json-ld.org/spec/ED/json-ld-syntax/20120522/. The <a href=\"http://json-ld.org/spec/latest/json-ld-syntax/\">latest edition of the JSON-LD Syntax</a> is available at http://json-ld.org/spec/latest/json-ld-syntax/";
berjon.biblio["JSON-POINTER"] = "<cite><a href=\"http://tools.ietf.org/html/draft-ietf-appsawg-json-pointer-02\">JSON Pointer</a></cite> P. Bryan, Ed. IETF Draft. URL: <a href=\"http://tools.ietf.org/html/draft-ietf-appsawg-json-pointer-02\">http://tools.ietf.org/html/draft-ietf-appsawg-json-pointer-02</a>";
berjon.biblio["RDF-NORMALIZATION"] = "<cite><a href=\"http://json-ld.org/spec/ED/rdf-graph-normalization/20111016/\">RDF Graph Normalization</a></cite> Manu Sporny, Dave Longley Editors. World Wide Web Consortium (work in progress). 16 October 2011. Editor's Draft. This edition of the RDF Graph Normalization specification is http://json-ld.org/spec/ED/rdf-graph-normalization/20111016/. The <a href=\"http://json-ld.org/spec/latest/rdf-graph-normalization/\">latest edition of RDF Graph Normalization</a> is available at http://json-ld.org/spec/latest/rdf-graph-normalization/";
berjon.biblio["IEEE-754-1985"] = "IEEE. <cite>IEEE Standard for Binary Floating-Point Arithmetic.</cite> See <a href=\"http://standards.ieee.org/reading/ieee/std_public/description/busarch/754-1985_desc.html\">http://standards.ieee.org/reading/ieee/std_public/description/busarch/754-1985_desc.html</a>";
berjon.biblio["RDF-CONCEPTS"] = "<cite><a href=\"http://www.w3.org/TR/2011/WD-rdf11-concepts-20110830/\">RDF 1.1 Concepts and Abstract Syntax</a></cite> Richard Cyganiak, David Wood, Editors. World Wide Web Consortium (work in progress). 30 May 2012. Editor's Draft. This edition of the JSON-LD Syntax specification is http://www.w3.org/TR/2011/WD-rdf11-concepts-20110830/. The <a href=\"http://www.w3.org/TR/rdf11-concepts/\">latest edition of the JSON-LD Syntax</a> is available at http://www.w3.org/TR/rdf11-concepts/";
berjon.biblio["TURTLE-TR"] = "Eric Prud'hommeaux, Gavin Carothers. <cite><a href=\"http://www.w3.org/TR/2011/WD-turtle-20110809/\">Turtle: Terse RDF Triple Language.</a></cite> 09 August 2011. W3C Working Draft. URL: <a href=\"http://www.w3.org/TR/2011/WD-turtle-20110809/\">http://www.w3.org/TR/2011/WD-turtle-20110809/</a>";
berjon.biblio["WEBIDL"] = "<cite><a href=\"http://www.w3.org/TR/2012/CR-WebIDL-20120419/\">Web IDL</a></cite> Cameron McCormack, Editor. World Wide Web Consortium. 19 April 2012. Candidate Recommendataion. This edition of Web IDL is http://www.w3.org/TR/2012/CR-WebIDL-20120419/. The <a href=\"http://dev.w3.org/2006/webapi/WebIDL/\">latest edition of Web IDL</a> is available at http://dev.w3.org/2006/webapi/WebIDL/";
// process the document before anything else is done
var refs = document.querySelectorAll('adef') ;
for (var i = 0; i < refs.length; i++) {
var item = refs[i];
var p = item.parentNode ;
var con = item.innerHTML ;
var sp = document.createElement( 'dfn' ) ;
var tit = item.getAttribute('title') ;
if (!tit) {
tit = con;
}
sp.className = 'adef' ;
sp.title=tit ;
sp.innerHTML = con ;
p.replaceChild(sp, item) ;
}
refs = document.querySelectorAll('aref') ;
for (var i = 0; i < refs.length; i++) {
var item = refs[i];
var p = item.parentNode ;
var con = item.innerHTML ;
var sp = document.createElement( 'a' ) ;
sp.className = 'aref' ;
sp.setAttribute('title', con);
sp.innerHTML = '@'+con ;
p.replaceChild(sp, item) ;
}
// local datatype references
refs = document.querySelectorAll('ldtref') ;
for (var i = 0; i < refs.length; i++) {
var item = refs[i];
if (!item) continue ;
var p = item.parentNode ;
var con = item.innerHTML ;
var ref = item.getAttribute('title') ;
if (!ref) {
ref = item.textContent ;
}
if (ref) {
ref = ref.replace(/\s+/g, '_') ;
}
var sp = document.createElement( 'a' ) ;
sp.className = 'datatype idlType';
sp.title = ref ;
sp.setAttribute('href', '#idl-def-' + ref);
sp.innerHTML = '<code>' + con + '</code>';
p.replaceChild(sp, item) ;
}
// external datatype references
refs = document.querySelectorAll('dtref') ;
for (var i = 0; i < refs.length; i++) {
var item = refs[i];
if (!item) continue ;
var p = item.parentNode ;
var con = item.innerHTML ;
var ref = item.getAttribute('title') ;
if (!ref) {
ref = item.textContent ;
}
if (ref) {
ref = ref.replace(/\s+/g, '_') ;
}
var sp = document.createElement( 'a' ) ;
sp.className = 'externalDFN';
sp.title = ref ;
sp.innerHTML = con ;
p.replaceChild(sp, item) ;
}
// now do terms
refs = document.querySelectorAll('tdef') ;
var tdefs = [];
for (var i = 0; i < refs.length; i++) {
var item = refs[i];
if (!item) continue ;
var p = item.parentNode ;
var con = item.innerHTML ;
var ref = item.getAttribute('title') ;
if (!ref) {
ref = item.textContent ;
}
if (ref) {
ref = ref.replace(/\s+/g, '_').toLowerCase() ;
}
if ( tdefs[ref]) {
throw "Duplicate definition of term '" + ref + "'" ;
}
var sp = document.createElement( 'dfn' ) ;
tdefs[ref] = sp ;
sp.title = ref ;
sp.innerHTML = con ;
p.replaceChild(sp, item) ;
}
// now term references
refs = document.querySelectorAll('tref') ;
for (var i = 0; i < refs.length; i++) {
var item = refs[i];
if (!item) continue ;
var p = item.parentNode ;
var con = item.innerHTML ;
var ref = item.getAttribute('title') ;
if (!ref) {
ref = item.textContent ;
}
if (ref) {
ref = ref.replace(/\s+/g, '_').toLowerCase() ;
}
if ( !tdefs[ref]) {
throw "Reference to undefined term '" + ref + "'" ;
}
var sp = document.createElement( 'a' ) ;
var id = item.textContent ;
sp.className = 'tref' ;
sp.title = ref ;
sp.innerHTML = con ;
p.replaceChild(sp, item) ;
}
}
} ;
var respecConfig = {
doRDFa: "1.1",
// specification status (e.g. WD, LCWD, NOTE, etc.). If in doubt use ED.
specStatus: "CG-FINAL",
//publishDate: "2010-04-29",
copyrightStart: "2010",
// the specification's short name, as in http://www.w3.org/TR/short-name/
shortName: "json-ld-api",
subtitle: "An Application Programming Interface for the JSON-LD Syntax",
// if you wish the publication date to be other than today, set this
// publishDate: "2009-08-06",
// if there is a previously published draft, uncomment this and set its YYYY-MM-DD date
// and its maturity status
previousPublishDate: "2012-05-24",
previousMaturity: "CG-DRAFT",
previousDiffURI: "http://json-ld.org/spec/ED/json-ld-api/20120524/",
diffTool: "http://www.aptest.com/standards/htmldiff/htmldiff.pl",
// if there a publicly available Editor's Draft, this is the link
edDraftURI: "http://json-ld.org/spec/latest/json-ld-api/",
// 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: [],
// 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: "Dave Longley", url: "http://digitalbazaar.com/",
company: "Digital Bazaar", companyURL: "http://digitalbazaar.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: "JSON for Linking Data Community Group",
// URI of the public WG page
wgURI: "http://www.w3.org/community/json-ld/",
// 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: 4,
preProcess: [ preProc ],
alternateFormats: [ {uri: "diff-20120524.html", label: "diff to previous version"} ],
};
function _esc(s) {
s = s.replace(/&/g,'&');
s = s.replace(/>/g,'>');
s = s.replace(/"/g,'"');
s = s.replace(/</g,'<');
return s;
}
function updateExample(doc, content) {
// perform transformations to make it render and prettier
content = content.replace(/<!--/, '');
content = content.replace(/-->/, '');
content = _esc(content);
content = content.replace(/\*\*\*\*([^*]*)\*\*\*\*/g, '<span class="diff">$1</span>') ;
return content ;
}
//]]>
</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
approved for transfer into the RDF Working Group for
review with the intent to publish the
document along the W3C
Recommendation track. This specification has undergone significant
development, review, and changes during the course of the last 18
months and is being published as a Final Community Group Specification so that
it may gain wider review and feedback.
</p>
<p>
There are currently
<a href="http://json-ld.org/#impl">five 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 First Public Working Draft
does not require that all Working Group members agree on the content of the
document.
</p>
</section>
<section>
<h2>Preface</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>
<h2>Contributing</h2>
<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.</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>
<section>
<h1>Introduction</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
a programming environment like JavaScript, Python or Ruby.
</p>
<p>
There are three major types of transformation that are discussed in this
document; compaction, expansion, and RDF conversion.
</p>
<section>
<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">
var input1 = {
"@context": "http://json-ld.org/contexts/person.jsonld"
"name": "Manu Sporny",
"homepage": "http://manu.sporny.org/"
}
</pre>
<p>
The next input example uses one <tref>IRI</tref> to express a property, but
leaves the rest of the information untouched.
</p>
<pre class="example" data-transform="updateExample">
var input2 = {
"@context": "http://json-ld.org/contexts/person.jsonld"
"****http://xmlns.com/foaf/0.1/name****": "Manu Sporny",
"homepage": "http://manu.sporny.org/"
}
</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 are <tref>IRI</tref>s to their fully expanded form.
The second is to express all values in <tdef>expanded form</tdef>. To
transform both inputs above to the same representation, the developer could
do the following:
</p>
<pre class="example" data-transform="updateExample">
function expansionCallback(output) {
console.log(output);
}
// the second parameter is 'null' because the developer does not wish to
// inject another context value
jsonld.expand(input1, null, expansionCallback);
jsonld.expand(input2, null, expansionCallback);
</pre>
<p>
The output for both calls above will be:
</p>
<pre class="example" data-transform="updateExample">
[{
"http://xmlns.com/foaf/0.1/name": [{
"@value": "Manu Sporny"
}],
"http://xmlns.com/foaf/0.1/homepage": [{
"@id": "http://manu.sporny.org/"
}]
}]
</pre>
<p>
Note that in the example above; all <tref>context</tref> definitions have
been removed, all <tref>term</tref> and prefixes have been expanded to full
IRIs, and all <tref>literal</tref>s 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>
<h2>Compaction</h2>
<p>
While expansion expands a given input as much as possible, compaction performs
the opposite operation - expressing a given input as succinctly as possible.
While expansion is meant to produce something that is easy to process by
software programs, compaction is meant to produce something that is easy to
ready by software developers. Compaction uses a developer-supplied
<tref>context</tref> to compresses all <tref>IRI</tref>s to <tref>term</tref>s
or <tref>prefix</tref>es, and compacts all <tref>literal</tref>s expressed
in <tref>expanded form</tref> as much as possible.
</p>
<p>
The following example expresses input that has already been fully expanded:
</p>
<pre class="example" data-transform="updateExample">
var expandedInput = [{
"http://xmlns.com/foaf/0.1/name": [{
"@value": "Manu Sporny"
}],
"http://xmlns.com/foaf/0.1/homepage": [{
"@id": "http://manu.sporny.org/"
}]
}]
</pre>
<p>
A developer that wants to transform the data above into a more human-readable
form, could do the following using the JSON-LD API:
</p>
<pre class="example" data-transform="updateExample">
function compactionCallback(output) {
console.log(output);
}
jsonld.compact(expandedInput, "http://json-ld.org/contexts/person.jsonld", compactionCallback);
</pre>
<p>
The following would be the result of the call above:
</p>
<pre class="example" data-transform="updateExample">
{
"@context": "http://json-ld.org/contexts/person.jsonld"
"name": "Manu Sporny",
"homepage": "http://manu.sporny.org/"
}
</pre>
<p>
Note that all of the <tref>term</tref>s have been compressed and
the <tref>context</tref> has been injected into the output. While compacted
output is most useful to humans, it can also be carefully used to generate
structures that are easy to use for developers to program against as well.
</p>
</section>
<section>
<h3>Conversion to and from RDF</h3>
<p>
JSON-LD can be used to losslessly express the RDF data model as described in
the RDF Concepts document [[RDF-CONCEPTS]]. This ensures that
data can be round-tripped from any RDF syntax, like N-Triples or TURTLE,
without any loss in the fidelity of the data. Assume the following RDF input
in N-Triples format:
</p>
<pre class="example" data-transform="updateExample">
<!--
var data = "
<http://manu.sporny.org/about/#manu> <http://xmlns.com/foaf/0.1/name> \"Manu Sporny\" .\n
<http://manu.sporny.org/about/#manu> <http://xmlns.com/foaf/0.1/homepage> <http://manu.sporny.org/> .";
-->
</pre>
<p>
A developer can use the JSON-LD API to transform the markup above into a
JSON-LD document:
</p>
<pre class="example" data-transform="updateExample">
function conversionCallback(result)
{
console.log("JSON-LD Document: ", result);
};
jsonld.fromRDF(data, conversionCallback, {"format": "ntriples"});
</pre>
<p>
The following expanded output would be the result of the call above:
</p>
<pre class="example" data-transform="updateExample">
[{
"@id": "http://manu.sporny.org/about/#manu",
"http://xmlns.com/foaf/0.1/name": [{
"@value": "Manu Sporny"
}],
"http://xmlns.com/foaf/0.1/homepage": [{
"@id": "http://manu.sporny.org/"
}]
}]
</pre>
<p>
Note that the output above, could easily be compacted to produce the following
using the technique outlined in the previous section:
</p>
<pre class="example" data-transform="updateExample">
{
"@context": "http://json-ld.org/contexts/person.jsonld",
"@id": "http://manu.sporny.org/about/#manu",
"name": "Manu Sporny",
"homepage": "http://manu.sporny.org/"
}
</pre>
<p>
Transforming the object above back to RDF is as simple as calling the
<code>toRDF()</code> method:
</p>
<pre class="example" data-transform="updateExample">
var jsonldDocument = ...; // assign the JSON-LD document here
function rdfCallback(quads)
{
console.log("RDF Data: ", quads);
};
jsonld.toRDF(jsonldDocument, rdfCallback);
</pre>
</section>
<section>
<h3>Framing and Normalization</h3>
<p class="issue">
There are currently two other API methods that are in active development and
were not ready for publication at the time this document was published.
Framing allows a developer to force a different layout for the data and
effectively perform query-by-example on JSON-LD documents - this is most
useful when a JSON-LD-based REST API does not know the exact form of the data
it is getting in, but still wants to operate upon it if some bare essentials
are found in the data. JSON-LD normalization allows JSON-LD documents to be
deterministically serialized such that they can be digitally signed or be
used to find the differences between two <tref>linked data graph</tref>s.
It is expected that framing will be a part of the final API. It is expected
that normalization will be an optional feature that JSON-LD processors may
implement.
</p>
</section>
</section>
<section>
<h2>The Application Programming Interface</h2>
<p>This API provides a clean mechanism that enables developers to convert
JSON-LD data into a a variety of output formats that are easier to work
with in various programming languages. If a JSON-LD API is provided in
a programming environment, the entirety of the following API MUST be
implemented.
</p>
<section>
<h3>General Terminology</h3>
<p class="issue">
The intent of the Working Group and the Editors of this specification is to
eventually align terminology used in this document with the terminology used
in the RDF Concepts document to the extent to which it makes sense to do so.
In general, if there is an analogue to terminology used in this document in
the RDF Concepts document, the preference is to use the terminology in the
RDF Concepts document.
</p>
<p>
The following is an explanation of the general terminology used in this
document:
</p>
<dl>
<dt><tdef>JSON object</tdef></dt><dd>
An object structure is represented as a pair of curly brackets surrounding zero or
more name-value pairs. A name is a <tref>string</tref>. A single colon comes after
each name, separating the name from the value. A single comma separates a value
from a following name. The names within an object SHOULD be unique.
</dd>
<dt><tdef>array</tdef></dt>
<dd>
An array is represented as square brackets surrounding zero or more
values that are separated by commas.
</dd>
<dt><tdef>string</tdef></dt><dd>
A string is a sequence of zero or more Unicode (UTF-8) 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>
Unless otherwise specified, a JSON-LD processor MUST act as if a
key-value pair in the body of a JSON-LD document was never declared when
the value equals <em>null</em>. If <code>@value</code>,
<code>@list</code>, or <code>@set</code> is set to <em>null</em> in
expanded form, then the entire JSON object is ignored. If
<code>@context</code> is set to <em>null</em>, the
<tref>active context</tref> is reset and when used
within a <tref>context</tref>, it removes any definition associated
with the key, unless otherwise specified.
</dd>
<dt><tdef>subject definition</tdef></dt><dd>
A <tref>JSON object</tref> used to represent a <tref>subject</tref> and
one or more properties of that subject. A <tref>JSON object</tref> is a
subject definition if it does not contain they keys <code>@value</code>,
<code>@list</code> or <code>@set</code> and it has one or more keys
other than <code>@id</code>.</dd>
<dt><tdef>subject reference</tdef></dt><dd>
A <tref>JSON object</tref> used to reference a subject having only the
<code>@id</code> key.</dd>
<dt><tdef>Linked Data</tdef></dt>
<dd>A set of documents, each containing a representation of a
<tref>linked data graph</tref>.</dd>
<dt><tdef>linked data graph</tdef> or <tdef>dataset</tdef></dt>
<dd>An unordered labeled directed graph, where <tref>node</tref>s are
<tref>subject</tref>s or <tref>object</tref>s, and edges are
properties.</dd>
<dt><tdef>node</tdef></dt>
<dd>A piece of information that is represented in a
<tref>linked data graph</tref>.</dd>
<dt><tdef>named graph</tdef></dt>
<dd>A <tref>linked data graph</tref> that has one or more
<tref>IRI</tref> that are used to refer to it.</dd>
<dt><tdef>graph name</tdef></dt>
<dd>An <tref>IRI</tref> that is a reference to a
<tref>named graph</tref>.</dd>
<dt><tdef>default graph</tdef></dt>
<dd>When executing an algorithm, the graph where data should be placed
if a <tref>named graph</tref> is not specified.</dd>
<dt><tdef>subject</tdef></dt>
<dd>Any node in a <tref>linked data graph</tref> with at least one
outgoing edge.</dd>
<dt><tdef><abbr title="Internationalized Resource Identifier">IRI</abbr></tdef></dt>
<dd>An Internationalized Resource Identifier as described
in [[!RFC3987]]).</dd>
<dt><tdef>object</tdef></dt>
<dd>A node in a <tref>linked data graph</tref> with at least one
incoming edge.</dd>
<dt><tdef>property</tdef></dt>
<dd>An edge of the <tref>linked data graph</tref>.</dd>
<dt><tdef>literal</tdef></dt>
<dd>An <tref>object</tref> with a label that is not an
<tref>IRI</tref>.</dd>
<dt><tdef>quad</tdef></dt>
<dd>A piece of information that contains four items; a
<tref>subject</tref>, a <tref>property</tref>, a <tref>object</tref>,
and a <tref>graph name</tref>.</dd>
<dt><tdef>context</tdef></dt>
<dd>A <tref>JSON object</tref> that contains rules for interpreting a
JSON-LD document.</dd>
<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
<a href="http://sites.local/json-ld.org/spec/latest/json-ld-syntax/#syntax-tokens-and-keywords">Syntax Tokens and Keywords</a>.</dd>
</dl>
</section>
<section>
<h3>JsonLdProcessor</h3>
<p>
The JSON-LD Processor interface is the high-level programming structure that
developers use to access the JSON-LD transformation methods.
</p>
<dl title="[NoInterfaceObject] interface JsonLdProcessor" class="idl">
<dt>void expand()</dt>
<dd>
<a href="#expansion">Expands</a> the given <code>input</code> according to
the steps in the <a href="#expansion-algorithm">Expansion Algorithm</a>. The
<code>input</code> MUST be copied, expanded and returned if there are
no errors. If the expansion fails, an appropriate exception MUST be thrown.
<dl class="parameters">
<dt>object or object[] or URL input</dt>
<dd>The JSON-LD object or array of JSON-LD objects to perform the expansion upon or an
<tref>IRI</tref> referencing the JSON-LD document to expand.</dd>
<dt>object or URL? context</dt>
<dd>An optional external context to use additionally to the context embedded in
<code>input</code> when expanding the <code>input</code>.</dd>
<dt>JsonLdCallback callback</dt>
<dd>A callback that is called when processing is complete on
the given <code>input</code>.</dd>
<dt>optional JsonLdOptions options</dt>
<dd>A set of options that MAY affect the expansion algorithm such as, e.g., the
input document's base <tref>IRI</tref>.</dd>
</dl>
<dl class="exception" title="InvalidContext">
<dt>INVALID_SYNTAX</dt>
<dd>A general syntax error was detected in the <code>@context</code>.
For example, if a <code>@type</code> key maps to anything other than
<code>@id</code> or an <tref>absolute IRI</tref>, this exception would be raised.</dd>
<dt>LOAD_ERROR</dt>
<dd>There was a problem encountered loading a remote context.</dd>
</dl>
<dl class="exception" title="ProcessingError">
<dt>LIST_OF_LISTS_DETECTED</dt>
<dd>A list of lists was detected. This is not supported in this
version of JSON-LD.</dd>
</dl>
</dd>
<dt>void compact()</dt>
<dd>
<a href="#compaction">Compacts</a> the given <code>input</code> using the
<code>context</code> according to the steps in the
<a href="#compaction-algorithm">Compaction Algorithm</a>. The
<code>input</code> MUST be copied, compacted and returned if there are
no errors. If the compaction fails, an appropirate exception MUST be
thrown.
<dl class="parameters">
<dt>object or object[] or URL input</dt>
<dd>The JSON-LD object or array of JSON-LD objects to perform the compaction upon or an
<tref>IRI</tref> referencing the JSON-LD document to compact.</dd>
<dt>object or URL context</dt>
<dd>The context to use when compacting the <code>input</code>; either in the
form of an <tref>JSON object</tref> or as <tref>IRI</tref>.</dd>
<dt>JsonLdCallback callback</dt>
<dd>A callback that is called when processing is complete on
the given <code>input</code>.</dd>
<dt>optional JsonLdOptions options</dt>
<dd>A set of options that MAY affect the expansion algorithm such as, e.g., the
input document's base <tref>IRI</tref>. This also includes <code>optimize</code>,
which if set will cause processor-specific optimization.</dd>
</dl>
<dl class="exception" title="InvalidContext">
<dt>INVALID_SYNTAX</dt>
<dd>A general syntax error was detected in the <code>@context</code>.
For example, if a <code>@type</code> key maps to anything other than
<code>@id</code> or an <tref>absolute IRI</tref>, this exception would be raised.</dd>
<dt>LOAD_ERROR</dt>
<dd>There was a problem encountered loading a remote context.</dd>
</dl>
<dl class="exception" title="ProcessingError">
<dt>LOSSY_COMPACTION</dt>
<dd>The compaction would lead to a loss of information, such as a
<code>@language</code> value.</dd>
<dt>CONFLICTING_DATATYPES</dt>
<dd>The target datatype specified in the coercion rule and the
datatype for the typed literal do not match.</dd>
<dt>LIST_OF_LISTS_DETECTED</dt>
<dd>A list of lists was detected. This is not supported in this
version of JSON-LD.</dd>
</dl>
</dd>
<dt>void fromRDF()</dt>
<dd>Creates a JSON-LD document given an set of <ldtref title="quad">Quads</ldtref>.
<dl class="parameters">
<dt>Quad[] input</dt>
<dd>A <tref>dataset</tref> represented as an array of <ldtref title="quad">Quads</ldtref>.</dd>
<dt>JsonLdCallback callback</dt>
<dd>A callback that is called when processing is complete on
the given <code>input</code>.</dd>
<dt>optional JsonLdOptions options</dt>
<dd>A set of options that will affect the algorithm. This includes <code>notType</code>,
which if set to <tref>true</tref> causes the resulting document to use <code>rdf:type</code>
as a property, instead of <code>@type</code>.</dd>
</dl>
</dd>
<dt>void toRDF()</dt>
<dd>
Processes the <code>input</code> according to the
<a href="#convert-to-rdf-algorithm">Convert to RDF Algorithm</a>, calling
the provided <code>callback</code> for each <ldtref>Quad</ldtref> generated.
<dl class="parameters">
<dt>object or object[] or URL input</dt>
<dd>The JSON-LD object or array of JSON-LD objects to convert to RDF or a <a>URL</a>
referencing the JSON-LD document to convert to RDF.</dd>
<dt>QuadCallback callback</dt>
<dd>A callback that is called when a <ldtref>Quad</ldtref> is created from processing
the given <code>input</code>.
</dd>
<dt>optional JsonLdOptions options</dt>
<dd>A set of options that MAY affect the conversion to RDF such as, e.g.,
the input document's base <tref>IRI</tref>.</dd>
</dl>
<dl class="exception" title="InvalidContext">
<dt>INVALID_SYNTAX</dt>
<dd>A general syntax error was detected in the <code>@context</code>.
For example, if a <code>@type</code> key maps to anything other than
<code>@id</code> or an <tref>absolute IRI</tref>, this exception would be raised.</dd>
<dt>LOAD_ERROR</dt>
<dd>There was a problem encountered loading a remote context.</dd>
</dl>
<dl class="exception" title="ProcessingError">
<dt>LIST_OF_LISTS_DETECTED</dt>
<dd>A list of lists was detected. This is not supported in this
version of JSON-LD.</dd>
</dl>
</dd>
</dl>
</section>
<section>
<h3>Callbacks</h3>
<section>
<h3>JsonLdCallback</h3>
<p>The <a>JsonLdCallback</a> is used to return a processed JSON-LD representation
as the result of processing an API method.</p>
<dl title="[NoInterfaceObject Callback] interface JsonLdCallback"
class="idl">
<dt>void jsonLd()</dt>
<dd>This callback is invoked when processing is complete.
<dl class="parameters">
<dt>object or object[] jsonld</dt>
<dd>The processed JSON-LD document.</dd>
</dl>
</dd>
</dl>
</section>
<section>
<h3>QuadCallback</h3>
<p>The <a>QuadCallback</a> is called whenever the processor generates a
quad during the <code>quad()</code> call.</p>
<dl title="[NoInterfaceObject Callback] interface QuadCallback"
class="idl">
<dt>void quad()</dt>
<dd>This callback is invoked whenever a quad is generated by the processor.
<dl class="parameters">
<dt>Quad quad</dt>
<dd>The quad.</dd>
</dl>
</dd>
</dl>
</section>
</section>
<section>
<h3>Data Structures</h3>
<p>This section describes datatype definitions used within the JSON-LD API.</p>
<section>
<h3>URL</h3>
<p>The <a>URL</a> datatype is a string representation of an <tref>IRI</tref>.</p>
<div title="typedef DOMString URL" class="idl">
This datatype indicates that the <tref>IRI</tref> is interpreted as a Universal Resource
Locator
identifying a document, which when parsed as JSON yields either a <code>JSON object</code>
or <code>array</code>.
</div>
</section>
<section>
<h3>JsonLdOptions</h3>
<p>The <a>JsonLdOptions</a> type is used to convery a set of options to an interface method.</p>
<dl title="typedef object JsonLdOptions" class="idl">
<dt><a>URL</a> base</dt>
<dd>The Base IRI to use when expanding the document. This overrides the value of
<em>input</em> if it is a <a>URL</a> or if it is a <code>object</code> or <code>object[]</code>.</dd>
<dt>boolean optimize</dt>
<dd>If set to <code>true</code>, the JSON-LD processor is allowed to
optimize the output of the <a href="#compaction-algorithm">Compaction Algorithm</a>
to produce even compacter representations. The algorithm for compaction
optimization is beyond the scope of this specification and thus
not defined. Consequently, different implementations MAY implement
different optimization algorithms.</dd>
<dt>boolean noType</dt>
<dd>If set to <code>true</code>, the JSON-LD processor will not use the
<code>@type</code> property when generating the output, and will use the
expanded <code>rdf:type</code> IRI as the property instead of <code>@type</code>.</dd>
</dl>
</section>
<p>The following data structures are used for representing data about
RDF quads. They are used for normalization, <a>fromRDF</a>,
and from <a>toRDF</a> interfaces.
</p>
<section>
<h3>Quad</h3>
<p>The <a>Quad</a> interface represents an RDF Quad.
See [[!RDF-CONCEPTS]] definition for
<cite><a href="http://www.w3.org/TR/rdf11-concepts/#dfn-rdf-triple">RDF triple</a></cite>,
which most closely aligns to <a>Quad</a>.
</p>
<dl title="[NoInterfaceObject] interface Quad" class="idl">
<dt>readonly attribute Node subject</dt>
<dd>The <tref>subject</tref> associated with the <a>Quad</a>.</dd>
<dt>readonly attribute Node predicate</dt>
<dd>The predicate associated with the <a>Quad</a>.
Within JSON-LD, an <tdef>RDF predicate</tdef> is refered to as a
<tref>property</tref></dd>
<dt>readonly attribute Node object</dt>
<dd>The <tref>object</tref> associated with the <a>Quad</a>.</dd>
<dt>readonly attribute Node? graphName</dt>
<dd>If present, the name associated with the <a>Quad</a> identifying
it as a member of a <tref>named graph</tref>. If it is missing, the quad
is a member of the <tref>default graph</tref>.
<div class="issue">This element is at risk, and may be removed.</div>
</dd>
</dl>
</section>
<section>
<h3>Node</h3>
<p><ldtref>Node</ldtref> is the base class of <ldtref>IRI</ldtref>,
<ldtref>BlankNode</ldtref>, and <ldtref>Literal</ldtref>. It is the IDL
representation of a <tref>linked data graph</tref> <tref>node</tref>.</p>
<dl title="[NoInterfaceObject] interface Node" class="idl">
</dl>
</section>
<section>
<h3>IRI</h3>