JSON SERVER SIMPLIFIED
Published: 2023-08-11 20:33:16


**What is JSON?**

JavaScript Object Notation or, more commonly known as, JSON. JSON servers are designed for mocking APIs (Application Programming Interface), to help with prototyping, and mocking of data. It follows a certain syntax format that is simple for humans to use and easy for machines to generate. JSON uses simple key-value pairs to represent its data. The key-value pairs are organized as strings with double or single quotes enclosing them. The value can be represented by the basic data type, such as, strings, numbers, booleans, objects, arrays, and null. But it is not suitable for complex logic or cases where advanced security is needed to secure data. 


**JSON Syntax**

The main operation file of JSON Server is most often called ‘db.json’. This file is the data source, it houses the mock data you need for the API. The structure of this data consists of key-value pairs, where the key is the entity name and the value is or can be an array of data objects that belongs to the entity in the key. Within each array, the data objects are defined with a JSON Server property, “id”. This property uniquely identifies each data object within the entity. The “id” property is a requirement in Json data for it to effectively handle CRUD (Create, Read, Update, Delete) operations. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2364z5dht13tkjrovup4.png)



## CRUD (Create, Read, Update, Delete)



- **Create/ Post** 

The “POST” method is used for the “Create” operation in JSON server, it’s a request that signifies a creation of a new data entry. When the server receives the “POST” request, it validates the data and creates a new entry with the data provided. The server then would generate a unique identifier(id) for the data to distinguish it from previous data. After the new entry has been created, the server would update using HTTP response, to indicate if the attempt was a success or failure. 



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dhfyyj119uycpxxzl5vd.png)


- **Read/ GET**

The ‘READ’ operation ,“GET” method, is the fundamental part of JSON servers. Reading involves accessing data from the database or an API, this operation presents the desired data to the client. The client then can perform various operations on, from displaying the content of the data to analysis of the data. Similar to the ‘POST’ method, the ‘GET’ method first sends a request to the endpoint indicating that the client has the intention of retrieving data. Once the ‘GET’ request has been received, the server processes it and retrieves the requested data based on the parameters it was provided with. The server packages the data requested along with a HTTP response.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/civ5ca4c6b7xb96v10om.png)




- **Update/ PATCH/ PUT**

The ‘UPDATE’ operation is performed using the ‘PATCH” method or ‘PUT’ method. The ‘PUT’ method  requires a complete representation of the updated record. If any parameter is omitted during the process, they can be overwritten with default values. The ‘PATCH’ method updates only the fields provided and leaves the other fields unchanged. When the server receives the request, the server locates the record usually using the unique identifier. Then changes the current record while preserving the identifier. After the update, the server would send an HTTP response to indicate success or failure. 



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ar6l5vge7rlv1zvxtpc4.png)



- **Delete**

The ‘DELETE’ operation is used for the removal of data that is no longer needed. This operation is useful for keeping the data in the system relevant to the client. To delete, the client would send the request with the endpoint's identifier to indicate which data to delete. Once the request is received, the server processes the request and deletes the specific data with the identifier indicated. 



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mr2dpo5skeli033nx50w.png)




References:

- https://github.com/typicode/json-server

- https://www.w3schools.com/js/js_json_server.asp

- https://www.geeksforgeeks.org/json-server-setup-and-introduction/