-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy path48_json.js
68 lines (38 loc) · 5.76 KB
/
48_json.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// JSON(JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate.In JavaScript, you can work with JSON data using the JSON object.
// Here's an example of creating a JSON object in JavaScript:
// const person = {
// name: 'John',
// age: 30,
// address: {
// street: '123 Main St',
// city: 'Anytown',
// state: 'CA'
// }
// };
// const jsonString = JSON.stringify(person);
// console.log(jsonString);
// In this example, we're creating a JavaScript object called "person" with properties for name, age, and address. The address property is itself an object with properties for street, city, and state. We then convert the object to a JSON string using the JSON.stringify() method and log the result to the console.
// Here's an example of parsing JSON data in JavaScript:
// const jsonString = '{"name":"John","age":30,"address":{"street":"123 Main St","city":"Anytown","state":"CA"}}';
// const person = JSON.parse(jsonString);
// console.log(person.name);
// console.log(person.address.city);
// In this example, we're starting with a JSON string that represents a person object with the same properties as in the previous example. We then parse the JSON string into a JavaScript object using the JSON.parse() method and log the values of the "name" and "address.city" properties to the console.
// JSON is commonly used for exchanging data between a web server and a web application.You can use the Fetch API or the XMLHttpRequest object to send and receive JSON data over the network.Once you have the JSON data in your JavaScript code, you can manipulate it as needed using standard JavaScript object and array methods.
// Some additional topics related to JSON in JavaScript:
// JSON Schema: JSON Schema is a vocabulary that allows you to annotate and validate JSON documents.You can use JSON Schema to define the structure of a JSON document and ensure that it conforms to a specific format.There are several libraries in JavaScript that provide support for JSON Schema, such as Ajv and Z - Schema.
// JSON Web Tokens(JWT): JSON Web Tokens are a compact, URL - safe means of representing claims to be transferred between two parties.You can use JWTs to securely transmit information between a client and a server in a way that cannot be easily tampered with.There are several libraries in JavaScript that provide support for JWTs, such as jsonwebtoken and node - jsonwebtoken.
// JSONP: JSONP(JSON with Padding) is a technique for bypassing the same - origin policy of web browsers.It involves making a JSON request to a different domain and wrapping the response in a function call. The function is then executed in the context of the original page, allowing it to access the JSON data.JSONP is a legacy technique and is generally not recommended due to security concerns, but it can still be useful in certain situations.
// JSON in Node.js: JSON is also commonly used in Node.js applications for exchanging data between different parts of the application, such as between a server and a client or between different modules within the application.Node.js provides built -in support for JSON through the JSON object and the JSON.stringify() and JSON.parse() methods.
// JSON in browser - based applications: JSON is also widely used in browser - based applications for exchanging data with web servers.In this context, JSON is typically used in combination with the Fetch API or the XMLHttpRequest object to make requests and receive responses over the network.JSON data can also be manipulated and displayed in the browser using JavaScript and HTML / CSS.
// Here are some best practices for working with JSON in JavaScript:
// Validate incoming JSON: Always validate incoming JSON data to ensure that it conforms to the expected schema or format.You can use libraries like Ajv or Z - Schema for validating JSON data.
// Use concise and meaningful property names: Use concise and meaningful property names in your JSON data.This will make it easier for developers to understand and work with the data.
// Minimize the size of JSON data: Minimize the size of JSON data by removing unnecessary data, using abbreviations or shorthand, and compressing the data when transmitting over the network.This will improve performance and reduce bandwidth usage.
// Use camelCase for property names: Use camelCase for property names in your JSON data, as this is a common convention in JavaScript and will make it easier to work with the data in your code.
// Use consistent formatting: Use consistent formatting for your JSON data, including indentation, whitespace, and line breaks.This will make the data easier to read and understand.
// Use arrays for ordered collections: Use arrays for ordered collections of data in your JSON data, as this is a common convention and will make it easier to work with the data in your code.
// Use objects for unordered collections: Use objects for unordered collections of data in your JSON data, as this is a common convention and will make it easier to work with the data in your code.
// Avoid circular references: Avoid circular references in your JSON data, as these can cause issues with parsing and serialization.If circular references are necessary, consider using a library like cycle.js to handle them.
// Use a consistent data model: Use a consistent data model across your application, as this will make it easier to maintain and extend your code.This includes using consistent property names, data types, and formats.
// Use JSON.stringify() and JSON.parse(): Use the built -in JSON.stringify() and JSON.parse() methods to convert between JavaScript objects and JSON data.These methods are fast, efficient, and reliable, and are supported by all modern browsers and Node.js.