fix(guide): simplify directory structure

This commit is contained in:
Mrugesh Mohapatra
2018-10-16 21:26:13 +05:30
parent f989c28c52
commit da0df12ab7
35752 changed files with 0 additions and 317652 deletions

View File

@ -0,0 +1,21 @@
---
title: JSON
---
JavaScript Object Notation or `JSON` uses the format of JavaScript Objects to store data. JSON is flexible because it allows for `Data Structures` with arbitrary combinations of `strings`, `numbers`, `booleans`, `arrays`, and `objects`.
Here is an example of a JSON object:
var ourMusic = [
{
"artist": "Daft Punk",
"title": "Homework",
"release_year": 1997,
"formats": [
"CD",
"Cassette",
"LP" ],
"gold": true
}
];
This is an array of objects and the object has various pieces of `metadata` about an album. It also has a nested `formats` array. Additional album records could be added to the top level array.

View File

@ -0,0 +1,28 @@
---
title: JSON Arrays
---
## JSON Arrays
JSON Arrays are no different from normal array object that you use in Javascript. Its an array object which contains multiple `JSON Objects`.
Here is an example of a JSON Array:
```Javascript
var aMyPlaylist = [{
artist: "Ed Sheeran",
track: "Supermarket flowers",
myRating: 10
}, {
artist: "Tracy Chapman",
track: "Fastcar",
myRating: 9
}];
```
This is an JSON Array named as `aMyPlaylist`. All array methods like `map`, `filter`, `sort` etc. can be applied on any JSON Array as well
#### More Information:
More array methods can be found in following links
- <a href='https://guide.freecodecamp.org/javascript/standard-objects/array' target='_blank' rel='nofollow'>Array - Freecodecamp</a>
- <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array' target='_blank' rel='nofollow'>Array - Mozilla Developer Network</a>

View File

