Simple Python Json Test Script

Hello together :slight_smile:

I made a simple Python Script which helps Devs to “Simulate” and Test for example SImple Json Answers or Devices in OpenEMS :slight_smile:

Feel free to improve or use it ! :slight_smile:

from flask import Flask, jsonify, request
import json

app = Flask(__name__)

# Configuration Variables
JSON_FILE_PATH = 'json_file.json'  # Path to your JSON file
SERVER_PORT = 80                     # Server port
SERVER_HOST = '0.0.0.0'                # Server host, use '0.0.0.0' to make server accessible externally
ENDPOINT = '/status'                  # Endpoint to serve JSON data

# Load our JSON data from a file
def load_json_data():
    with open(JSON_FILE_PATH, 'r') as file:
        data = json.load(file)
    return data

# Set up the endpoint to serve the JSON data
@app.route(ENDPOINT, methods=['GET'])
def get_data():
    data = load_json_data()

    # Check if 'id' parameter is present in the query string
    query_id = request.args.get('id')
    if query_id:
        filtered_data = {key: value for key, value in data.items() if key == query_id}
        return jsonify(filtered_data)

    # If no 'id' parameter is specified, return all data
    return jsonify(data)

if __name__ == '__main__':
    app.run(debug=True, host=SERVER_HOST, port=SERVER_PORT)
1 Like