ECMAScript

JSON ECMAScript is standardized JavaScript. The current version is the 12th edition: Péter Jeszenszky , ECMAScript 2021 Language Specification, Standard ECMA-262, 12th ed., June 2021. https://www.ecma- international.org/publications-and-standards/standards/ecma-262/ October 8, 2021 The next version currently under development is ECMAScript 2022: ECMAScript 2022 Language Specification https://tc39.es/ecma262/

Péter Jeszenszky JSON October 8, 2021 1 / 94 Péter Jeszenszky JSON October 8, 2021 3 / 94

JSON (JavaScript Object Notation) JavaScript

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

Péter Jeszenszky JSON October 8, 2021 2 / 94 Péter Jeszenszky JSON October 8, 2021 4 / 94 JavaScript Engines (1) Node.js (1)

SpiderMonkey (written in: /C++; license: Mozilla Public License 2.0) https://spidermonkey.dev/ A JavaScript runtime environment built on the V8 JavaScript engine The JavaScript engine of the Mozilla Project. that is designed to build scalable network applications. V8 (written in: C++; license: New BSD License) https://v8.dev/ Website: https://nodejs.org/ https://github.com/nodejs/node https://github.com/v8/v8/ License: MIT License The JavaScript engine of Chromium. Written in: C++, JavaScript JavaScriptCore (written in: C++; license: LGPLv2) https://developer.apple.com/documentation/javascriptcore https: Platform: Linux, macOS, Windows //github.com/WebKit/webkit/tree/master/Source/JavaScriptCore The JavaScript engine developed for the WebKit rendering engine.

Péter Jeszenszky JSON October 8, 2021 5 / 94 Péter Jeszenszky JSON October 8, 2021 7 / 94

JavaScript Engines (2) Node.js (2)

Nashorn (written in: Java, license: GPLv2) https://openjdk.java.net/projects/nashorn/ Lets the developers to build applications in JavaScript that run outside a . JavaScript engine developed by Oracle that is included in JDK 8, JDK 9, and JDK 10 (see the package javax.script). Can be used for client-side and -side application development. GraalVM Community Edition (CE) (written in: Java; license: GPLv2) Its package ecosystem, , is the largest ecosystem of open source https://www.graalvm.org/ https://github.com/oracle/graal libraries in the world. Available on Linux, macOS, and Windows systems. See: http://www.modulecounts.com/ See: https://www.graalvm.org/docs/reference-manual/languages/js/

Péter Jeszenszky JSON October 8, 2021 6 / 94 Péter Jeszenszky JSON October 8, 2021 8 / 94 Node.js (3) Compatibility

“Hello, World!” example: Source: https://nodejs.org/en/docs/guides/getting-started-guide/ const http = require('http'); ECMAScript compatibility of implementations: const hostname = '127.0.0.1'; const port = 3000; ECMAScript Compatibility Tables https://kangax.github.io/compat-table/ const server = http.createServer((req, res) => { https://github.com/kangax/compat-table res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello, World!\n'); }); server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); });

Péter Jeszenszky JSON October 8, 2021 9 / 94 Péter Jeszenszky JSON October 8, 2021 11 / 94

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 technology (ICT), https://github.com/strongloop/express consumer electronics (CE) Meteor (license: MIT License) https://www.meteor.com/ Was founded in 1961 originally, operates under its current name since https://github.com/meteor/meteor 1994. Sails.js (license: MIT License) https://sailsjs.com/ European Computer Manufacturers Association (ECMA) https://github.com/balderdashy/sails Website: https://www.ecma-international.org/

Péter Jeszenszky JSON October 8, 2021 10 / 94 Péter Jeszenszky JSON October 8, 2021 12 / 94 History Specification

Discovered and popularized by . Was “discovered” in 2001. Crockford originally used JSON for communication between Ecma International, The JSON Data Interchange Format, Second JavaScript clients and Java servers. Edition, Standard ECMA-404, December 2017. https://www.ecma- international.org/publications-and-standards/standards/ecma-404/ IEEE Computer Society, Discovering JavaScript Object Notation with Douglas Crockford, 28 March 2012. T. Bray (ed.), The JavaScript Object Notation (JSON) Data https://www.youtube.com/watch?v=kc8BAR7SHJI Interchange Format, RFC 8259, December 2017. “I don’t claim to have invented it, because it already existed in nature. https://www.rfc-editor.org/rfc/rfc8259 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: https://inkdroid.org/2012/04/30/lessons-of-json/

