0% found this document useful (0 votes)
78 views

Json En-2x2 PDF

Node.js is a JavaScript runtime environment that allows developers to build scalable network applications using JavaScript. It uses the V8 engine and includes various frameworks like Express. Node.js can be used for both client-side and server-side development and has the largest ecosystem of open source libraries.

Uploaded by

Daniel J. Santos
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

Json En-2x2 PDF

Node.js is a JavaScript runtime environment that allows developers to build scalable network applications using JavaScript. It uses the V8 engine and includes various frameworks like Express. Node.js can be used for both client-side and server-side development and has the largest ecosystem of open source libraries.

Uploaded by

Daniel J. Santos
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

JSON ECMAScript


ECMAScript is standardized JavaScript.

The current version is the 10th edition.
Péter Jeszenszky – Ecma International, ECMAScript 2019 Language
Faculty of Informatics, University of Debrecen Specification, Standard ECMA-262, 10th Edition,
[email protected] June 2019.
http://www.ecma-international.org/publications/sta
Last modified: November 25, 2019 ndards/Ecma-262.htm

The next version currently under development
is ECMAScript 2020.
– ECMAScript 2020 Language Specification
https://tc39.github.io/ecma262/ 3

JSON (JavaScript Object Notation) JavaScript



Lightweight, textual, and platform independent ●
The term JavaScript is used for the
data exchange format. implementations of ECMAScript by different
– Used for representing structured data. vendors.
– Can be read and written easily by humans. – See also: JavaScript technologies overview
https://developer.mozilla.org/en-US/docs/Web/Jav
– Can be generated and processed easily by aScript/JavaScript_technologies_overview
computer programs.

Originates from the ECMAScript programming
language.

Web page: http://www.json.org/
2 4
JavaScript Engines Node.js (2)
SpiderMonkey (programming language: C/C++; license: Mozilla Public License 2.0)
It lets the developers to build applications in


https://developer.mozilla.org/docs/Mozilla/Projects/SpiderMonkey

JavaScript that run outside a web browser.



The JavaScript engine of the Mozilla Project.

V8 (programming language: C++; license: New BSD License) https://developers.google.com/v8/
https://github.com/v8/v8/


– The JavaScript engine of Chromium.
Chakra: the JavaScript engine developed for the Microsoft Edge web browser.

It can be used for client-side and server-side
– Its core is available as free and open source software from early 2016 (license: MIT License).
https://github.com/Microsoft/ChakraCore application development.
– See also: Microsoft Edge’s JavaScript engine to go open-source


https://blogs.windows.com/msedgedev/2015/12/05/open-source-chakra-core/
Nashorn (programming language: Java, license: GPLv2) http://openjdk.java.net/projects/nashorn/

Its package ecosystem, npm, is the largest

– JavaScript engine developed by Oracle that is included in JDK 8, JDK 9, and JDK 10 (see the package javax.script).
GraalVM Community Edition (CE) (programming language: Java; license: GPL v2) https://www.graalvm.org/
ecosystem of open source libraries in the
https://github.com/oracle/graal
– Available on Linux and macOS systems.
world.
See: https://www.graalvm.org/docs/reference-manual/languages/js/
See: http://www.modulecounts.com/



5 7

Node.js (1) Node.js (3)



A JavaScript runtime environment built on the ●
“Hello, World!” example:
V8 JavaScript engine that is designed to build – See:
scalable network applications. https://nodejs.org/en/docs/guides/getting-started-g
uide/
– Website: https://nodejs.org/
const http = require('http');
https://github.com/nodejs/node
const hostname = '127.0.0.1';
– License: MIT License const port = 3000;
– Written in: C++, JavaScript const server = http.createServer((req, res) => {
res.statusCode = 200;
– Platform: Linux, macOS, Windows res.setHeader('Content-Type', 'text/plain');
res.end('Hello, World!\n');
});

server.listen(port, hostname, () => {


6 console.log(`Server running at http://${hostname}:${port}/`); 8
});
Node.js (4) ECMA International

Node.js frameworks: http://nodeframework.com/ ●
International non-profit standards organization.
– Express (license: MIT License) http://expressjs.com/ – Target domains: information and communication
https://github.com/strongloop/express technology (ICT), consumer electronics (CE)
– Meteor (license: MIT License) https://www.meteor.com/
https://github.com/meteor/meteor

Was founded in 1961 originally, operates
under its current name since 1994.
– Sails.js (license: MIT License) https://sailsjs.com/
https://github.com/balderdashy/sails – European Computer Manufacturers Association
– … (ECMA)

Web page: http://www.ecma-international.org/

9 11

Compatibility History

ECMAScript compatibility of implementations: ●
Discovered and popularized by Douglas Crockford.
– ECMAScript 2016+ compatibility table – Was “discovered” in 2001.
http://kangax.github.io/compat-table/es2016plus/ – Crockford originally used JSON for communication
between JavaScript clients and Java servers.
– IEEE Computer Society, Discovering JavaScript Object
Notation with Douglas Crockford, 28 March 2012.
https://www.youtube.com/watch?v=kc8BAR7SHJI

„I don’t claim to have invented it, because it already existed in
nature. I just saw it, recognized the value of it, gave it a name,
and a description, and showed its benefits. But I did not invent
it. I don’t claim to be the first person to have discovered it.”

