├── LICENSE ├── README.md ├── _config.yml └── _layouts └── default.html /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Tanbir chowdhury 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Welcome to Json-Guide 2 | JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. 3 | 4 | ## Comments 5 | --- 6 | Comments are not Supported in JSON :smiley: 7 | 8 | ## Json Syntax 9 | --- 10 | ### Json data types: 11 | * a string 12 | * a number 13 | * an object (JSON object) 14 | * an array 15 | * a boolean 16 | * null 17 | 18 | ### Json number types: 19 | * Integer 20 | * Fraction 21 | * Exponent 22 | 23 | ### Json Objects: 24 | ```js 25 | { 26 | "developer":{ "name":"Rafi", "age":20, "city":"Barisal" } 27 | } 28 | ``` 29 | ### Json Array: 30 | ```js 31 | { 32 | "developer":[ "Rafi", "Dhaka", "Bangladesh" ] 33 | } 34 | ``` 35 | ### Json Strings: 36 | ```js 37 | { "name":"Rafi" } 38 | ``` 39 | ### Json Numbers: 40 | ```js 41 | { "year": 2017 } 42 | ``` 43 | ### Json Booleans: 44 | ```js 45 | { "name": true } 46 | ``` 47 | ### Json Null: 48 | ```js 49 | { "name": null } 50 | ``` 51 | ### Example showing array containing multiple objects: 52 | ```js 53 | { 54 | "devlopers": [ 55 | { "name":"nahid" , "skill":"laravel" }, 56 | { "name":"rid" , "skill":"php" }, 57 | { "name":"rafi" , "skill":"html" } 58 | ] 59 | } 60 | ``` 61 | ## Json Objects 62 | --- 63 | ### Json Object Syntax: 64 | ```js 65 | { "name":"rafi", "age":10, "class":null, "year": true } 66 | ``` 67 | ### Json Accessing Object Values: 68 | ```js 69 | obj = { "name":"rafi", "age":10, "class":null, "year": true }; 70 | myobj= obj.name; 71 | ``` 72 | or 73 | ```js 74 | obj = { "name":"rafi", "age":10, "class":null, "year": true }; 75 | myobj= obj["name"]; 76 | ``` 77 | ### Json Looping Object: 78 | ```js 79 | loopobj = { "name":"rafi", "age":10, "class":null, "year": true }; 80 | for (x in loopobj) { 81 | document.getElementById("demo").innerHTML += x; 82 | } 83 | ``` 84 | or 85 | ```js 86 | loopobj = { "name":"rafi", "age":10, "class":null, "year": true }; 87 | for (x in loopobj) { 88 | document.getElementById("demo").innerHTML += loopobj[x]; 89 | } 90 | ``` 91 | ### Json Nested Objects: 92 | ```js 93 | Nesobj = { 94 | "name":"nahid", 95 | "age":30, 96 | "skill": { 97 | "skill1":"laravel", 98 | "skill2":"php", 99 | "skill3":"js", 100 | "skill4":"vue.js" 101 | } 102 | } 103 | ``` 104 | > Access nested Json objects 105 | ```js 106 | x = nesobj.skill.skill3; 107 | ``` 108 | or 109 | ```js 110 | x = nesobj.skill["skill3"]; 111 | ``` 112 | ## Json Arrays 113 | --- 114 | ### Arrays in Json Objects: 115 | ```js 116 | arrobj = { 117 | "name":"Rafi", 118 | "phone":01723*****, 119 | "address": "Dhaka, Bangladesh", 120 | "skill":[ "php", "html", "css", "javascript" ] 121 | } 122 | ``` 123 | > Access Array Values 124 | ```js 125 | x = arrobj.skill[0]; 126 | ``` 127 | ### Json Looping array: 128 | ```js 129 | for (i in arrobj.skill) { 130 | x += arrobj.skill[i]; 131 | } 132 | ``` 133 | or 134 | ```js 135 | for (i = 0; i < arrobj.skill.length; i++) { 136 | x += arrobj.skill[i]; 137 | } 138 | ``` 139 | ### Json Nested arrays: 140 | ```js 141 | nesarr = { 142 | "name":"Rafi", 143 | "age":10, 144 | "year":2017, 145 | "computer": [ 146 | { "name":"MacBook Pro ", "models":[ "MacBookPro14,1", "MacBookPro14,2", "MacBookPro14,3" ] }, 147 | { "name":"dell", "models":[ "XPS 13", "XPS 15", "XPS 13 2-in-1" ] }, 148 | { "name":"fujitsu", "models":[ "LH-532", "LH-531" ] } 149 | ] 150 | } 151 | ``` 152 | > Access Nested array Values 153 | ```js 154 | for (i in nesarr.computer) { 155 | x += "
" + nesarr.computer[i].name + "
"; 156 | for (j in nesarr.computer[i].models) { 157 | x += nesarr.computer[i].models[j]; 158 | } 159 | } 160 | ``` 161 | ### Json Modify array Values: 162 | ```js 163 | nesarr.computer[1] = "XPS 17" 164 | ``` 165 | ### Json Delete array: 166 | ```js 167 | delete nesarr.computer[1]; 168 | ``` 169 | ## Json parse 170 | --- 171 | ### Json Parsing 172 | ```js 173 | '{ "name":"Rafi", "Phone":01723, "city":"Dhaka"}' 174 | ``` 175 | ### Use the JavaScript function Json parse 176 | ```js 177 | var json = '{"name":"rafi", "result":true,"count":2}', 178 | obj = JSON.parse(json); 179 | ``` 180 | > Use the JavaScript object 181 | ```js 182 | 183 | 186 | ``` 187 | ### get data from the server: 188 | ```js 189 | var xmlhttp = new XMLHttpRequest(); 190 | xmlhttp.onreadystatechange = function() { 191 | if (this.readyState == 4 && this.status == 200) { 192 | var myObj = JSON.parse(this.responseText); 193 | document.getElementById("demo").innerHTML = obj.name; 194 | } 195 | }; 196 | xmlhttp.open("GET", "json_demo.txt", true); 197 | xmlhttp.send(); 198 | ``` 199 | ## Json stringify 200 | --- 201 | ### Json Stringify with JavaScript Object: 202 | ```js 203 | var myobj = { "name":"Rafi", "hometown":"Barisal", "currentcity":"Dhaka", "phone":01723****}; 204 | ``` 205 | > JavaScript function JSON.stringify, and string data sent to be a server 206 | ```js 207 | var myJSON = JSON.stringify(myobj); 208 | document.getElementById("demo").innerHTML = myJSON; 209 | ``` 210 | ### Json Stringify date: 211 | ```js 212 | var myobj = { "name":"Rafi", "date":new Date(), "home":"Barisal" }; 213 | ``` 214 | ### Json Stringify function: 215 | ```js 216 | var myobj = { "name":"Rafi", "phone":function () {return 01723***;}, "home":"Barisal" }; 217 | ``` 218 | ## JSON Schema 219 | --- 220 | ### Building a product schema: 221 | > JSON data for a product API 222 | ```js 223 | { 224 | "id": 1, 225 | "name": "Ace Plus", 226 | "price": 12.50, 227 | "tags": ["medicine", "drug"] 228 | } 229 | ``` 230 | > Starting the schema 231 | ```js 232 | { 233 | "$schema": "http://json-schema.org/draft-06/schema#", 234 | "title": "Product", 235 | "description": "A product from Medicine", 236 | "type": "medicine" 237 | } 238 | ``` 239 | ### Defining the properties: 240 | > what is id? 241 | ```js 242 | { 243 | "$schema": "http://json-schema.org/draft-06/schema#", 244 | "title": "Product", 245 | "description": "A product from Medicine", 246 | "type": "object", 247 | "properties": { 248 | "id": { 249 | "description": "The unique indentifier for a product", 250 | "type": "integer" 251 | } 252 | }, 253 | "required": ["id"] 254 | } 255 | ``` 256 | > Is name required? 257 | ```js 258 | { 259 | "$schema": "http://json-schema.org/draft-06/schema#", 260 | "title": "Product", 261 | "description": "A product from Medicine", 262 | "type": "object", 263 | "properties": { 264 | "id": { 265 | "description": "The unique identifier for a product", 266 | "type": "integer" 267 | }, 268 | "name": { 269 | "description": "Name of the product", 270 | "type": "string" 271 | }, 272 | "required": ["id","name"] 273 | } 274 | ``` 275 | > Can price be 0? 276 | ```js 277 | { 278 | "$schema": "http://json-schema.org/draft-06/schema#", 279 | "title": "Product", 280 | "description": "A product from Acme's catalog", 281 | "type": "object", 282 | "properties": { 283 | "id": { 284 | "description": "The unique identifier for a product", 285 | "type": "integer" 286 | }, 287 | "name": { 288 | "description": "Name of the product", 289 | "type": "string" 290 | }, 291 | "price": { 292 | "type": "number", 293 | "exclusiveMinimum": 0 294 | } 295 | }, 296 | "required": ["id", "name", "price"] 297 | } 298 | ``` 299 | > Are all tags strings? 300 | ```js 301 | { 302 | "$schema": "http://json-schema.org/draft-06/schema#", 303 | "title": "Product", 304 | "description": "A product from Acme's catalog", 305 | "type": "object", 306 | "properties": { 307 | "id": { 308 | "description": "The unique identifier for a product", 309 | "type": "integer" 310 | }, 311 | "name": { 312 | "description": "Name of the product", 313 | "type": "string" 314 | }, 315 | "price": { 316 | "type": "number", 317 | "exclusiveMinimum": 0 318 | }, 319 | "tags": { 320 | "type": "array", 321 | "items": { 322 | "type": "string" 323 | }, 324 | "minItems": 1, 325 | "uniqueItems": true 326 | } 327 | }, 328 | "required": ["id", "name", "price"] 329 | } 330 | ``` 331 | > Summary of products: 332 | ```js 333 | [ 334 | { 335 | "id": 2, 336 | "name": "An ice sculpture", 337 | "price": 12.50, 338 | "tags": ["cold", "ice"], 339 | "dimensions": { 340 | "length": 7.0, 341 | "width": 12.0, 342 | "height": 9.5 343 | }, 344 | "warehouseLocation": { 345 | "latitude": -78.75, 346 | "longitude": 20.4 347 | } 348 | }, 349 | { 350 | "id": 3, 351 | "name": "A blue mouse", 352 | "price": 25.50, 353 | "dimensions": { 354 | "length": 3.1, 355 | "width": 1.0, 356 | "height": 1.0 357 | }, 358 | "warehouseLocation": { 359 | "latitude": 54.4, 360 | "longitude": -32.7 361 | } 362 | } 363 | ] 364 | ``` 365 | ## Json-Examples 366 | --- 367 | ### JSON with Php 368 | > encode 369 | ```js 370 | $myArray = array('Name' => 'Tanbir', 'Age' => 22, 'City' => 'Dhaka', 'Job' => 'nai'); 371 | echo json_encode($myArray); 372 | ``` 373 | > decode 374 | ```js 375 | $json = '{"Name": "Tanbir","Age": 22,"City": "Dhaka","Job": "nai"}'; 376 | var_dump(json_decode($json)); 377 | ``` 378 | ### JSON with Python 379 | > encode 380 | ```js 381 | data = [ { 'Name' : 'Rafi', 'Age' : 8, 'City' : 'Dhaka', 'Job' : 'nai'} ] 382 | json = demjson.encode(data) 383 | print json 384 | ``` 385 | > decode 386 | ```js 387 | json = '{"html":5,"css":3,"laravel":5.5}'; 388 | text = demjson.decode(json) 389 | print text 390 | ``` 391 | ### JSON with Java 392 | 393 | > encode 394 | ```js 395 | class JsonEncodeDemo { 396 | 397 | public static void main(String[] args){ 398 | JSONObject obj = new JSONObject(); 399 | 400 | obj.put("name", "rafi"); 401 | obj.put("number", new Integer(01723)); 402 | obj.put("balance", new Double(123450.21)); 403 | obj.put("ip", new Boolean(true)); 404 | 405 | System.out.print(obj); 406 | } 407 | } 408 | ``` 409 | > decode 410 | ```js 411 | String jsonString = "{\"stat\":{\"sdr\": \"aa:bb:cc:dd:ee:ff\",\"rcv\": \"aa:bb:cc:dd:ee:ff\",\"time\": \"UTC in millis\",\"type\": 1,\"subt\": 1,\"argv\": [{1,2},{2,3}]}}"; 412 | JSONObject jsonObject = new JSONObject(jsonString); 413 | JSONObject newJSON = jsonObject.getJSONObject("stat"); 414 | System.out.println(newJSON); 415 | ``` 416 | 417 | ### License 418 |  419 | 420 | ### Admin 421 | [Tanbir](https://github.com/totanbir) 422 | 423 | ### Contributors 424 | [contributors](https://github.com/totanbir/json-guide/graphs/contributors) 425 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman 2 | title: JSON GUIDE 3 | description: For those who wanna learn JSON 4 | show_downloads: true 5 | -------------------------------------------------------------------------------- /_layouts/default.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | {% seo %} 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |