diff --git a/source/_posts/json.md b/source/_posts/json.md index 1f1dd8d..d4e6247 100644 --- a/source/_posts/json.md +++ b/source/_posts/json.md @@ -3,19 +3,17 @@ title: JSON date: 2021-09-14 18:26:55 background: bg-[#646464] tags: - - config - - format + - config + - format categories: - - Programming + - Programming intro: | - This is a quick reference cheat sheet for understanding and writing JSON format configuration files. + This is a quick reference cheat sheet for understanding and writing JSON format configuration files. plugins: - - copyCode + - copyCode --- -Getting Started ---------------- - +## Getting Started ### Introduction @@ -26,10 +24,9 @@ Getting Started - JSON is language agnostic data-interchange format - JSON filename extension is `.json` - JSON Internet Media type is `application/json` + {.marker-round} - - ### Examples ```json @@ -41,17 +38,16 @@ Getting Started "salary": 70000, "married": true, "children": [ - {"name": "Tom", "age": 9, "gender":"M"}, - {"name": "Ava", "age": 7, "gender":"F"} + { "name": "Tom", "age": 9, "gender": "M" }, + { "name": "Ava", "age": 7, "gender": "F" } ] } ``` - ### Types | Type | Description | -|-----------|-----------------------------------------| +| --------- | :-------------------------------------- | | `Number` | Double precision floating-point | | `String` | Series of characters | | `Boolean` | `true` or `false` | @@ -60,12 +56,10 @@ Getting Started | `Object` | Unordered collection of key/value pairs | | `null` | Null or Empty | - - - ### String {.row-span-3} + | | | -|------|----------------------------| +| ---- | :------------------------- | | `\"` | Double quote | | `\\` | Backslash | | `\/` | Forward slash | @@ -77,47 +71,51 @@ Getting Started | `\u` | Trailed by four hex digits | #### Examples + ```json {.wrap} { "url": "https://cheatsheets.zip", - "msg" : "Hi,\n\"CheatSheets.zip\"", + "msg": "Hi,\n\"CheatSheets.zip\"", "intro": "Share quick reference and cheat sheet for developers." } ``` + #### Invalid String + ```json -{ "foo": 'bar' } +{ "foo": "bar" } ``` + Have to be delimited by double quotes - - ### Number {.row-span-2} | Type | Description | -|------------|----------------------------------------| +| ---------- | :------------------------------------- | | `Integer` | Digits 1-9, 0 and positive or negative | | `Fraction` | Fractions like 0.3, 3.9 | | `Exponent` | Exponent like e, e+, e-, E, E+, E | #### Examples + ```json { - "positive" : 12, - "negative" : -1, - "fraction" : 10.25, - "exponent" : 1.0E+2, - "zero" : 0 + "positive": 12, + "negative": -1, + "fraction": 10.25, + "exponent": 1.0e2, + "zero": 0 } ``` + #### Invalid Number + ```json -{ "foo": 0xFF } +{ "foo": 0xff } ``` + In JSON you can use only Decimal Literals - - ### Objects ```json @@ -135,28 +133,27 @@ In JSON you can use only Decimal Literals Multiple key/value pairs separated by a comma - - ### Arrays + ```json [1, 2, 3, 4, 5] ``` + Begins with `[` and ends with `]` - - ### Array of objects + ```json { "children": [ - {"name": "Jimmy Smith", "age": 15}, - {"name": "Sammy Sosa", "age": 12} + { "name": "Jimmy Smith", "age": 15 }, + { "name": "Sammy Sosa", "age": 12 } ] } ``` - ### Object of arrays + ```json { "attributes": ["a1", "a2"], @@ -165,8 +162,8 @@ Begins with `[` and ends with `]` } ``` - ### 2D Array + ```json { "my_sequences": [ @@ -178,7 +175,6 @@ Begins with `[` and ends with `]` } ``` - ### Object of objects ```json @@ -195,6 +191,7 @@ Begins with `[` and ends with `]` ``` ### Nested + ```json { "Jack": { @@ -203,148 +200,122 @@ Begins with `[` and ends with `]` "salary": 25000, "hobby": ["a", "b"], "location": { - "country": "A", "city": "A-A" + "country": "A", + "city": "A-A" } } } ``` - - -Access JSON in JavaScript -------------------------- +## Access JSON in JavaScript ### Access Object ```javascript let myObject = { - "name": "Jason", - "last": "Doe", - "age": 39, - "gender": "M", - "salary": 70000, - "married": true + name: "Jason", + last: "Doe", + age: 39, + gender: "M", + salary: 70000, + married: true, }; ``` ----- + +--- | | | -|--------------------|-----------| +| ------------------ | :-------- | | `myObject.name` | "Jason" | | `myObject["name"]` | "Jason" | | `myObject.age` | 39 | | `myObject.other` | undefined | | `myObject[0]` | undefined | - - ### Access Nested {.row-span-2} + ```javascript let myObject = { - "ref": { - "name": 0, - "last": 1, - "age": 2, - "gender": 3, - "salary": 4, - "married": 5 + ref: { + name: 0, + last: 1, + age: 2, + gender: 3, + salary: 4, + married: 5, }, - "jdoe": [ - "Jason", - "Doe", - 39, - "M", - 70000, - true - ], - "jsmith": [ - "Tom", - "Smith", - 42, - "F", - 80000, - true - ] + jdoe: ["Jason", "Doe", 39, "M", 70000, true], + jsmith: ["Tom", "Smith", 42, "F", 80000, true], }; ``` ----- + +--- | | | -|--------------------------|--------------------------| +| ------------------------ | :----------------------- | | `myObject.ref.age` | 2 | | `myObject["ref"]["age"]` | 2 | | `myObject.jdoe` | ["Jason", "Doe", 39 ...] | | `myObject.jsmith[3]` | "F" | | `myObject[1]` | undefined | - - ### Access Array of Objects {.row-span-2} + ```javascript let myArray = [ { - "name": "Jason", - "last": "Doe", - "age": 39, - "gender": "M", - "salary": 70000, - "married": true + name: "Jason", + last: "Doe", + age: 39, + gender: "M", + salary: 70000, + married: true, }, { - "name": "Tom", - "last": "Smith", - "age": 42, - "gender": "F", - "salary": 80000, - "married": true + name: "Tom", + last: "Smith", + age: 42, + gender: "F", + salary: 80000, + married: true, }, { - "name": "Amy", - "last": "Burnquist", - "age": 29, - "gender": "F", - "salary": 60000, - "married": false - } + name: "Amy", + last: "Burnquist", + age: 29, + gender: "F", + salary: 60000, + married: false, + }, ]; ``` ----- + +--- | | | -|---------------------|----------------------------| +| ------------------- | :------------------------- | | `myArray[0]` | `{`"name": "Jason", ...`}` | | `myArray[1].name` | "Tom" | | `myArray[1][2]` | 42 | | `myArray[3]` | undefined | | `myArray[3].gender` | TypeError: Cannot read... | - - - ### Access Array + ```javascript -let myArray = [ - "Jason", - "Doe", - 39, - "M", - 70000, - true -]; +let myArray = ["Jason", "Doe", 39, "M", 70000, true]; ``` ------ + +--- | | | -|--------------|-----------| +| ------------ | :-------- | | `myArray[1]` | "Doe" | | `myArray[5]` | true | | `myArray[6]` | undefined | +## Also see {.cols-1} - - -Also see --------- - [JSON](https://www.json.org/json-en.html) _(json.org)_ - [JSON Editor Online](http://jsoneditoronline.org/) _(jsoneditoronline.org)_ - [Convert JSON Array to Markdown Table, CSV and more](https://tableconvert.com/json-to-markdown) _(tableconvert.com)_