Péter Jeszenszky JSON October 8, 2021 13 / 94 Péter Jeszenszky JSON October 8, 2021 15 / 94

File Properties JSON vs. ECMAScript

Starting with ECMAScript 2019, JSON is a syntactic subset of ECMAScript. File extension: .json See: IANA : application/json Subsume JSON a.k.a. JSON ￿ ECMAScript, 14 August 2019. https://v8.dev/features/subsume-json Axel Rauschmayer, Exploring ES2018 and ES2019 – JSON superset. https://exploringjs.com/es2018-es2019/ch_json-superset.html

Péter Jeszenszky JSON October 8, 2021 14 / 94 Péter Jeszenszky JSON October 8, 2021 16 / 94 JSON vs. XML (1) JSON vs. XML (3)

The main difference is that JSON is dataoriented, while XMLis JSON can be used as an alternative to XML for data exchange. document-oriented. Provides the same advantages as XML but without its disadvantages. JSON is the perfect choice for representing data structures. See: JSON: The Fat-Free Alternative to XML JSON is less verbose compared to XML. https://www.json.org/xml.html Use XML for document-oriented applications. XML is extensible and has a more mature infrastructure.

Péter Jeszenszky JSON October 8, 2021 17 / 94 Péter Jeszenszky JSON October 8, 2021 19 / 94

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

Example:

Common characteristics of JSON and XML: XML: Simplicity (undoubtedly JSON is the winner) The Dark Knight Can be read and written easily by humans 2008 https://www.imdb.com/title/tt0468569/ Can be generated and processed easily by computer programs false (undoubtedly JSON is the winner) JSON: Interoperability { "movie": { "title": "The Dark Knight", Self-describing data representation "year": 2008, "url": "https://www.imdb.com/title/tt0468569/", Universal data exchange format "standalone": false } }

Péter Jeszenszky JSON October 8, 2021 18 / 94 Péter Jeszenszky JSON October 8, 2021 20 / 94 JSON vs. XML (5) Tokens

XML:

JSON text is a sequence of tokens the conforms to the JSON value /home/jeszy grammar rule. jeszy Tokens: JSON: The {, }, [, ], : and , characters are structural tokens. { Strings "properties": { "property": [ Numbers { true, false and null are literal tokens. "@name": "user.home", "#text": "/home/jeszy" Whitespace characters are allowed before and after tokens and are }, insignificant. { "@name": "user.name", Whitespace characters: HT (U+0009), LF (U+000A), CR (U+000D), "#text": "jeszy" } (U+0020). ] Strings are the only tokens that can contain whitespace characters. } }

Péter Jeszenszky JSON October 8, 2021 21 / 94 Péter Jeszenszky JSON October 8, 2021 23 / 94

Data Types and Structures JSON Value

The following primitive types are available: strings numbers booleans null The following structured types are available: arrays objects

Péter Jeszenszky JSON October 8, 2021 22 / 94 Péter Jeszenszky JSON October 8, 2021 24 / 94 Numbers (1) Strings (1)

Sequences of characters delimited by quotation marks (U+0022). Can contain any characters with the following exceptions that must be escaped: Quotation mark (U+0022), (U+005C), control characters (U+0000–U+001F) Special characters can be specified using escape sequences, such as \", \\, \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"

Péter Jeszenszky JSON October 8, 2021 25 / 94 Péter Jeszenszky JSON October 8, 2021 27 / 94

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

Péter Jeszenszky JSON October 8, 2021 26 / 94 Péter Jeszenszky JSON October 8, 2021 28 / 94 Arrays (1) Objects (1)

Unordered collections of zero or more name/value pairs. Ordered sequences of zero or more values (can be empty). A name is a string, a value is a JSON value. Elements can be of any type (including arrays). Name/value pairs are also called as members.

Péter Jeszenszky JSON October 8, 2021 29 / 94 Péter Jeszenszky JSON October 8, 2021 31 / 94

Arrays (2) Objects (2)

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

Péter Jeszenszky JSON October 8, 2021 30 / 94 Péter Jeszenszky JSON October 8, 2021 32 / 94 Objects (3) Objects (5)