Source:
10 https://inkdroid.org/2012/04/30/lessons-of-json/ 12
File Properties JSON vs. ECMAScript
● File extension: .json ●
JSON is based on the syntax of ECMAScript,
but is not fully compatible with it.
● IANA media type: application/json
– Further information:

https://developer.mozilla.org/en-US/docs/Web/JavaScri
pt/Reference/Global_Objects/JSON

Magnus Holm, JSON: The JavaScript subset that isn't,
2014.
http://timelessrepo.com/json-isnt-a-javascript-subset

13 15

Specification JSON vs. XML (1)



Ecma International, The JSON Data ●
JSON can be used as an alternative to XML
Interchange Format, Second Edition, Standard for data exchange.
ECMA-404, December 2017. – Provides the same advantages as XML but without
http://www.ecma-international.org/publications/ its disadvantages.
standards/Ecma-404.htm ●
Lásd: JSON: The Fat-Free Alternative to XML

T. Bray (ed.), The JavaScript Object Notation http://www.json.org/xml.html
(JSON) Data Interchange Format, RFC 8259,
December 2017.
https://tools.ietf.org/html/rfc8259

14 16
JSON vs. XML (2) JSON vs. XML (4)

Common characteristics of JSON and XML: ●
Example:
– Simplicity (undoubtedly JSON is the winner) – XML:
<file>
– Can be read and written easily by humans <uri>http://www.w3.org/Icons/w3c_home.png</uri>
<size>1936</size>
– Can be generated and processed easily by computer <contentType>image/png</contentType>
programs (undoubtedly JSON is the winner) <lastModified>2006-07-24T14:58:33Z</lastModified>
</file>
– Interoperability – JSON:
– Open standard {
"file": {
– Self-describing data representation "uri": "http://www.w3.org/Icons/w3c_home.png",
"size": 1936,
– Universal data exchange format "contentType": "image/png",
"lastModified": "2006-07-24T14:58:33Z"
}
17 } 19

JSON vs. XML (3) JSON vs. XML (5)



The main difference is that JSON is data- ●
Example:
XML:
oriented, while XML is document-oriented. –
<properties>
<property name="user.home">/home/jeszy</property>
– JSON is the perfect choice for representing data <property name="user.name">jeszy</property>
</properties>
structures. – JSON:

JSON is less verbose compared to XML. {
"properties": {
– Use XML for document-oriented applications. "property": [
{

XML is extensible and has a more mature infrastructure. "name": "user.home",
"content": "/home/jeszy"
},
{
"name": "user.name",
"content": "jeszy"
}
]
}
18 20
}
Data Types and Structures JSON Value

The following primitive types are available:
– strings
– numbers
– booleans
– null

The following structured types are available:
– arrays
– objects

21 23

Tokens Numbers (1)



JSON text is a sequence of tokens the conforms to the JSON
value grammar rule.

Tokens:
– The {, }, [, ], :and , characters are structural tokens.
– Strings
– Numbers
– true, false and null are literal tokens.

Whitespace characters are allowed before and after tokens
and are insignificant.
– Whitespace characters: HT (U+0009), LF (U+000A), CR (U+000D),
space (U+0020)
– Strings are the only tokens that can contain whitespace characters.
22 24
Numbers (2) Strings (2)

No constraints are imposed on the range and
precision of numbers.
– Implementations should consider interoperability.

For example, they should use double-precision
numbers.

Examples:
– 0
– -1.0
– 2.718282
– 1E-12
25 27

Strings (1) Arrays (1)



Sequences of Unicode characters delimited by quotation ●
Ordered sequences of zero or more values
marks (U+0022).
(can be empty).

Can contains any characters with the following exceptions
that must be escaped: – Elements can be of any type (including arrays).
– Quotation mark (U+0022), backslash (U+005C), control characters
(U+0000–U+001F)

Special characters can be specified using escape
sequences: \", \\, \t, \n, \r, …
● Unicode characters in the BMP can be specified as \unnnn,
where nnnn is the code point of the character encoded by
four hexadecimal digits.
● Examples: "", "\"Hello, world!\n\"", "\u263A", "\
u263a" 26 28
Arrays (2) Objects (2)

Examples: ●
RFC 8259:
– ["Athos", "Porthos", "Aramis", – Objects with unique names are interoperable.
"d'Artagnan"] – Objects with non-unique names can be handled
– [9, 14, 19, 25, 26, 28] differently by applications.
– ["Pi", 3.141593, null, true] – Implementations may or may not make the
– [[45.7370889, 16.1133866], ordering of members available.
[48.5852340, 22.8981217]]

29 31

Objects (1) Objects (3)



Unordered collections of zero or more ●
Example:
name/value pairs. {
"title": "Alien",
– A name is a string, a value is a JSON value. "year": 1979,
"rating": 8.5,
– Name/value pairs are also called as members. "votes": 643196,
"genres": ["horror", "sci-fi"],
"url": "http://www.imdb.com/title/tt0078748/"
}

30 32
Objects (4) Character Encoding

Example: ●
RFC 8259:
{
"artist": "Porcupine Tree", – JSON text exchanged between systems be
"title": "Fear of a Blank Planet",
"year": 2007,
encoded using UTF-8.
"tracks": [
{
"title": "Fear of a Blank Planet",
"length": 448
},
{
"title": "My Ashes",
"length": 307
},
{
"title": "Anesthetize",
"length": 1062
},
...
33 35
]
}

Objects (5) Display JSON in Web Browser



Example: ●
Chromium, Google Chrome:
– JSON Editor (license: MIT License)
– Zippopotamus – Zip Code Galore https://github.com/tulios/json-viewer
http://api.zippopotam.us/ ●
Microsoft Edge (EdgeHTML):

Obtaining information about the Hungarian ZIP code – JSON Formatter for Edge (license: MIT License)
4028: http://api.zippopotam.us/hu/4028 https://www.microsoft.com/en-us/store/p/json-formatter-for-edge/9nz
9d2j86w6s
– Nominatim https://nominatim.openstreetmap.org/ ●
Firefox:
https://nominatim.org/
– JSONView (license: MIT License) https://jsonview.com/

Example:
https://nominatim.openstreetmap.org/search?q=debrec

Opera:
en&format=json – JsonViewer (license: ?)
https://addons.opera.com/hu/extensions/details/jsonviewer/

34 36
JSON-P: Java API for JSON
Editors (1)
Processing

Free and open source software: ●
JSR 374: Java API for JSON Processing 1.1 (Final Release)
– Atom (platform: Linux, macOS, Windows; license: MIT License) https://atom.io/ https://www.jcp.org/en/jsr/detail?id=374
https://github.com/atom/atom

Recommended package: Pretty JSON https://atom.io/packages/pretty-json – Was introduced in Java EE 7 (JSR 353).
– Eclipse (platform: Linux, macOS, Windows; license: Eclipse Public License 2.0)
https://www.eclipse.org/
– See the javax.json, javax.json.spi and javax.json.stream

Recommended plug-ins: packages.
JavaScript Development Tools (JSDT) https://www.eclipse.org/webtools/jsdt/
Implementations:


– JSON Editor Plugin https://marketplace.eclipse.org/content/json-editor-plugin https://github.com/boothen/Json-Eclipse-Plugin
– JSON Editor (programming language: JavaScript; license: Apache License 2.0)
https://github.com/josdejong/jsoneditor/
– Reference implementation provided as part of the GlassFish application

Online: http://jsoneditoronline.org/
server: https://javaee.github.io/jsonp/
– Notepad++ (platform: Windows; license: GPLv2) https://notepad-plus-plus.org/ ●
License: GPLv2/Common Development and Distribution License (CDDL) Version 1.1
https://github.com/notepad-plus-plus/notepad-plus-plus ● Available in the Maven Central Repository: org.glassfish:javax.json:*.

Recommended plugin: JSToolNpp http://www.sunjw.us/jstool/npp/ https://github.com/sunjw/jstoolnpp
– Visual Studio Code (platform: Linux, macOS, Windows; license: MIT License)
– Apache Johnzon (license: Apache License 2.0) http://johnzon.apache.org/
https://code.visualstudio.com/ https://github.com/Microsoft/vscode ● Available in the Maven Central Repository: org.apache.johnzon:johnzon:*.

See: Editing JSON with Visual Studio Code https://code.visualstudio.com/docs/languages/json

37 39

Editors (2) JSON-B: Java API for JSON Binding


JSR 367: Java API for JSON Binding (JSON-B) (Final Release)

Non-free software: ●

https://jcp.org/en/jsr/detail?id=367
– <oXygen/> XML Editor (platform: Linux, macOS, – Was introduced in Java EE 8.
Windows) https://www.oxygenxml.com/ – See the javax.json.bind, javax.json.bind.adapter,
javax.json.bind.annotation, javax.json.bind.config,

See: https://www.oxygenxml.com/xml_editor/json_editor.html javax.json.bind.serializer and javax.json.bind.spi packages.
Web page: http://json-b.net/
IntelliJ IDEA (platform: Linux, macOS, Windows)


Implementations:
https://www.jetbrains.com/idea/

– Eclipse Yasson (license: Eclipse Public License 1.0)



See: https://www.jetbrains.com/help/idea/json.html https://github.com/eclipse-ee4j/yasson
The reference implementation, available in the Maven Central Repository:
XMLSpy XML Editor (platform: Windows)

– org.eclipse:yasson:*.
https://www.altova.com/xmlspy-xml-editor – Apache Johnzon (license: Apache License 2.0) http://johnzon.apache.org/johnzon-jsonb/
● Available in the Maven Central Repository: org.apache.johnzon:johnzon:*.

See: https://www.altova.com/xmlspy-xml-editor/json_editor

38 40
Libraries Gson (2)

See: http://json.org/ ●
Example:
– C++:
import java.math.BigDecimal;

nlohmann/json (license: MIT License) https://github.com/nlohmann/json import java.net.URL;

RapidJSON (license: MIT License) https://github.com/miloyip/rapidjson import java.util.List;
– Java:

Genson (license: Apache License 2.0) https://owlike.github.io/genson/ public class Movie {

Gson (license: Apache License 2.0) https://github.com/google/gson

