Including " as a delimiter character in event data string[Solved]

I am trying to send an event with data string that is structured JSON like;

"Variable_Name1","value1",
"Variable_Name2","value2",
...
"Variable_Namen","valuen"

I had a look on Stackoverflow and the 2 approaches suggested were to use “” or "

I am using a String::format() to format the data string and to insert the " characters. I have tried “” and " - unfortunately neither of these work.

Any suggestions as to make this work?

Hi @armor. I have assumed you are asking how to embed a " character in a string within particle code.
I used the " (backslash) before the inserted " character.
This worked for me.

See code fragment below:

    String message = "{\"SMSTokString\": \"Bearer ";
    message.concat(token);
    message.concat("\", \"body\": \"");
    message.concat(body);
    message.concat("\"}");

This successfully produces a string containing the " characters.

I dont know about the string::format() command and how this might differ.
Hope this helps
Regards
GM

Thanks for your help. When I follow the approach you have suggested (dataStr is declared as a String!)

    dataStr = "\"Param_TimerP\",";
    dataStr.concat(String::format("%3i",param.timerPeriod));
    dataStr.concat("\"");

the event is received like this;

"data":"2016-10-09T14:27:32Z,\"Param_TimerP\",222\""

This is where I started!

If you are using String::format() you needn’t use concat() - just do it in one go

    dataStr = String::format("\"Param_TimerP\",\"%3d\"",param.timerPeriod);

And for the embedded escaped double quotes don’t mind them.
Just have a look at the actual string via

  Serial.print(dataStr);

BTW, what’s your target for that data? I’d think JSON key value pairs usually should be connected with colons (:) and individual pairs separated with commas (,).

@ScruffR

Hello

String::format("\"Param_TimerP\",\"%3i\"",param.timerPeriod); 

This was exactly what I did first however " are showing in the CLI rather than " when I do a Particle Subscribe Mine. Is that the issue i.e. if I output to Serial with print it will appear OK?

In answer to your BTW - I have been asked to output the events in the following JSON like format:

1.	"data":"{
           "variables": {
           "Param_TimerP","20"}"

The Particle CLI prints the event as a JSON object. For example:

{"name":"test1","data":"{\"a\":123}","ttl":"60","published_at":"2016-10-09T15:39:39.478Z","coreid":"001"}

Because the event data is represented as a string in the event object, any JSON characters in the string have to be escaped. Thus it shows as {\"a\":123} when it’s printed, but if you use that event data from something that understands JSON, like webhooks, or a node.js script, it’s handled properly. In other words, this is what a webhook would see:

{"a":123}

@rickkas7 Thanks for explaining that behaviour of the CLI. Problem Solved

2 Likes

Glad you were able to get it fixed @armor, thanks everyone for the help!

@armor, what Rick explained there was what I meant when I said that :wink:

and if you checked

you might have seen the actual string

1 Like

No Worries! Just pleased to be getting on with things - I thought I had it the first time.

1 Like