@ -0,0 +1,58 @@
---
title: JSON Parse
---
## JSON Parse
The `JSON.parse()` method parses a string and construct a new object described by a string.
#### Syntax:
```javascript
JSON.parse(text [, reviver])
```
##### Parameters:
`text`
The string to parse as JSON
`reviver`(Optional)
The function will receive `key` and `value` as arguments. This function can be used to tranform the result value.
Here is an example on how to use `JSON.parse()`:
```javascript
var data = '{"foo": "bar"}';
console.log(data.foo); // This will print `undefined` since `data` is of type string and has no property named as `foo`
// You can use JSON.parse to create a new JSON object from the given string
var convertedData = JSON.parse(data);
console.log(convertedData.foo); // This will print `bar
```
<a href='https://repl.it/MwgK/0' target='_blank' rel='nofollow'>Repl.it Demo</a>
Here is an example with `reviver`:
```javascript
var data = '{"value": 5}';
var result = JSON.parse(data, function(key, value) {
if (typeof value === 'number') {
return value * 10;
}
return value;
});
// Original Data
console.log("Original Data:", data); // This will print Original Data: {"value": 5}
// Result after parsing
console.log("Parsed Result: ", result); // This will print Parsed Result: { value: 50 }
```
In the above example, all numberic values are being multipled by `10` - <a href='https://repl.it/Mwfp/0' target='_blank' rel='nofollow'>Repl.it Demo</a>
#### More Information:
<a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse' target='_blank' rel='nofollow'>JSON.parse - MDN</a>

View File

@ -0,0 +1,191 @@
---
title: JSON Stringify
---
## JSON Stringify
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
The `JSON.stringify()` method converts a *JSON-safe* JavaScript value to a JSON compliant string.
What are JSON-safe values one may ask! Let's make a list of all JSON-unsafe values and anything that isn't on the list can be considered JSON-safe.
#### JSON-unsafe values:
- `undefined`
- `function(){}`
- (ES6+) `Symbol`
- An object with circular reference(s) in it
#### Syntax
```javascript
JSON.stringify( value [, replacer [, space]])
```
In its simplest and most used form:
```javascript
JSON.stringify( value )
```
#### Parameters
`value` : The JavaScript value to be 'stringified'.
`replacer` : (Optional) A function or an array which serves as a filter for properties of the value object to be included in the JSON string.
`space` : (Optional) A numeric or string value to provide indentation to the JSON string. If a numeric value is provided, that many spaces (upto 10) act as indentaion at each level. If a string value is provided, that string (upto first 10 chracters) acts as indentation at each level.
#### Return type
The return type of the method is: `string`.
## Description
The JSON-safe values are converted to their corresponding JSON string form. The JSON-unsafe values on the other hand return :
- `undefined` if they are passed as values to the method
- `null` if they are passed as an array element
- nothing if passed as properties on an object
- throws an error if its an object with circular references(s) on it.
```javascript
//JSON-safe values
JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify('foo'); // '"foo"'
JSON.stringify([1, 'false', false]); // '[1,"false",false]'
JSON.stringify({ x: 5 }); // '{"x":5}'
JSON.stringify(new Date(2006, 0, 2, 15, 4, 5)) // '"2006-01-02T15:04:05.000Z"'
//JSON-unsafe values passed as values to the method
JSON.stringify( undefined ); // undefined
JSON.stringify( function(){} ); // undefined
//JSON-unsafe values passed as array elements
JSON.stringify({ x: [10, undefined, function(){}, Symbol('')] }); // '{"x":[10,null,null,null]}'
//JSON-unsafe values passed as properties on a object
JSON.stringify({ x: undefined, y: Object, z: Symbol('') }); // '{}'
//JSON-unsafe object with circular reference on it
var o = { },
a = {
b: 42,
c: o,
d: function(){}
};
// create a circular reference inside `a`
o.e = a;
// would throw an error on the circular reference
// JSON.stringify( a );
```
`JSON.stringify(...)` behaves differently if an object passed to it has a `toJSON()` method defined on it. The return value from the `toJSON()` method will be serialized instead of the object itself.
This comes in exceptionally handy when an object contains any illegal JSON value.
```javascript
//JSON-unsafe values passed as properties on a object
var obj = { x: undefined, y: Object, z: Symbol('') };
//JSON.stringify(obj); logs '{}'
obj.toJSON = function(){
return {
x:"undefined",
y: "Function",
z:"Symbol"
}
}
JSON.stringify(obj); //"{"x":"undefined","y":"Function","z":"Symbol"}"
//JSON-unsafe object with circular reference on it
var o = { },
a = {
b: 42,
c: o,
d: function(){}
};
// create a circular reference inside `a`
o.e = a;
// would throw an error on the circular reference
// JSON.stringify( a );
// define a custom JSON value serialization
a.toJSON = function() {
// only include the `b` property for serialization
return { b: this.b };
};
JSON.stringify( a ); // "{"b":42}"
```
#### The `replacer`
The `replacer`, as mentioned earlier, is a filter which indicates which properties are to be included in the JSON string. It can either be an array or a function.
When an array, the replacer contains the string representations of only those properties which are to be included in the JSON string.
```javascript
var foo = {foundation: 'Mozilla', model: 'box', week: 45, transport: 'car', month: 7};
JSON.stringify(foo, ['week', 'month']); // '{"week":45,"month":7}', only keep "week" and "month" properties
```
If `replacer` is a function, it will be called once for the object itself, and then once for each property in the object, and each time is passed two arguments, *key* and *value*. To skip a *key* in the serialization, `undefined` should be returned. Otherwise, the *value* provided should be returned. If any of these *values* are objects themselves, the `replacer` function serializes them recursively as well.
```javascript
function replacer(key, value) {
// Filtering out properties
if (typeof value === 'string') {
return undefined;
}
return value;
}
var foo = {foundation: 'Mozilla', model: 'box', week: 45, transport: 'car', month: 7};
JSON.stringify(foo, replacer); // '{"week":45,"month":7}'
```
If an array is passed to `JSON.stringify()` and `replacer` returns `undefined` for any of its elements, the element's value is replaced with `null`. `replacer` functions cannot remove values from an array.
```javascript
function replacer(key, value) {
// Filtering out properties
if (typeof value === 'string') {
return undefined;
}
return value;
}
var foo = ['Mozilla', 'box', 45, 'car', 7];
JSON.stringify(foo, replacer); // "[null,null,45,null,7]"
```
#### The `space`
The `space` parameter used for indentation makes the result of `JSON.stringify()` prettier.
```javascript
var a = {
b: 42,
c: "42",
d: [1,2,3]
};
JSON.stringify( a, null, 3 );
// "{
// "b": 42,
// "c": "42",
// "d": [
// 1,
// 2,
// 3
// ]
// }"
JSON.stringify( a, null, "-----" );
// "{
// -----"b": 42,
// -----"c": "42",
// -----"d": [
// ----------1,
// ----------2,
// ----------3
// -----]
// }"
```
#### More Information:
<!-- Please add any articles you think might be helpful to read before writing the article -->
Refer to [MDN docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify).

View File

@ -0,0 +1,118 @@
---
title: JSON Syntax
---
## JSON Syntax
The JSON syntax is a subset of the JavaScript syntax.
### JSON Syntax Rules
- JSON Object is an unordered set of name/value pairs.
- Object names are followed by a colon (:).
- Curly braces {} are used to hold objects. Object begins with { (left curly brace) and ends with } (right curly brace).
- JSON Object data is represented as a collection of name/value pair.
- Each name/value pairs are separated by comma (,)
- Square braces [] are used to hold Arrays.
### JSON Data - A Name and a Value
JSON data is written as name/value pairs.
A name/value pair consists of a field name (in double quotes), followed by a colon, followed by a value:
```
"handle":"moghya"
```
- JSON names require double quotes.
### JSON - Evaluates to JavaScript Objects
The JSON format is almost identical to JavaScript objects.
In JSON, keys must be strings, written with double quotes:
- JSON
```
"handle":"moghya"
```
- JavaScript
```
handle:"moghya"
```
### JSON Values
In JSON, values must be one of the following data types:
- a string
- a number
- an object (JSON object)
- an array
- a boolean
- null
In JavaScript values can be all of the above, plus any other valid JavaScript expression, including:
- a function
- a date
- undefined
### JSON Uses JavaScript Syntax
Because JSON syntax is derived from JavaScript object notation, very little extra software is needed to work with JSON within JavaScript.
With JavaScript you can create an object and assign data to it, like this:
```
var person = {
"name":"Shubham",
"age":21,
"handle":"moghya",
"website":"http://moghya.me/"
};
```
You can access a JavaScript object like this:
```
//returns moghya
person.handle;
```
It can also be accessed like this:
```
//returns http://moghya.me/
person["website"];
```
### Arrays in JSON
```
var team = {
"name":"novatoscript",
"members" :
[
{
"name":"Shubham Sawant",
"age":21,
"handle":"moghya",
"website":"http://moghya.me",
},
{
"name":"Saurabh Banore",
"age":21,
"handle":"banoresaurabh",
"website":"http://banoresaurabh.me/",
}
]
}
```
### Example
A very large example of JSON is [here!](http://moghya.me/js/profile.json).