Jackson (license: Apache License 2.0) private String title;
https://github.com/FasterXML/jackson private int year;
– Python: the json module is part of the standard library private BigDecimal rating;
https://docs.python.org/3/library/json.html private int votes;
private List<String> genres;
– R: private URL url;

jsonlite (license: MIT License) // ...
https://cran.r-project.org/web/packages/jsonlite/

rjson (license: GPLv2) https://cran.r-project.org/web/packages/rjson/ 41 } 43

Gson (1) Gson (3)



Example: ●
Example (continued):
import com.google.gson.Gson; import com.google.gson.Gson;
// ... import com.google.gson.GsonBuilder;
Gson gson = new Gson(); // ...
String s1 = gson.toJson(3.141593); // "3.141593" Gson gson = new GsonBuilder().setPrettyPrinting().create();
String s2 = gson.toJson("Hello, world!"); // "\"Hello, world!\""
String s3 = gson.toJson(false); // "false" Movie m = new Movie();
String s4 = gson.toJson(null); // "null" m.setTitle("Alien");
String s5 = gson.toJson(new int[] {3, 26, 31, 46, 66}); m.setYear(1979);
// "[3,26,31,46,66]" m.setRating(8.5);
m.setVotes(681290);
double result1 = gson.fromJson(s1, Double.class); m.setGenres(List.of("horror", "sci-fi"));
String result2 = gson.fromJson(s2, String.class); m.setUrl(new URL("http://www.imdb.com/title/tt0078748/"));
boolean result3 = gson.fromJson(s3, Boolean.class);
Object result4 = gson.fromJson(s4, Object.class); FileWriter writer = new FileWriter("alien.json");
int[] result5 = gson.fromJson(s5, int[].class); gson.toJson(m, writer);
writer.close();
// ...
42
Movie result = gson.fromJson(new FileReader("alien.json"), 44
Movie.class);
Gson (4) Gson (6)

Example (continued): the contents of the ●
Example (continued):
alien.json file import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
// ...
{ Gson gson = new GsonBuilder().setPrettyPrinting().create();
"title": "Alien",
"year": 1979, Album album = new Album();
"rating": 8.5, album.setArtist("Porcupine Tree");
"votes": 681290, album.setTitle("Fear of a Blank Planet");
album.setYear(2007);
"genres": [
"horror", ArrayList<Track> tracks = new ArrayList<Track>();
"sci-fi" tracks.add(new Track("Fear of a Blank Planet", 448));
], tracks.add(new Track("My Ashes", 307));
"url": "http://www.imdb.com/title/tt0078748/" tracks.add(new Track("Anesthetize", 1062));
album.setTracks(tracks);
}
String s = gson.toJson(album);
45
// ... 47
Album result = gson.fromJson(s, Album.class);

Gson (5) Gson (7)



Example: ●
Example: http://country.io/names.json
public class Album {
private String artist; {
private String title; "BD": "Bangladesh",
private int year; "BE": "Belgium",
private List<Track> tracks; "BF": "Burkina Faso",
// ...
} "BG": "Bulgaria",
"BA": "Bosnia and Herzegovina",
public class Track { "BB": "Barbados",
private String title; ...
private int length;
}
public Track(String title, int length) {
this.title = title;
this.length = length;
}
// ...
} 46 48
Gson (8) Gson (10)

Example (continued): ●
Example (continued):
import java.io.InputStreamReader; // PostCodeInfo.java:
import java.io.Reader; import com.google.gson.annotations.SerializedName;
public class PostCodeInfo {
import java.lang.reflect.Type; @SerializedName("post code") private String postCode;
import java.net.URL; private String country;
import java.util.Map; @SerializedName("country abbreviation") private String
countryAbbreviation;
import com.google.gson.Gson; private List<Place> places;
import com.google.gson.reflect.TypeToken; // ...
}
URL url = new URL("http://country.io/names.json");
// Place.java:
Reader reader = new InputStreamReader(url.openStream()); import com.google.gson.annotations.SerializedName;
Type type = new TypeToken<Map<String, String>>(){}.getType(); public class Place {
Map<String, String> map = new Gson().fromJson(reader, type); @SerializedName("place name") private String placeName;
private String state;
@SerializedName("state abbreviation") private String stateAbbreviation;
private Double longitude;
private Double latitude;
49 // ... 51
}

Gson (9) Gson (11)



Example: http://api.zippopotam.us/hu/4032 ●
Example (continued): Gson + Unirest
{ Unirest.config().setObjectMapper(new GsonObjectMapper());
"post code": "4032",
"country": "Hungary", HttpResponse<PostCodeInfo> response = Unirest.get(
"country abbreviation": "HU", "http://api.zippopotam.us/{countryCode}/{postCode}")
"places": [
{
.routeParam("countryCode", "hu")
"place name": "Debrecen", .routeParam("postCode", "4028")
"longitude": "21.24", .asObject(PostCodeInfo.class);
"state": "Hajd\u00fa-Bihar",
"state abbreviation": "HB", if (response.getStatus() != 200) {
"latitude": "47.7675" // Hiba
} // ...
] } else {
} PostCodeInfo pci = response.getBody();
// ...
}
50 52
import com.google.gson.TypeAdapter;
Gson (12) import
import
com.google.gson.stream.JsonReader;
com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;