Examples: Example: Frankfurter https://www.frankfurter.app/ { E.g., https://api.frankfurter.app/latest?base=HUF "title": "Alien", "year": 1979, The Internet Chuck Norris https://www.icndb.com/ "rating": 8.5, E.g., https://api.icndb.com/jokes/random "votes": 643196, "genres": ["horror", "sci-fi"], Nominatim https://nominatim.openstreetmap.org/ "url": "http://www.imdb.com/title/tt0078748/" https://nominatim.org/ } E.g., https: //nominatim.openstreetmap.org/search?q=debrecen&format=json

Péter Jeszenszky JSON October 8, 2021 33 / 94 Péter Jeszenszky JSON October 8, 2021 35 / 94

Objects (4) Character Encoding

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

Péter Jeszenszky JSON October 8, 2021 34 / 94 Péter Jeszenszky JSON October 8, 2021 36 / 94 Viewing JSON in Web Browsers Editors (2)

Firefox: includes a built-in JSON viewer. Non-: See: https://developer.mozilla.org/en-US/docs/Tools/JSON_viewer XML Editor (platform: Linux, macOS, Windows) Chromium, Google Chrome: https://www.oxygenxml.com/ Recommended extensions: See: https://www.oxygenxml.com/xml_editor/json_editor.html JSON Formatter https://chrome.google.com/webstore/detail/json- IntelliJ IDEA (platform: Linux, macOS, Windows) formatter/bcjindcccaagfpapjjmafapmmgkkhgoa/ https://www.jetbrains.com/idea/ https://github.com/callumlocke/json-formatter JSON-handle https://chrome.google.com/webstore/detail/json- See: https://www.jetbrains.com/help/idea/json.html handle/iahnhfdhidomcpggpaimmmahffihkfnj/

Péter Jeszenszky JSON October 8, 2021 37 / 94 Péter Jeszenszky JSON October 8, 2021 39 / 94

Editors (1) Pretty-Printing JSON

Free and open source software: Pretty-printing JSON text on the command line: JSON Editor (written in: JavaScript; license: Apache License 2.0) https://github.com/josdejong/jsoneditor/ python -m json.tool cat | python -m json.tool Online: http://jsoneditoronline.org/ curl | python -m json.tool (platform: Linux, macOS, Windows; license: MIT Usage: License) https://code.visualstudio.com/ https://github.com/Microsoft/vscode python -m json.tool --help See: Editing JSON with Visual Studio Code See: https://docs.python.org/3/library/json.html#module-json.tool https://code.visualstudio.com/docs/languages/json

Péter Jeszenszky JSON October 8, 2021 38 / 94 Péter Jeszenszky JSON October 8, 2021 40 / 94 Libraries BSON (“Binary JSON”)

See: https://www.json.org/ C++: Binary data exchange format. nlohmann/json (license: MIT License) Used by the MongoDB NoSQL database system: https://github.com/nlohmann/json https://www.mongodb.org/ RapidJSON (license: MIT License) BSON is used for data storage and network transfer. https://github.com/miloyip/rapidjson Specification: https://bsonspec.org/ Java: Extends the types of JSON. (license: Apache License 2.0) https://github.com/google/gson Jackson (license: Apache License 2.0) For example, timestamps, regular expressions. https://github.com/FasterXML/jackson There is no number type, uses the int32, int64 and double types instead. Python: the json module is part of the standard library https://docs.python.org/3/library/json.html

Péter Jeszenszky JSON October 8, 2021 41 / 94 Péter Jeszenszky JSON October 8, 2021 43 / 94

JSON Extensions JSON Lines (1)

JSON5 https://json5.org/ https://github.com/json5/json5 A proposed extension to JSON that aims to make it easier for humans to write and maintain by hand. For example, adds new syntax features to JSON, such as comments; A line-oriented format for storing one JSON value per line. the names of members in objects can be strings or identifiers. Also called as -delimited JSON. Is a superset of JSON, but does not introduce new data types. Specification: The JSON5 Data Interchange Format Website: https://jsonlines.org/ https://spec.json5.org/ File extension: .jsonl YAML: YAML Ain’t Markup Language https://yaml.org/ See also: https://en.wikipedia.org/wiki/JSON_streaming Is a superset of JSON. Is more human readable than JSON, and offers a number of new features. Specification: YAML Ain’t Markup Language (YAML) Version 1.2 https://yaml.org/spec/1.2/spec.html

