murphyslemon / Raspberry_Pi_Server

International Sensor Development Project: This project is a voting system that uses a raspberry pi server and multiple voting devices. The system is easy, secure, scalable, and supports anonymous and registered voting. This repository focuses on the Raspberry Pi server.
0 stars 3 forks source link

decodeStringToJSON function #3

Open MagnusLii opened 7 months ago

MagnusLii commented 7 months ago

String decode function

Goal

The objective is to create a function that takes in a string from a MQTT message and tries to convert said string into a JSON object. The function should not modify the original string.

Arguments

Returns

MagnusLii commented 7 months ago
def decodeStringToJSON(json_string):
    try:
        decodedMessage = json.loads(json_string)
        print('JSON test passed.')
        return decodedMessage
    except json.decoder.JSONDecodeError as error:
        print(f'JSON test failed. Error: {error}')
        return -1