Example: using a custom type adapter import java.time.ZoneId;

(java.time.LocalDate) public class LocalDateAdapter extends TypeAdapter<LocalDate> {

@Override
– See: java.time.LocalDate public void write(JsonWriter out, ZoneId value) throws IOException {
if (value == null) {
https://docs.oracle.com/en/java/javase/11/docs/api out.nullValue();
/java.base/java/time/LocalDate.html } else {
out.value(value.toString());
}
}
Gson gson = new Gson();
@Override
String s = gson.toJson(LocalDate.now()); public ZoneId read(JsonReader in) throws IOException {
System.out.println(s); if (in.peek() == JsonToken.NULL) {
in.nextNull();
// {"year":2019,"month":3,"day":15} return null;
}
return LocalDate.parse(in.nextString());
}

}
53 55

Gson (13) Gson (15)



Example (continued): using a custom type ●
Example (continued): using a custom type
adapter (java.time.LocalDate) adapter (java.time.LocalDate)
– See: import com.google.gson.Gson;

Gson User Guide – Custom Serialization and import com.google.gson.GsonBuilder;
Deserialization // ...
https://github.com/google/gson/blob/master/UserGuide. Gson gson = new GsonBuilder()
md#TOC-Custom-Serialization-and-Deserialization .registerTypeAdapter(LocalDate.class,
● com.google.gson.TypeAdapter new LocalDateIdAdapter())
https://javadoc.io/page/com.google.code.gson/gson/late .create();
st/com/google/gson/TypeAdapter.html
String s = gson.toJson(LocalDate.now());
System.out.println(s);
// "2019-03-15"
54 56
JSON Extensions Applications (1)

JSON5 https://json5.org/ ●
Ajax (Asynchronous JavaScript and XML), AJAJ (Asynchronous
– The JSON5 Data Interchange Format is a proposed extension to JSON that JavaScript and JSON)
aims to make it easier for humans to write and maintain by hand. – See: https://developer.mozilla.org/en-US/docs/Web/Guide/AJAX

Adding new syntax features to JSON, such as comments, the names of members in
objects can be strings or identifiers, … ●
GeoJSON http://geojson.org/
– Is a superset of JSON but does not introduce new data types. – Is a format for encoding geographic data structures.
– Specification: – Specification:

The JSON5 Data Interchange Format https://spec.json5.org/

Howard Butler, Martin Daly, Allan Doyle, Stefan Hagen, Tim Schaub, The

YAML: YAML Ain't Markup Language https://yaml.org/ GeoJSON Format, RFC 7946, August 2016. https://tools.ietf.org/html/rfc7946
– Is a superset of JSON. ●
JSON-LD https://json-ld.org/
– Is more human readable than JSON, offers a number of new features. – A lightweight syntax to serialize Linked Data in JSON.
– Specification: – Specification:

YAML Ain’t Markup Language (YAML) Version 1.2 https://yaml.org/spec/1.2/spec.html

Manu Sporny, Gregg Kellogg, Markus Lanthaler (eds.), JSON-LD 1.0: A JSON-
based Serialization for Linked Data, W3C Recommendation, 16 January 2014.
https://www.w3.org/TR/json-ld/
57 59

BSON („Binary JSON”) Applications (2)



Binary data exchange format. ●
NoSQL databases: a number of document-oriented databases use
JSON for storing data.
– Specification: http://bsonspec.org/ – Apache CouchDB (programming language: Erlang; platform: Linux, macOS,
Windows; license: Apache License 2.0) http://couchdb.apache.org/

Used by the MongoDB NoSQL database system. https://github.com/apache/couchdb
https://www.mongodb.org/ – EJDB2 (written in: C; license: MIT License) https://ejdb.org/
https://github.com/Softmotions/ejdb
– BSON is used for data storage and network transfer. – RethinkDB (programming language: C++; platform: Linux, macOS; license:

Extends the types of JSON. Apache License 2.0) https://rethinkdb.com/
https://github.com/rethinkdb/rethinkdb
– For example, timestamps, regular expressions, … – UnQLite (programming language: C; platform: Linux, macOS, Windows;
license: Simplified BSD License) https://unqlite.org/
– There is no number type, uses the int32, int64 and https://github.com/symisc/unqlite
double types instead. – …

58 60
Applications (3) Applications (5)

Web APIs: ●
Storing configuration data (continued):
– Facebook APIs – AWS CloudFormation
https://developers.facebook.com/docs/apis-and-sd https://aws.amazon.com/cloudformation/
ks – WebExtensions
– Flickr API https://www.flickr.com/services/api/ https://developer.mozilla.org/en-US/docs/Mozilla/A
– Nominatim API dd-ons/WebExtensions
https://nominatim.org/release-docs/develop/api/Ov ● See: manifest.json
erview/ https://developer.mozilla.org/en-US/docs/Mozilla/Add-on
s/WebExtensions/manifest.json
– reddit API https://www.reddit.com/dev/api – …
– …

61 63

Applications (4) JSON Schema (1)



Storing configuration data: ●
JSON-based schema language http://json-schema.org/
– package.json: http://package.json.is/

Current version: 2019-09
https://docs.npmjs.com/files/package.json – As far as I know, currently, it is supported by only one
implementation.

