kristofer revisó este gist . Ir a la revisión
1 file changed, 8 insertions, 8 deletions
json.md
| @@ -14,19 +14,19 @@ JSON (JavaScript Object Notation) is a __text-based data exchange format__. | |||
| 14 | 14 | It is a collection of key-value pairs where the key must be a string type, and | |
| 15 | 15 | the value can be of any of the following types: | |
| 16 | 16 | ||
| 17 | - | - Number | |
| 18 | - | - String | |
| 19 | - | - Boolean | |
| 20 | - | - Array | |
| 21 | - | - Object | |
| 22 | - | - null | |
| 17 | + | - Number `{“age”: 45}` | |
| 18 | + | - String `{"school": "ZipCode"}` | |
| 19 | + | - Boolean `{“active”:true}` | |
| 20 | + | - Array `{ "nums": [ 13, 17, 19, 23 ] }` | |
| 21 | + | - Object `{ "spouse":{"firstName":"John","lastName":"Smith" } }` | |
| 22 | + | - null `{"weddingDate": null}` | |
| 23 | 23 | ||
| 24 | 24 | A couple of important rules to note: | |
| 25 | 25 | ||
| 26 | - | - In the JSON data format, the keys must be enclosed in double quotes. | |
| 26 | + | - In the JSON data format, the keys must be enclosed in double quotes as strings. | |
| 27 | 27 | - The key and value must be separated by a colon (:) symbol. | |
| 28 | 28 | - There can be multiple key-value pairs. Two key-value pairs must be separated by a comma (,) symbol. | |
| 29 | - | - No comments (// or /* */) are allowed in JSON data. (But you can get around that, if you're curious.) | |
| 29 | + | - No comments (// or /* */) are allowed in JSON data. | |
| 30 | 30 | ||
| 31 | 31 | ## Usage | |
| 32 | 32 | ||
kristofer revisó este gist . Ir a la revisión
1 file changed, 7 insertions, 4 deletions
json.md
| @@ -4,13 +4,15 @@ JavaScript Object Notation (JSON) is a standard text-based format for representi | |||
| 4 | 4 | based on JavaScript object syntax. | |
| 5 | 5 | It is commonly used for transmitting data in web applications (e.g., sending some data | |
| 6 | 6 | from the server to the client, so it can be displayed on a web page, or vice versa). | |
| 7 | - | You'll come across it quite often, so in this article, we give you all you need to | |
| 8 | - | work with JSON using JavaScript, including parsing JSON so you can access data within it, and creating JSON. | |
| 7 | + | You'll come across it quite often. | |
| 8 | + | Better get a handle on it. | |
| 9 | + | ||
| 9 | 10 | ||
| 10 | 11 | ## Data Types | |
| 11 | 12 | ||
| 12 | 13 | JSON (JavaScript Object Notation) is a __text-based data exchange format__. | |
| 13 | - | It is a collection of key-value pairs where the key must be a string type, and the value can be of any of the following types: | |
| 14 | + | It is a collection of key-value pairs where the key must be a string type, and | |
| 15 | + | the value can be of any of the following types: | |
| 14 | 16 | ||
| 15 | 17 | - Number | |
| 16 | 18 | - String | |
| @@ -29,7 +31,8 @@ A couple of important rules to note: | |||
| 29 | 31 | ## Usage | |
| 30 | 32 | ||
| 31 | 33 | Often, you may read or write it to files, for configuration purposes, small data sets, and useful structured data. | |
| 32 | - | It is a common, go-to data format. Most languages have great support for reading and writing it, like Java and Python. | |
| 34 | + | It is a common, go-to data format. Most languages have great support for reading and writing it, | |
| 35 | + | like Java and Python. | |
| 33 | 36 | ||
| 34 | 37 | JSON exists as a string — useful when you want to transmit data across a network. | |
| 35 | 38 | It needs to be converted to a native object when you want to access the data within a program. | |
kristofer revisó este gist . Ir a la revisión
1 file changed, 3 insertions, 3 deletions
json.md
| @@ -97,7 +97,7 @@ It has a great set of syntax flow diagrams you can use to under stand it. | |||
| 97 | 97 | ||
| 98 | 98 | ## JSON Examples | |
| 99 | 99 | ||
| 100 | - | Maybe something that came from a bloggin system. | |
| 100 | + | Maybe something that came from a bloggin' system. | |
| 101 | 101 | ||
| 102 | 102 | ```json | |
| 103 | 103 | { | |
| @@ -112,7 +112,7 @@ Maybe something that came from a bloggin system. | |||
| 112 | 112 | } | |
| 113 | 113 | ``` | |
| 114 | 114 | ||
| 115 | - | Or this one, a example of something whihc might have come from a database: | |
| 115 | + | Or this one, a example of something which might have come from a database: | |
| 116 | 116 | ||
| 117 | 117 | ```json | |
| 118 | 118 | { | |
| @@ -129,7 +129,7 @@ Or this one, a example of something whihc might have come from a database: | |||
| 129 | 129 | } | |
| 130 | 130 | ``` | |
| 131 | 131 | ||
| 132 | - | An array: | |
| 132 | + | An array of employees: | |
| 133 | 133 | ||
| 134 | 134 | ```json | |
| 135 | 135 | { | |
kristofer revisó este gist . Ir a la revisión
1 file changed, 19 insertions, 13 deletions
json.md
| @@ -34,7 +34,19 @@ It is a common, go-to data format. Most languages have great support for reading | |||
| 34 | 34 | JSON exists as a string — useful when you want to transmit data across a network. | |
| 35 | 35 | It needs to be converted to a native object when you want to access the data within a program. | |
| 36 | 36 | ||
| 37 | - | Example of [python reading of file containing JSON. | |
| 37 | + | ||
| 38 | + | A simple object | |
| 39 | + | ||
| 40 | + | ```json | |
| 41 | + | { | |
| 42 | + | "fruit": "Apple", | |
| 43 | + | "size": "Large", | |
| 44 | + | "color": "Red", | |
| 45 | + | "nums": [ 13, 17, 19, 23 ] | |
| 46 | + | } | |
| 47 | + | ``` | |
| 48 | + | ||
| 49 | + | Example of python reading of file containing JSON. | |
| 38 | 50 | ||
| 39 | 51 | ```python | |
| 40 | 52 | # Python program to read | |
| @@ -48,9 +60,12 @@ f = open('data.json') | |||
| 48 | 60 | # returns JSON object as a dictionary | |
| 49 | 61 | data = json.load(f) | |
| 50 | 62 | ||
| 63 | + | print(data['fruit']) # 'Apple' | |
| 64 | + | ||
| 65 | + | ||
| 51 | 66 | # Iterating through the json list | |
| 52 | - | for i in data['some-array-key']: # in data | |
| 53 | - | print(i) | |
| 67 | + | for i in data['nums']: # in data | |
| 68 | + | print(i) # 13, ... | |
| 54 | 69 | ||
| 55 | 70 | # Closing file | |
| 56 | 71 | f.close() | |
| @@ -75,22 +90,13 @@ import json | |||
| 75 | 90 | with open('data.json', 'w', encoding='utf-8') as f: | |
| 76 | 91 | json.dump(data, f, ensure_ascii=False, indent=4) | |
| 77 | 92 | ``` | |
| 93 | + | Which will ofrmat the JSON for human reading. | |
| 78 | 94 | ||
| 79 | 95 | Check out: [JSON (JavaScript Object Notation)](https://www.json.org/json-en.html) | |
| 80 | 96 | It has a great set of syntax flow diagrams you can use to under stand it. | |
| 81 | 97 | ||
| 82 | 98 | ## JSON Examples | |
| 83 | 99 | ||
| 84 | - | A simple object | |
| 85 | - | ||
| 86 | - | ```json | |
| 87 | - | { | |
| 88 | - | "fruit": "Apple", | |
| 89 | - | "size": "Large", | |
| 90 | - | "color": "Red" | |
| 91 | - | } | |
| 92 | - | ``` | |
| 93 | - | ||
| 94 | 100 | Maybe something that came from a bloggin system. | |
| 95 | 101 | ||
| 96 | 102 | ```json | |
kristofer revisó este gist . Ir a la revisión
Sin cambios
kristofer revisó este gist . Ir a la revisión
1 file changed, 4 insertions, 6 deletions
json.md
| @@ -36,7 +36,7 @@ It needs to be converted to a native object when you want to access the data wit | |||
| 36 | 36 | ||
| 37 | 37 | Example of [python reading of file containing JSON. | |
| 38 | 38 | ||
| 39 | - | ```json | |
| 39 | + | ```python | |
| 40 | 40 | # Python program to read | |
| 41 | 41 | # json file | |
| 42 | 42 | ||
| @@ -45,13 +45,11 @@ import json | |||
| 45 | 45 | # Opening JSON file | |
| 46 | 46 | f = open('data.json') | |
| 47 | 47 | ||
| 48 | - | # returns JSON object as | |
| 49 | - | # a dictionary | |
| 48 | + | # returns JSON object as a dictionary | |
| 50 | 49 | data = json.load(f) | |
| 51 | 50 | ||
| 52 | - | # Iterating through the json | |
| 53 | - | # list | |
| 54 | - | for i in data['emp_details']: | |
| 51 | + | # Iterating through the json list | |
| 52 | + | for i in data['some-array-key']: # in data | |
| 55 | 53 | print(i) | |
| 56 | 54 | ||
| 57 | 55 | # Closing file | |
kristofer revisó este gist . Ir a la revisión
1 file changed, 149 insertions
json.md
| @@ -1,5 +1,154 @@ | |||
| 1 | 1 | # JSON Fundamentals | |
| 2 | 2 | ||
| 3 | + | JavaScript Object Notation (JSON) is a standard text-based format for representing structured data | |
| 4 | + | based on JavaScript object syntax. | |
| 5 | + | It is commonly used for transmitting data in web applications (e.g., sending some data | |
| 6 | + | from the server to the client, so it can be displayed on a web page, or vice versa). | |
| 7 | + | You'll come across it quite often, so in this article, we give you all you need to | |
| 8 | + | work with JSON using JavaScript, including parsing JSON so you can access data within it, and creating JSON. | |
| 9 | + | ||
| 10 | + | ## Data Types | |
| 11 | + | ||
| 12 | + | JSON (JavaScript Object Notation) is a __text-based data exchange format__. | |
| 13 | + | It is a collection of key-value pairs where the key must be a string type, and the value can be of any of the following types: | |
| 14 | + | ||
| 15 | + | - Number | |
| 16 | + | - String | |
| 17 | + | - Boolean | |
| 18 | + | - Array | |
| 19 | + | - Object | |
| 20 | + | - null | |
| 21 | + | ||
| 22 | + | A couple of important rules to note: | |
| 23 | + | ||
| 24 | + | - In the JSON data format, the keys must be enclosed in double quotes. | |
| 25 | + | - The key and value must be separated by a colon (:) symbol. | |
| 26 | + | - There can be multiple key-value pairs. Two key-value pairs must be separated by a comma (,) symbol. | |
| 27 | + | - No comments (// or /* */) are allowed in JSON data. (But you can get around that, if you're curious.) | |
| 28 | + | ||
| 29 | + | ## Usage | |
| 30 | + | ||
| 31 | + | Often, you may read or write it to files, for configuration purposes, small data sets, and useful structured data. | |
| 32 | + | It is a common, go-to data format. Most languages have great support for reading and writing it, like Java and Python. | |
| 33 | + | ||
| 34 | + | JSON exists as a string — useful when you want to transmit data across a network. | |
| 35 | + | It needs to be converted to a native object when you want to access the data within a program. | |
| 36 | + | ||
| 37 | + | Example of [python reading of file containing JSON. | |
| 38 | + | ||
| 39 | + | ```json | |
| 40 | + | # Python program to read | |
| 41 | + | # json file | |
| 42 | + | ||
| 43 | + | import json | |
| 44 | + | ||
| 45 | + | # Opening JSON file | |
| 46 | + | f = open('data.json') | |
| 47 | + | ||
| 48 | + | # returns JSON object as | |
| 49 | + | # a dictionary | |
| 50 | + | data = json.load(f) | |
| 51 | + | ||
| 52 | + | # Iterating through the json | |
| 53 | + | # list | |
| 54 | + | for i in data['emp_details']: | |
| 55 | + | print(i) | |
| 56 | + | ||
| 57 | + | # Closing file | |
| 58 | + | f.close() | |
| 59 | + | ``` | |
| 60 | + | ||
| 61 | + | Writing JSON: | |
| 62 | + | ||
| 63 | + | `data` is a Python dictionary. It needs to be encoded as JSON before writing. | |
| 64 | + | ||
| 65 | + | Use this for maximum compatibility (Python3 & 2, if you must): | |
| 66 | + | ||
| 67 | + | ```python | |
| 68 | + | import json | |
| 69 | + | with open('data.json', 'w') as f: | |
| 70 | + | json.dump(data, f) | |
| 71 | + | ``` | |
| 72 | + | ||
| 73 | + | On a modern system (i.e. Python 3 and UTF-8 support), you can write a nicer file using: | |
| 74 | + | ||
| 75 | + | ```python | |
| 76 | + | import json | |
| 77 | + | with open('data.json', 'w', encoding='utf-8') as f: | |
| 78 | + | json.dump(data, f, ensure_ascii=False, indent=4) | |
| 79 | + | ``` | |
| 80 | + | ||
| 3 | 81 | Check out: [JSON (JavaScript Object Notation)](https://www.json.org/json-en.html) | |
| 4 | 82 | It has a great set of syntax flow diagrams you can use to under stand it. | |
| 5 | 83 | ||
| 84 | + | ## JSON Examples | |
| 85 | + | ||
| 86 | + | A simple object | |
| 87 | + | ||
| 88 | + | ```json | |
| 89 | + | { | |
| 90 | + | "fruit": "Apple", | |
| 91 | + | "size": "Large", | |
| 92 | + | "color": "Red" | |
| 93 | + | } | |
| 94 | + | ``` | |
| 95 | + | ||
| 96 | + | Maybe something that came from a bloggin system. | |
| 97 | + | ||
| 98 | + | ```json | |
| 99 | + | { | |
| 100 | + | "title": "New Blog Post", | |
| 101 | + | "content": "This is the content of the blog post...", | |
| 102 | + | "publishedDate": "2023-08-25T15:00:00Z", | |
| 103 | + | "author": { | |
| 104 | + | "username": "authoruser", | |
| 105 | + | "email": "author@example.com" | |
| 106 | + | }, | |
| 107 | + | "tags": ["Technology", "Programming"] | |
| 108 | + | } | |
| 109 | + | ``` | |
| 110 | + | ||
| 111 | + | Or this one, a example of something whihc might have come from a database: | |
| 112 | + | ||
| 113 | + | ```json | |
| 114 | + | { | |
| 115 | + | "patientName": "Jane Doe", | |
| 116 | + | "dateOfBirth": "1985-02-15", | |
| 117 | + | "bloodType": "A+", | |
| 118 | + | "allergies": ["Pollen", "Penicillin"], | |
| 119 | + | "conditions": ["Hypertension", "Diabetes"], | |
| 120 | + | "medications": ["Lisinopril", "Metformin"], | |
| 121 | + | "emergencyContact": { | |
| 122 | + | "username": "emergencyuser", | |
| 123 | + | "email": "johnDoe@gmale.com" | |
| 124 | + | } | |
| 125 | + | } | |
| 126 | + | ``` | |
| 127 | + | ||
| 128 | + | An array: | |
| 129 | + | ||
| 130 | + | ```json | |
| 131 | + | { | |
| 132 | + | "employees":[ | |
| 133 | + | {"firstName":"John", "lastName":"Doe"}, | |
| 134 | + | {"firstName":"Anna", "lastName":"Smith"}, | |
| 135 | + | {"firstName":"Peter", "lastName":"Jones"} | |
| 136 | + | ] | |
| 137 | + | } | |
| 138 | + | ``` | |
| 139 | + | ||
| 140 | + | ||
| 141 | + | ||
| 142 | + | ||
| 143 | + | ||
| 144 | + | ||
| 145 | + | ||
| 146 | + | ||
| 147 | + | ||
| 148 | + | ||
| 149 | + | ||
| 150 | + | ||
| 151 | + | ||
| 152 | + | ||
| 153 | + | ||
| 154 | + | ||
kristofer revisó este gist . Ir a la revisión
1 file changed, 5 insertions
json.md(archivo creado)
| @@ -0,0 +1,5 @@ | |||
| 1 | + | # JSON Fundamentals | |
| 2 | + | ||
| 3 | + | Check out: [JSON (JavaScript Object Notation)](https://www.json.org/json-en.html) | |
| 4 | + | It has a great set of syntax flow diagrams you can use to under stand it. | |
| 5 | + | ||