Péter Jeszenszky JSON October 8, 2021 42 / 94 Péter Jeszenszky JSON October 8, 2021 44 / 94 JSON Lines (2) JSON Lines (4)

Implementations: Examples: ["title", "year", "standalone"] Java: ["Batman Begins", 2005, false] ["The Dark Knight", 2008, false] Gson (license: Apache License 2.0) https://github.com/google/gson ["The Dark Knight Rises", 2012, false] The class com.google.gson.JsonStreamParser can be used to read data ["Interstellar", 2014, true] in JSON Lines format. { "title": "Batman Begins", "year": 2005, "standalone": false } Python: { "title": "The Dark Knight", "year": 2008, "standalone": false } { "title": "The Dark Knight Rises", "year": 2012, "standalone": false } jsonlines (license: New BSD License) { "title": "Interstellar", "year": 2014, "standalone": true } https://github.com/wbolster/jsonlines https://jsonlines.readthedocs.io/

Péter Jeszenszky JSON October 8, 2021 45 / 94 Péter Jeszenszky JSON October 8, 2021 47 / 94

JSON Lines (3) Applications (1)

Ajax (Asynchronous JavaScript and XML) Uses: https://jsonlines.org/on_the_web/ See: https://developer.mozilla.org/en-US/docs/Web/Guide/AJAX

Péter Jeszenszky JSON October 8, 2021 46 / 94 Péter Jeszenszky JSON October 8, 2021 48 / 94 Applications (2) Applications (4)

Storing configuration data: package.json: http://package.json.is/ Data interchange and storage: https://docs.npmjs.com/files/package.json Firefox: exporting and importing bookmarks npm https://www.npmjs.com/ https://github.com/npm/cli See: Restore bookmarks from backup or move them to another Grunt https://gruntjs.com/ https://github.com/gruntjs/grunt computer https://support.mozilla.org/en-US/kb/restore-bookmarks- from-backup-or-move-them See: https://gruntjs.com/getting-started#package.json Visual Studio Code https://code.visualstudio.com/ https://github.com/microsoft/vscode See: https://code.visualstudio.com/api/references/extension-manifest

Péter Jeszenszky JSON October 8, 2021 49 / 94 Péter Jeszenszky JSON October 8, 2021 51 / 94

Applications (3) Applications (5)

Data interchange and storage (continued): GeoJSON https://geojson.org/ Storing configuration data (continued): A format for encoding geographic data structures. Visual Studio Code (settings.json) Specification: Howard Butler, Martin Daly, Allan Doyle, Stefan Hagen, See: https://code.visualstudio.com/docs/getstarted/settings Tim Schaub, The GeoJSON Format, RFC 7946, August 2016. https://www.rfc-editor.org/rfc/rfc7946 WebExtensions (manifest.json) https://developer.mozilla.org/en- JSON-LD https://json-ld.org/ US/docs/Mozilla/Add-ons/WebExtensions A lightweight syntax to serialize Linked Data in JSON. See: https://developer.mozilla.org/en-US/docs/Mozilla/Add- Specification: JSON-LD 1.1: A JSON-based for Linked ons/WebExtensions/manifest.json Data (W3C Recommendation, 16 July 2020) https://www.w3.org/TR/json-ld/

Péter Jeszenszky JSON October 8, 2021 50 / 94 Péter Jeszenszky JSON October 8, 2021 52 / 94 Applications (6) JSON Schema (1)

Web services: Facebook for Developers https://developers.facebook.com/ A JSON-based schema language for validating JSON documents. Flickr API https://www.flickr.com/services/api/ Website: https://json-schema.org/ GitHub REST API https://docs.github.com/en/rest/ Current version: 2020-12 Nominatim API The most widely supported version is still draft-07 published in https://nominatim.org/release-docs/develop/api/Overview/ 2018. Twitter API https://developer.twitter.com/en/docs/twitter-api

Péter Jeszenszky JSON October 8, 2021 53 / 94 Péter Jeszenszky JSON October 8, 2021 55 / 94

