String::format will not format strings correctly for Publish data

Hi All,

Trying to publish and pass data formatted as JSON.

Here is my code:

String b = "def";
String c = "ghi";
dataString = String::format("{\"A\" : \"abc\", \"B\" : \"%s\", \"C\" : \"%s\"}", b, c);
Particle.publish("test",dataString, PRIVATE);

Here’s what I am seeing int he console after it runs:

"data": "{\"A\" : \"abc\", \"B\" : \"\", \"C\" : \" \u0010�\u0001 \u0003 \"}"

NOTE: I tried to use b.c_str() for the arguments at the end, but it won’t compile.

This is pretty close to the example in the docs, so I am not sure what’s up.

If I code the string statically as a string it works fine.

(Cloud compile from VSCODE Particle Workbench)

You need to use (const char*)b or c.c_str() since sprintf() (which is used for formatting) will not accept String objects but only const char* for the %s place holder.

When stating something like this it’s always also advisable to quote the actual compile error to see why it wouldn’t.