Problems with JSON


I’m working on a binding for the Tesla Powerwall that uses the Tesla Cloud API.

I’m struggling the build the JSON to send a command in the right format.

A curl command like:

curl -H 'Authorization: Bearer token' --location --request POST ''  -H "Content-Type: application/json" -d '{"backup_reserve_percent":'10'}'

will work to set the backup_reserve_percent to 10%

My current code is:

        String payload = "{\"backup_reserve_percent\":'" + newreserve + "'}";

        logger.debug("payload = {}", payload);
        ByteArrayInputStream input = new ByteArrayInputStream(payload.getBytes());
        String response = invoke("POST", "" + siteID + "/backup",
                accessToken, input, "application/json");
        logger.debug("response to reserve change = {}", response);

and the output I get is:

2022-02-04 19:12:07.520 [DEBUG] [rnal.TeslaPowerwallControlWebTargets] - payload = {"backup_reserve_percent":'12'}
2022-02-04 19:12:07.521 [DEBUG] [rnal.TeslaPowerwallControlWebTargets] - Calling url:
2022-02-04 19:12:07.707 [DEBUG] [rnal.TeslaPowerwallControlWebTargets] - response to reserve change = {"error":"invalid JSON: unexpected character (after backup_reserve_percent) at line 1, column 27 [parse.c:769] in '{\"backup_reserve_percent\":'12'}"}

Note the \ characters that are making it thru to Tesla’s server, and the extra "}

Any ideas?

Did you try to replace the single quotes ( ’ ) with double quotes (") ?

assume that is required to make the error message JSON compliant as well …

I fixed this. The Tesla server doesn’t like " so in testing I was using ’ via curl and it worked. Turns out, removing the ’ fixes it.


        String payload = "{\"backup_reserve_percent\":" + newreserve + "}";

Results in json that Tesla will accept :slight_smile:

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.