Applications (7) JSON Schema (2) NoSQL : a number of document-oriented databases use JSON for storing data. Specifications: Apache CouchDB (written in: Erlang; platform: Linux, macOS, Austin Wright (ed.), Henry Andrews (ed.), Ben Hutton (ed.), Greg Windows; license: Apache License 2.0) https://couchdb.apache.org/ Dennis, JSON Schema: A Media Type for Describing JSON https://github.com/apache/couchdb Documents, January 28, 2020. EJDB2 (written in: C; platform: Android, iOS, Linux, macOS, http://json-schema.org/latest/json-schema-core.html Windows; license: MIT License) https://ejdb.org/ Austin Wright (ed.), Henry Andrews (ed.), Ben Hutton (ed.), JSON https://github.com/Softmotions/ejdb Schema Validation: A Vocabulary for Structural Validation of JSON, RethinkDB (written in: C++; platform: Linux, macOS; license: January 28, 2020. Apache License 2.0) https://rethinkdb.com/ http://json-schema.org/latest/json-schema-validation.html https://github.com/rethinkdb/rethinkdb Geraint Luff, Henry Andrews (ed.), Relative JSON Pointers, January UnQLite (written in: C; platform: Linux, macOS, Windows; license: 28, 2020. http://json-schema.org/latest/relative-json-pointer.html Simplified BSD License) https://unqlite.org/ https://github.com/symisc/unqlite

Péter Jeszenszky JSON October 8, 2021 54 / 94 Péter Jeszenszky JSON October 8, 2021 56 / 94 JSON Schema (3) JSON Schema (5)

Related specification: Paul C. Bryan (ed.), Kris Zyp, Mark Nottingham (ed.), JavaScript Property: a member of an object instance. Object Notation (JSON) Pointer, RFC 6901, April 2013. Keyword: a property of a schema object that is applied to an https://www.rfc-editor.org/rfc/rfc6901 instance. Defines a syntax for identifying a specific value inside aJSON Keywords assert constraints on JSON instances or annotate those document. instances with additional information. Examples: Examples: "properties", "type", "$ref" /country /places/0 /places/0/longitude

Péter Jeszenszky JSON October 8, 2021 57 / 94 Péter Jeszenszky JSON October 8, 2021 59 / 94

JSON Schema (4) JSON Schema (6)

JSON document: an information resource described by the Vocabulary: a set of keywords defined for a particular purpose. media type, i.e., a JSON value. application/json Keywords have well defined syntax and semantics. Instance : a JSON document to which a schema is applied. Each of the following specifications defines a vocabulary: Austin Wright (ed.), Henry Andrews (ed.), Ben Hutton (ed.), Greg JSON schema: a JSON document used to describe an instance. Dennis, JSON Schema: A Media Type for Describing JSON Documents, January 28, 2020. An object or a boolean. http://json-schema.org/latest/json-schema-core.html Can be nested one into another. Austin Wright (ed.), Henry Andrews (ed.), Ben Hutton (ed.), JSON The outermost schema is called the root schema, the others are called Schema Validation: A Vocabulary for Structural Validation of JSON, subschemas. January 28, 2020. Media type: application/schema+json http://json-schema.org/latest/json-schema-validation.html

Péter Jeszenszky JSON October 8, 2021 58 / 94 Péter Jeszenszky JSON October 8, 2021 60 / 94 JSON Schema (7) JSON Schema (9)

Implementations: https://json-schema.org/implementations.html C++: json-schema-validator (license: MIT License) Meta-schema: a schema that itself describes a schema. https://github.com/pboettch/json-schema-validator Example: JSON Schema meta-schema: http://json-schema.org/draft/2020-12/schema Java: http://json-schema.org/draft-07/schema json-schema (license: Apache License 2.0) https://github.com/everit-org/json-schema JavaScript: Ajv (license: MIT License) https://ajv.js.org/ https://github.com/epoberezkin/ajv

Péter Jeszenszky JSON October 8, 2021 61 / 94 Péter Jeszenszky JSON October 8, 2021 63 / 94

JSON Schema (8) JSON Schema (10) Tools: Free and open source software: Implementations: https://json-schema.org/implementations.html Visual Studio Code (platform: Linux, macOS, Windows; license: MIT .NET: License) https://code.visualstudio.com/ Json.NET Schema (license: AGPLv3) https://github.com/Microsoft/vscode http://www.newtonsoft.com/jsonschema See: Editing JSON with Visual Studio Code https://code.visualstudio.com/docs/languages/json https://github.com/JamesNK/Newtonsoft.Json.Schema Web interface: https://www.jsonschemavalidator.net/ Non-free software: Python: XML Editor (platform: Linux, macOS, Windows) jschon (license: MIT License) https://github.com/marksparkza/jschon https://www.oxygenxml.com/ Web interface: https://jschon.dev/ See: Editing JSON Schema Documents https://www.oxygenxml.com/doc/versions/23.1/ug- jsonschema (license: MIT License) editor/topics/editing-JSON-schema.html https://python-jsonschema.readthedocs.io/ IntelliJ IDEA (platform: Linux, macOS, Windows) https://github.com/Julian/jsonschema https://www.jetbrains.com/idea/ See: https://www.jetbrains.com/help/idea/json.html