Uses:
– npm https://www.npmjs.com/ https://github.com/npm/cli

Previous version: draft-07
– Grunt https://gruntjs.com/ https://github.com/gruntjs/grunt – It has many implementations.

See: https://gruntjs.com/getting-started#package.json
– Visual Studio Code https://code.visualstudio.com

Migrating from draft-07 to 2019-09:
https://github.com/microsoft/vscode – JSON Schema 2019-09 Release Notes

See: https://json-schema.org/draft/2019-09/release-notes.html
https://code.visualstudio.com/api/references/extension-man
ifest

62 64
JSON Schema (2) JSON Schema (4)

Related specification: ●
JSON document: an information resource described by
the application/json media type, i.e., a JSON value.
– Paul C. Bryan (ed.), Kris Zyp, Mark Nottingham (ed.),
JavaScript Object Notation (JSON) Pointer, RFC ●
Instance: a JSON document to which a schema is
6901, April 2013. https://tools.ietf.org/html/rfc6901 applied.

Defines a syntax for identifying a specific value inside a ●
JSON schema:
JSON document. – A JSON document used to describe an instance.
– Examples:
● /country
– An object or a boolean.
● /places/0
– Can be nested one into another.
● /places/0/longitude ●
The outermost schema is called the root schema, the others are called

See: JSON Pointer Tester http://www.jsonpointer.com/ subschemas.
– IANA media type: application/schema+json

65 67

JSON Schema (3) JSON Schema (5)



Specifications: ●
Property: a member of an object instance.
– Austin Wright (ed.), Henry Andrews (ed.), Ben Hutton (ed.), Greg Dennis,

Keyword: a property of a schema object that is applied to an instance.
JSON Schema: A Media Type for Describing JSON Documents, – Keywords assert constraints on JSON instances or annotate those instances with additional
September 17, 2019. http://json-schema.org/latest/json-schema-core.html information.
– Examples: "properties", "type", "$ref"
– Austin Wright (ed.), Henry Andrews (ed.), Ben Hutton (ed.), JSON Schema
Validation: A Vocabulary for Structural Validation of JSON, September 17, ●
Vocabulary: a set of keywords defined for a particular purpose.
2019. http://json-schema.org/latest/json-schema-validation.html – Keywords have well defined syntax and semantics.
– Henry Andrews (ed), Austin Wright (ed.), JSON Hyper-Schema: A – Each of the following specifications defines a vocabulary:
Vocabulary for Hypermedia Annotation of JSON, September 17, 2019. ●
Austin Wright (ed.), Henry Andrews (ed.), Ben Hutton (ed.), Greg Dennis, JSON Schema: A Media Type
for Describing JSON Documents, September 17, 2019.
http://json-schema.org/latest/json-schema-hypermedia.html http://json-schema.org/latest/json-schema-core.html
– Geraint Luff, Henry Andrews (ed.), Relative JSON Pointers, September 17, ●
Austin Wright (ed.), Henry Andrews (ed.), Ben Hutton (ed.), JSON Schema Validation: A Vocabulary for
Structural Validation of JSON, September 17, 2019.
2019. http://json-schema.org/latest/relative-json-pointer.html http://json-schema.org/latest/json-schema-validation.html

Henry Andrews (ed), Austin Wright (ed.), JSON Hyper-Schema: A Vocabulary for Hypermedia
Annotation of JSON, September 17, 2019. http://json-schema.org/latest/json-schema-hypermedia.html

66 68
JSON Schema (6) JSON Schema (8)

Meta-schema: a schema that itself describes ●
Implementations: http://json-schema.org/implementations.html
C++:
a schema.


json-schema (license: Boost Software License 1.0)
https://github.com/KayEss/json-schema
– Example: – Java:

JSON Schema meta-schema: ●
json-schema (license: Apache License 2.0) https://github.com/everit-org/json-schema
– JavaScript:
– http://json-schema.org/draft/2019-09/schema ●
Ajv (license: MIT License) https://ajv.js.org/ https://github.com/epoberezkin/ajv
– http://json-schema.org/draft-07/schema – .NET:

Json.NET Schema (license: AGPLv3) http://www.newtonsoft.com/jsonschema
– Web interface: https://www.jsonschemavalidator.net/

Manatee.Json (license: MIT License) https://github.com/gregsdennis/Manatee.Json
– Python:

jsonschema (license: MIT License) https://github.com/Julian/jsonschema

69 71

JSON Schema (7) JSON Schema (9)



Tools: ●
JSON Schema Store
Free and open source software:
http://schemastore.org/json/


Visual Studio Code (platform: Linux, macOS, Windows; license: MIT License)
https://code.visualstudio.com/ https://github.com/Microsoft/vscode https://github.com/SchemaStore/schemastore
– See: Editing JSON with Visual Studio Code https://code.visualstudio.com/docs/languages/json
– Non-free software: – A collection of schemas for commonly known

<oXygen/> XML Editor (platform: Linux, macOS, Windows)
https://www.oxygenxml.com/
JSON file formats.
– See: Editing JSON Schema Documents
https://www.oxygenxml.com/doc/versions/21.1/ug-developerEclipse/topics/editing-JSON-schema.h
– Editor support: IntelliJ IDEA, Microsoft Visual
tml

