This Flask application provides a simple API for managing and validating JSON data. It allows users to retrieve, update, and delete key-value pairs, validate JSON objects based on a specified maximum depth, and handle errors gracefully. The application is designed to be flexible and easily extendable, making it a useful tool for managing JSON data in a web environment.
- Index Route: Displays a welcome message to indicate that the server is running.
- Validate JSON: Validates a JSON object based on a specified maximum depth.
- Data Management: Allows retrieval, update, and deletion of key-value pairs in a global
data_content
dictionary. - Error Handling: Provides custom error handling for 404 errors (Page Not Found).
- Description: Returns a welcome message.
- Response:
{ "message": "Welcome to the templates app server!" }
- Description: Validates a JSON object based on a specified maximum depth.
- Request Parameters:
max_depth
(optional, default: 2): The maximum allowed depth of the JSON object.
- Request Body:
- A JSON object to be validated.
- Response:
- If the JSON is valid:
{ "valid": true }
- If the JSON is invalid:
{ "valid": false, "error": "Provided data is not a valid JSON object" }
- If the JSON is valid:
- Description: Returns the entire
data_content
dictionary. - Response:
{ "key_1": "value_1", "key_2": "value_2" }
- Description: Retrieves the value associated with a specified key in
data_content
. - Request Parameters:
key
: The key for which the value should be retrieved.
- Response:
- If the key is found:
{ "key": "value" }
- If the key is not found:
{ "message": "The requested parameter <key> was not found" }
- If the key is found:
- Description: Deletes a key-value pair from
data_content
. - Request Parameters:
key
: The key to be deleted.
- Response:
- If the key is found and deleted:
{ "message": "Parameter <key> and its content was deleted successfully" }
- If the key is not found:
{ "message": "The parameter <key> was not found" }
- If the key is found and deleted:
- Description: Updates the
data_content
dictionary with new key-value pairs. - Request Body:
- A JSON object containing key-value pairs to be added or updated in
data_content
.
- A JSON object containing key-value pairs to be added or updated in
- Response:
{ "data_content": { "key_1": "value_1", "key_2": "value_2", ... } }
- Description: Handles non-existent routes and returns a 404 error message.
- Response:
{ "error": "404 Not Found: The requested URL was not found on the server." }
To run the application, execute the following command in your terminal:
python app.py
The server will start on http://0.0.0.0:5000/
and can be accessed via a web browser or a tool like curl
or Postman.
- Flask: A lightweight WSGI web application framework for Python.
- validate_json: A custom module to validate the structure of JSON objects.
To install the dependencies, run:
pip install Flask