Péter Jeszenszky JSON October 8, 2021 62 / 94 Péter Jeszenszky JSON October 8, 2021 64 / 94 JSON Schema (11) JSON Schema (13)

JSON Schema Store https://www.schemastore.org/json/ Further recommended reading: https://github.com/SchemaStore/schemastore Michael Droettboom, Understanding JSON Schema. A collection of schemas for commonly known JSON file formats. https://json-schema.org/understanding-json-schema/ Editor support: IntelliJ IDEA, , … https://github.com/json-schema-org/understanding-json-schema

Péter Jeszenszky JSON October 8, 2021 65 / 94 Péter Jeszenszky JSON October 8, 2021 67 / 94

JSON Schema (12) JSON Schema Examples (1)

Associating JSON schemas with JSON documents: JSON schema specifications do not provide any document level Schema: Schema: means. true false Implementation specific solutions: {} { "not": {} } Valid instances: any JSON Valid instances: none XML Editor: value Invalid instances: any JSON https://www.oxygenxml.com/doc/versions/23.1/ug-editor/topics/json- Invalid instances: none value associating-schema-directly-in-doc.html Visual Studio Code: https://code.visualstudio.com/docs/languages/json#_json-schemas- and-settings

Péter Jeszenszky JSON October 8, 2021 66 / 94 Péter Jeszenszky JSON October 8, 2021 68 / 94 JSON Schema Examples (2) JSON Schema Examples (4)

Schema: Schema: { { "type": "array", "type": "string" "items": { "type": "string" } } } Valid instances: Valid instances: "" [] "Hello, World!\n" ["Hello, World!\n"] ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]

Péter Jeszenszky JSON October 8, 2021 69 / 94 Péter Jeszenszky JSON October 8, 2021 71 / 94

JSON Schema Examples (3) JSON Schema Examples (5)

Schema: Schema: { { "type": "array", "type": ["string", "null"] "items": { "type": "string" }, } "minItems": 1, Valid instances: "uniqueItems": true } "" "Hello, World!\n" Valid instances: Invalid instances: null ["sci-fi"] [] ["crime", "drama"] ["sci-fi", "sci-fi"]

Péter Jeszenszky JSON October 8, 2021 70 / 94 Péter Jeszenszky JSON October 8, 2021 72 / 94 JSON Schema Examples (6) JSON Schema Examples (8)

Schema (continued): Schema: { { "$schema": "https://json-schema.org/draft/2020-12/schema", "$schema": "https://json-schema.org/draft/2020-12/schema", "type": "object", "type": "object", "properties": { "properties": { "name": { "type": "string" }, "name": { "type": "string" }, "age": { "type": "integer", "minimum": 0 }, "age": { "type": "integer" }, "email": { "type": "string", "format": "email" }, "email": { "type": "string" }, "webpage": { "type": "string", "format": "uri", "webpage": { "type": "string" } "pattern": "^http(s)?://.*" }, } "required": ["name", "age", "email"] }, } "required": ["name", "age", "email"] }

Péter Jeszenszky JSON October 8, 2021 73 / 94 Péter Jeszenszky JSON October 8, 2021 75 / 94

JSON Schema Examples (7) JSON Schema Examples (9)

Schema: { "$schema": "https://json-schema.org/draft/2020-12/schema", "type": "object", A valid instance (continued): "properties": { "title": { "type": "string" }, { "year": { "type": "integer" }, "name": "Douglas Crockford", "rating": { "type": "number", "minimum": 0, "maximum": 10 }, "votes": { "type": "integer", "minimum": 0 }, "age": 66, "genres": { "email": "[email protected]", "type": "array", "items": { "type": "string" }, "webpage": "https://www.crockford.com/" "minItems": 1, } "uniqueItems": true }, "url": { "type": "string", "format": "uri" } }, "required": ["title", "year", "rating", "votes", "genres", "url"], "additionalProperties": false }

