Concatenating strings like this is quite error prone.
I’d recommend first populating a Python dictionary, then encoding it as a JSON string using the json
module. In your case, it could look like this:
import json
capability_alternate_id = "SENSOR_001"
sensor_alternate_id = "4711_CR_SENS"
measures = ["24.0", "99.0"]
body = {
"capabilityAlternateId": capability_alternate_id,
"measures": [measures],
"sensorAlternateId": sensor_alternate_id
}
json_body = json.dumps(body)
Here, we set Python variables for capability_alternate_id
(a string), sensor_alternate_id
(a string), and measures
(an array of strings). We then define a dictionary (called body), and define keys and values in line with the format defined in your post above.
json.dumps(body)
takes the Python object body
, and converts it into JSON (using a process called serialisation).
This is preferred to editing a string directly — if you mess up defining body here, Python will throw an error that you can debug. When you manually concatenate a string together, Python will treat it like any other string, so it’s difficult to debug errors.
CLICK HERE to find out more related problems solutions.