IntelliJ IDEA (platform: Linux, macOS, Windows) https://www.jetbrains.com/idea/
Studio, …
– See: https://www.jetbrains.com/help/idea/json.html

XMLSpy XML Editor (platform: Windows) https://www.altova.com/xmlspy-xml-editor
– See: https://www.altova.com/xmlspy-xml-editor/json_schema_editor

70 72
JSON Schema (10) JSON Schema Examples (1)
Schema:
Associating JSON schemas with JSON Schema:
● ●

– true – false
documents: – {} – { "not": {} }
Valid instances: Valid instances:
JSON schema specifications do not provide any
● ●

42 None
means.
– –

– true ●
Invalid instances:
– Implementation specific solutions: – null – 42
– ["Gargamel", "Azrael"] – true

<oXygen/> XML Editor { "firstName": "Douglas",
– – null
https://www.oxygenxml.com/doc/versions/21.1/ug-editor "lastName": "Crockford" } ["Gargamel", "Azrael"]
/topics/json-associating-schema-directly-in-doc.html – …

– { "firstName": "Douglas",

Visual Studio Code: ●
Invalid instances: "lastName": "Crockford" }
https://code.visualstudio.com/docs/languages/json#_jso – None – …
n-schemas-and-settings

73 75

JSON Schema (11) JSON Schema Examples (2)


Schema:

Further recommended reading: ●

{
"type": "string"
– Michael Droettboom, Understanding JSON }
Schema, 2019. ●
Valid instances:
https://json-schema.org/understanding-json-schem – ""
a/index.html – "Hello, world!\n"
– …

Invalid instances:
– 42
– true
– null
– ["Gargamel", "Azrael"]
– { "firstName": "Douglas", "lastName": "Crockford" }
– …
74 76
JSON Schema Examples (3) JSON Schema Examples (5)
Schema:
Schema: Valid instances:

● ●
{
"type": "array",
"items": { "type": "string" }
{ – []
"type": "array",
} "items": { – ["Pi=",

Valid instances: "type": ["number", "string"]
– [] } 3.141593, "\n"]
}
– ["Hello, world!\n"] – …
– ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
– …

Invalid instances:
– "Hello, world!\n"
– 3.141593
– true
– null
– { "firstName": "Douglas", "lastName": "Crockford" }
– … 77 79

JSON Schema Examples (4) JSON Schema Examples (6)



Schema: ●
Valid instances: ●
Schema:
{ – ["sci-fi"] {
"type": "array", "$schema": "http://json-schema.org/draft-07/schema#",
"items": { "type": "string" },
– ["sci-fi", "horror"]
"type": "object",
"minItems": 1, – … "properties": {
"uniqueItems": true
"uri": { "type": "string" },
} ●
Invalid instances: "size": { "type": "integer" },
– [] "contentType": { "type": "string" },
"lastModified": { "type": "string" }
– ["a", "a"] },
– ["a", "b", "a"] "required": ["uri", "size", "contentType", "lastModified"]
}
– …

78 80
JSON Schema Examples (7) JSON Schema Examples (9)

Schema: ●
Schema:
{
{ "$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-07/schema#", "type": "object",
"type": "object", "properties": {
"properties": { "artist": { "type": "string" },
"uri": { "type": "string", "format": "uri" }, "title": { "type": "string" },
"size": { "year": { "type": "integer" },
"type": "integer", "minimum": 0 }, "tracks": {
"type": "array",
"contentType": { "items": {
"type": "string", "type": "object",
"pattern": "^[a-z0-9][a-z0-9!#$&\\-^_]*/[a-z0-9][a-z0-9!#$&\\-^_]*$" "properties": {
}, "title": { "type": "string" },
"lastModified": { "length": { "type": "integer", "minimum": 0 }
"type": "string", "format": "date-time" } },
"required": ["title", "length"],
},
"additionalProperties": false
"required": ["uri", "size", "contentType", "lastModified"] },
} "minItems": 1
}
},
"required": ["artist", "title", "year", "tracks"],
81 "additionalProperties": false 83
}

JSON Schema Examples (8) JSON Schema Examples (10)



Schema: ●
Schema:
{ {
"$schema": "http://json-schema.org/draft-07/schema#", "$schema": "http://json-schema.org/draft-07/schema#",
"type": "object", "type": "object",
"properties": { "properties": {
"title": { "type": "string" }, "name": { "type": "string" },
"year": { "type": "integer" }, "born": { "$ref": "#/definitions/event" },
"rating": { "type": "number", "minimum": 0, "maximum": 10 }, "died": { "$ref": "#/definitions/event" },
"votes": { "type": "integer", "minimum": 0 }, "gender": { "enum": ["female", "male"] }
"genres": { },
"type": "array", "required": ["name", "born", "gender"],
"items": { "type": "string" }, "definitions": {
"minItems": 1, "event": {
"uniqueItems": true "type": "object",
}, "properties": {
"url": { "type": "string", "format": "uri" } "date": { "type": "string" },
}, "place": { "type": "string" }
"required": ["title", "year", "rating", "votes", "genres", "url"], }
"additionalProperties": false }
} }
82 } 84
JSON Schema Examples (11) JSON Schema Examples (13)

A valid instance (continued): ●
Schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
{ "type": "object",
"name": "Edgar Allan Poe", "properties": {
"name": { "type": "string" },
"born": { "born": { "$ref": "#/definitions/event" },
"date": "1809-01-19", "died": {
"allOf": [
"place": "Boston, Massachusetts, United States" { "$ref": "#/definitions/event" },
}, {
"properties": {
"died": { "cause": { "type": "string" }
"date": "1849-10-07", },
"required": ["cause"]
"place": "Baltimore, Maryland, United States" }
}, ]
},
"gender": "male" "gender": { "enum": ["female", "male"] }
} },
"required": ["name", "born", "gender"],
"definitions": {
"event": {
"type": "object",
"properties": {
"date": { "type": "string" },
"place": { "type": "string" }
}
85 } 87
}
}

