JSON: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: JSON
| Old | New | Line | |
|---|---|---|---|
| 1 | + | ## Overview | |
| 2 | + | ||
| 3 | + | [JSON](http://www.json.org) (JavaScript Object Notation) is a form of data representation that provides a standardised data exchange format using simple text. As the name suggests, it is based on a subset of the [[JavaScript]] programming language; however, it is completely language-agnostic. Besides being easy for humans to read and write, it is also easy for machines to parse and generate. | |
| 4 | + | ||
| 5 | + | Compared to [[XML]], it is more human-readable. | |
| 6 | + | ||
| 7 | + | ## JSON Object | |
| 8 | + | ||
| 9 | + | A JSON Object is nothing more than a collection of comma-separated name/value pairs (often called members) enclosed in curly brackets: | |
| 10 | + | ||
| 11 | + | ``` javascript | |
| 12 | + | {"name1":value1, "name2":value2 ...} | |
| 13 | + | ``` | |
| 14 | + | ||
| 15 | + | To make it easier for humans to read and visualize, the name/value pairs are often listed vertically: | |
| 16 | + | ||
| 17 | + | ``` javascript | |
| 18 | + | { | |
| 19 | + | "name1": value1, | |
| 20 | + | "name2": value2 | |
| 21 | + | } | |
| 22 | + | ``` | |
| 23 | + | ||
| 24 | + | The name is supposed to be a string in double quotes, while the value can be any of the following: | |
| 25 | + | ||
| 26 | + | - a simple string, numeric, boolean or null value (strings are enclosed in double quotes):\ | |
| 27 | + | `{"id":1, "name":"John Doe", "married":false}` | |
| 28 | + | ||
| 29 | + | - an array; which is a collection of comma-separated values in square brackets:\ | |
| 30 | + | `{"primeNumbers":[2, 3, 5, 7, 11, 13, 17], "oddNumbers":[1,3,5,7]}` | |
| 31 | + | ||
| 32 | + | - an object; which is a collection of name:value pairs enclosed in curly brackets:\ | |
| 33 | + | `{"address":{"street":"145 Koinange Street", "City":"Nairobi", "Country":"Kenya"}}` | |
| 34 | + | ||
| 35 | + | JSON objects can always be nested arbitrarily to create even more complex objects: | |
| 36 | + | ||
| 37 | + | ``` javascript | |
| 38 | + | {"user": | |
| 39 | + | { "userid": 1900, | |
| 40 | + | "username": "jsmith", | |
| 41 | + | "password": "secret", | |
| 42 | + | "groups": [ "admins", "users", "maintainers"] | |
| 43 | + | } | |
| 44 | + | } | |
| 45 | + | ``` | |
| 46 | + | ||
| 47 | + | Check this [[fcl-json|link]] for extracting the information when you have several users. | |
| 48 | + | ||
| 49 | + | ## Valid JSON format | |
| 50 | + | ||
| 51 | + | There are a few rules and guidelines that define the JSON syntax (see RFC 4627): | |
| 52 | + | ||
| 53 | + | - JSON objects are encapsulated within opening and closing brackets `{ }`. An empty object can be represented by `{ }` | |
| 54 | + | - Arrays are encapsulated within opening and closing square brackets `[ ]`. An empty array can be represented by `[ ]` | |
| 55 | + | - A member is represented by a key-value pair | |
| 56 | + | - The key of a member should be contained in double quotes | |
| 57 | + | - Each member SHOULD have a unique key within an object structure | |
| 58 | + | - The value of a member must be contained in double quotes if it is a string | |
| 59 | + | - Boolean values are represented using the **true** or **false** literals in lower case | |
| 60 | + | - Number values are represented using double-precision floating-point format; Scientific notation is supported; Numbers should not have leading zeroes | |
| 61 | + | - "Offensive" characters in a string need to be escaped using the backslash character | |
| 62 | + | - Null values are represented by the **null** literal in lower case | |
| 63 | + | - Other object types, such as dates, are not natively supported and should be converted to strings; to be managed by the parser/client | |
| 64 | + | - Each member of an object or each array value must be followed by a comma if it is not the last one | |
| 65 | + | - The common extension for json files is **.json** | |
| 66 | + | - The MIME type for json files is **application/json** | |
| 67 | + | ||
| 68 | + | ## Implementations | |
| 69 | + | ||
| 70 | + | JSON implementation is not very strict, and a lot of leeway is granted to the client application and/or parser to enforce the guidelines. As usual when writing code: | |
| 71 | + | ||
| 72 | + | - be liberal in what you accept | |
| 73 | + | - be strict (adhering to the standard) in what you send. | |
| 74 | + | ||
| 75 | + | There are two main implementations of JSON: | |
| 76 | + | ||
| 77 | + | ### The official JSON Specification syntax | |
| 78 | + | ||
| 79 | + | This implementation adheres strictly to the RFC 4627 guidelines above and does not allow deviation from the specification. | |
| 80 | + | ||
| 81 | + | ### The Javascript syntax | |
| 82 | + | ||
| 83 | + | This implementation follows the implementation of the Javascript programming language and as such allows for a few deviations from the official specification. For example: | |
| 84 | + | ||
| 85 | + | 1. allows un-quoted keys eg `{name: "John Doe" }` | |
| 86 | + | 2. allows single quotes for keys and/or string values; and a liberal mix of single and double quotes eg `{'name': "John Doe", "language":'Pascal'}` | |
| 87 | + | 3. allows trailing comma after the last member of an array and/or object eg `{"keywords":["if","begin","for",], "IDEs":["Lazarus","fpIDE","MSEide"],}` | |
| 88 | + | ||
| 89 | + | ## See also | |
| 90 | + | ||
| 91 | + | - [[fcl-json]] package that implements JSON for Free Pascal and Lazarus | |
| 92 | + | - [[Streaming JSON]] - (English translation from original German wiki page) | |
| 93 | + | ||
| 94 | + | ## External links | |
| 95 | + | ||
| 96 | + | - [Official JSON website](http://www.json.org) | |
| 97 | + | - [JSON Formatter](https://jsonformatter.org) | |
| 98 | + | ||
| 99 | + | --- | |
| 100 | + | *From the Free Pascal wiki, page "JSON", as it stood on 2021-06-08 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* | |
| 101 | + | Category: JSON |