Péter Jeszenszky JSON October 8, 2021 74 / 94 Péter Jeszenszky JSON October 8, 2021 76 / 94 JSON Schema Examples (10) JSON Schema Examples (12)

Schema: Schema: { { "$schema": "https://json-schema.org/draft/2020-12/schema", "$schema": "https://json-schema.org/draft/2020-12/schema", "type": "object", "type": "object", "properties": { "properties": { "artist": { "type": "string" }, "title": { "type": "string" }, "name": { "type": "string" }, "year": { "type": "integer" }, "born": { "$ref": "#/$defs/event" }, "tracks": { "died": { "$ref": "#/$defs/event" }, "type": "array", "items": { "gender": { "enum": ["female", "male"] } "type": "object", }, "properties": { "required": ["name", "born", "gender"], "title": { "type": "string" }, "length": { "type": "integer", "minimum": 0 } "$defs": { }, "event": { "required": ["title", "length"], "type": "object", "additionalProperties": false }, "properties": { "minItems": 1 "date": { "type": "string" }, } "place": { "type": "string" } }, "required": ["artist", "title", "year", "tracks"], } "additionalProperties": false } } } }

Péter Jeszenszky JSON October 8, 2021 77 / 94 Péter Jeszenszky JSON October 8, 2021 79 / 94

JSON Schema Examples (11) JSON Schema Examples (13) Schema: A valid instance (continued): { "$schema": "http://json-schema.org/2020-12/schema", { "type": "object", "name": "Edgar Allan Poe", "patternProperties": { "^[a-z]{2}$": { "type": "string" } "born": { }, "date": "1809-01-19", "additionalProperties": false, "place": "Boston, Massachusetts, United States" "minProperties": 2 }, } "died": { A valid instance: "date": "1849-10-07", "place": "Baltimore, Maryland, United States" { }, "en": "Lord of the Rings", "gender": "male" "de": "Der Herr der Ringe", "hu": "A gyűrűk ura" } }

Péter Jeszenszky JSON October 8, 2021 78 / 94 Péter Jeszenszky JSON October 8, 2021 80 / 94 JSON Schema Examples (14) JSON Schema Examples (16)

Schema: A valid instance (continued):

{ "$schema": "https://json-schema.org/draft/2020-12/schema", { "type": "object", "properties": { "name": "John F. Kennedy", "name": { "type": "string" }, "born": { "$ref": "#/$defs/event" }, "born": { "died": { "$ref": "#/$defs/event" }, "gender": { "enum": ["female", "male"] } "date": "1917-05-29", }, "required": ["name", "born", "gender"], "place": "Brookline, Massachusetts, United States" "$defs": { "event": { }, "type": "object", "properties": { "died": { "date": { "type": "string" }, "place": { "type": "string" } "date": "1963-11-22", }, "place": "Dallas, Texas, United States", "anyOf": [ { "required": ["date"] }, "cause": "assassination" { "required": ["place"] } ] }, } } "gender": "male" } }

Péter Jeszenszky JSON October 8, 2021 81 / 94 Péter Jeszenszky JSON October 8, 2021 83 / 94

JSON Schema Examples (15) JSON Schema Examples (17)

{ "$schema": "https://json-schema.org/draft/2020-12/schema", Schema: "type": "object", "properties": { { "name": { "type": "string" }, "$schema": "https://json-schema.org/draft/2020-12/schema", "born": { "$ref": "#/$defs/event" }, "type": "object", "died": { "properties": { "allOf": [ "type": { "enum": ["book", "serial"] }, { "$ref": "#/$defs/event" }, "title": { "type": "string" }, { "publisher": { "type": "string" } "properties": { }, "cause": { "type": "string" } "required": ["type", "title", "publisher"], }, "if": { "required": ["cause"] "properties": { "type": { "const": "book"} } } }, ] "then": { }, "properties": { "gender": { "enum": ["female", "male"] } "isbn": { "type": "string", "pattern": "^[0-9]{13}$" } }, }, "required": ["name", "born", "gender"], "required": ["isbn"] "$defs": { }, "event": { "else": { "type": "object", "properties": { "properties": { "issn": { "type": "string", "pattern": "^[0-9]{4}-[0-9]{3}[0-9X]$" } "date": { "type": "string" }, }, "place": { "type": "string" } "required": ["issn"] } } } } } }