JSON Schema Examples (12) JSON Schema Examples (14)



Schema: ●
A valid instance (continued):
{
"$schema": "http://json-schema.org/draft-07/schema#", {
"type": "object", "name": "John F. Kennedy",
"properties": { "born": {
"name": { "type": "string" },
"born": { "$ref": "#/definitions/event" }, "date": "1917-05-29",
"died": { "$ref": "#/definitions/event" }, "place": "Brookline, Massachusetts, United States"
"gender": { "enum": ["female", "male"] } },
},
"required": ["name", "born", "gender"],
"died": {
"definitions": { "date": "1963-11-22",
"event": { "place": "Dallas, Texas, United States",
"type": "object", "cause": "assassination"
"properties": {
"date": { "type": "string" }, },
"place": { "type": "string" } "gender": "male"
}, }
"anyOf": [
{ "required": ["date"] },
{ "required": ["place"] }
]
}
} 86 88
}
JSON Schema Examples (15) JSON Schema Examples (17)

Schema: ●
Real examples:
{
"$schema": "http://json-schema.org/draft-07/schema#", – manifest.json (WebExtensions):
"type": "object",
"properties": {
http://json.schemastore.org/webextension
"$schema": { "type": "string" },
"type": { "enum": ["book", "serial"] },
– package.json (npm): http://json.schemastore.org/package
"title": { "type": "string" },
"publisher": { "type": "string" } – SWAPI – The Star Wars API https://swapi.co/
},
"required": ["type", "title", "publisher"], ●
https://swapi.co/api/planets/schema
"if": {
"properties": { "type": { "const": "book"} } ●
https://swapi.co/api/species/schema
},
"then": {

https://swapi.co/api/starships/schema
"properties": {
"isbn": { "type": "string", "pattern": "^[0-9]{13}$" }


},
"required": ["isbn"]
– tsconfig.json (TypeScript compiler configuration file):
},
"else": {
http://json.schemastore.org/tsconfig
"properties": {
"issn": { "type": "string", "pattern": "^[0-9]{4}-[0-9]{3}[0-9X]$" }
},
"required": ["issn"] 89 91
}
}

JSON Schema Examples (16) XML–JSON Conversion (1)



Valid instances: ●
Code Beautify https://codebeautify.org/
{ – JSON to XML Converter
"type": "book",
"title": "The Hound of the Baskervilles", https://codebeautify.org/jsontoxml
"publisher": "Penguin Books",
"isbn": " 9780241952870"
– XML to JSON Converter
} https://codebeautify.org/xmltojson
{ ●
<oXygen/> XML Editor
"type": "serial",
"title": "IEEE Internet of Things Journal", https://www.oxygenxml.com/
"publisher": "IEEE",
"issn": "2327-4662" – Tools → JSON to XML…
}
– Tools → XML to JSON…

90 92
XML–JSON Conversion (2) Query Languages

JSON-java (programming language: Java; ●
There exist a number of custom languages, I personally find the
following of them promising:
license: JSON License) – JSONiq http://www.jsoniq.org/
https://github.com/stleary/JSON-java ●
Declarative functional language based on XQuery for querying and processing JSON.

Implementation:
– The JSONObject() method of the XML class – Zorba – The NoSQL Query Processor (programming language: C++; platform: Linux, macOS,
Windows; license: Apache License 2.0) http://www.zorba.io/
converts an XML document to an equivalent JSON – jq (programming language: C; platform: Linux, macOS, Windows; license:
object. Expat License) https://stedolan.github.io/jq/

Command line JSON processor.

XSLTJSON (programming language: XSLT – JMESPath http://jmespath.org/
2.0; license: New BSD License) ●
Implementations: Go, Java, JavaScript, Lua, .NET, PHP, Python, Ruby, Rust (license:
MIT License)
https://github.com/bramstein/xsltjson – JSONata (license: MIT License) http://jsonata.org/

Query and transformation language inspired by the semantics of XPath 3.1 path
expressions.

JavaScript implementation.
93 95

XML–JSON Conversion (3) Acknowledgments



JsonML (JSON Markup Language)
http://www.jsonml.org/
https://github.com/mckamey/jsonml
– Aims the lossless conversion of XML documents
to JSON. Thanks for László Szathmáry
for his remarks and for recommending jq.
– Syntax: http://www.jsonml.org/syntax/

94 96

You might also like