This is a simple Flask API for retrieving nested data from a JSON file.
-
Clone the repository:
git clone https://github.com/immodded/json-server.git cd json-server
-
Install dependencies:
Ensure you have Python installed. Then install Flask using:
pip install Flask
-
Run the application:
Use the following command to run the Flask application:
python main.py <json_file_path> [port]
<json_file_path>
: Path to the JSON file you want to load.[port]
(optional): Port number to run the Flask application. Defaults to 7070 if not provided.
-
Access the API:
- Open your web browser and navigate to
http://127.0.0.1:7070/
to see the entire JSON data. - To access nested data, use paths like
http://127.0.0.1:7070/key1/key2/key3
.
- Open your web browser and navigate to
GET /
: Returns the entire JSON data.GET /<path:keys>
: Returns the nested data at the specified path.
Example:
http://127.0.0.1:7070/
returns the entire JSON data.http://127.0.0.1:7070/key1/2/key3::gt:69/keyx
returns the nested data at the specified path.http://localhost:6001/from:1::to:5
return the list from index 1 to index 5http://localhost:6001/from:3::
return the list from index 3 to last.http://localhost:6001/::to:6
return the list from starting to 6.http://localhost:6001/age::gt:20
return the list in which dictionary item has age[key] value greater than 20.http://localhost:6001/age::lt:30
return the list in which dictionary item has age[key] value less than 30.http://localhost:6001/id::et:3
return the list in which dictionary item has id[key] value equal to 3.
- If an invalid path, index out of range, or trailing slash is detected, an error message will be returned in JSON format.
Example:
{
"error": "Invalid path, index out of range, or remove trailing slash."
}
This project is licensed under the MIT License - see the LICENSE file for details.