Péter Jeszenszky JSON October 8, 2021 82 / 94 Péter Jeszenszky JSON October 8, 2021 84 / 94 JSON Schema Examples (18) JSON Schema Use Cases

Valid instances: { Web UI generation from JSON schema: "type": "book", Alpaca (written in: JavaScript; license: Apache License 2.0) "title": "The Hound of the Baskervilles", http://www.alpacajs.org/ https://github.com/gitana/alpaca "publisher": "Penguin Books", "isbn": "9780241952870" jsonforms (written in: TypeScript; license: MIT License) } https://jsonforms.io/ https://github.com/eclipsesource/jsonforms { react-jsonschema-form (written in: JavaScript; license: Apache "type": "serial", License 2.0) https://rjsf-team.github.io/react-jsonschema-form/ "title": "IEEE Internet of Things Journal", https://github.com/rjsf-team/react-jsonschema-form "publisher": "IEEE", See: https://json-schema.org/implementations.html#web-ui-generation "issn": "2327-4662" }

Péter Jeszenszky JSON October 8, 2021 85 / 94 Péter Jeszenszky JSON October 8, 2021 87 / 94

JSON Schema Examples (19) XML-JSON Conversion (1)

Real examples: GeoJSON: https://json.schemastore.org/geojson XML Editor https://www.oxygenxml.com/ manifest.json (WebExtensions): https://json.schemastore.org/webextension Tools JSON to XML... manifest.json (Chromium, Google Chrome): Tools XML to JSON... https://json.schemastore.org/chrome-manifest Visual Studio Code package.json (npm): https://json.schemastore.org/package Plugin: XML to JSON (license: MIT License) https://marketplace.visu alstudio.com/items?itemName=buianhthang.xml2json SWAPI – The Star Wars API https://swapi.dev/ https://swapi.dev/api/planets/schema https://swapi.dev/api/species/schema https://swapi.dev/api/starships/schema

Péter Jeszenszky JSON October 8, 2021 86 / 94 Péter Jeszenszky JSON October 8, 2021 88 / 94 XML-JSON Conversion (2) Query Languages (1)

JSON-java (written in: Java; license: JSON License) https://github.com/stleary/JSON-java XPath, XQuery: JSON support was added in the latest version The JSONObject() method of the XML class converts an XML (version 3.1). document to an equivalent JSON object. Further information: https://www.w3.org/XML/Query/ -js (written in: JavaScript; license: MIT License) https://github.com/nashwaan/xml-js

Péter Jeszenszky JSON October 8, 2021 89 / 94 Péter Jeszenszky JSON October 8, 2021 91 / 94

XML-JSON Conversion (3) Query Languages (2)

There a number of custom languages, I personally find the following of them promising: JSONiq https://www.jsoniq.org/

JsonML (JSON Markup Language) http://www.jsonml.org/ Declarative functional language based on XQuery for querying and processing JSON. https://github.com/mckamey/jsonml Implementations: Aims the lossless conversion of XML documents to JSON. Syntax: http://www.jsonml.org/syntax/ RumbleDB (written in: Java; license: Apache License 2.0) https://rumbledb.org/ https://github.com/RumbleDB/rumble JSONata (license: MIT License) https://jsonata.org/ https://github.com/jsonata-js/jsonata Query and transformation language inspired by the semantics of XPath 3.1 path expressions. A reference implementation in JavaScript.

Péter Jeszenszky JSON October 8, 2021 90 / 94 Péter Jeszenszky JSON October 8, 2021 92 / 94 Query Languages (3) Acknowledgments

There exist a number of custom languages, I personally find the following of them promising: (continued) jq (written in: C; platform: Linux, macOS, Windows; license: Expat License) https://stedolan.github.io/jq/ https://github.com/stedolan/jq Thanks for László Szathmáry for his remarks and for recommending jq. Command line JSON processor. JMESPath https://jmespath.org/ https://github.com/jmespath Implementations: Go, Java, JavaScript, Lua, .NET, PHP, Python, Ruby, Rust (license: MIT License) ObjectPath (written in: Python; license: MIT License) http://objectpath.org/ https://github.com/adriank/ObjectPath

Péter Jeszenszky JSON October 8, 2021 93 / 94 Péter Jeszenszky JSON October 8, 2021 94 / 94