Particle.publish() json string conversion

Hello

I have an issue concerning the publishing of an event. I make the following json string in my code:

String jsonData = String("[{\"whatever0\" : " + String(val0) + ", \"whatever1\" : " + String(val1) + ", \"whatever2\" : " + String(val2) + ", \"whatever3\" : " + String(val3) + ", \"whatever4\" : " + String(val4) + "}]");

My problem is that at the endpoint table in azure, the different titles and their corresponding values aren’t stored correctly. It is stored in a single string. Are there someone who has faced the same issue?

What exactly does that mean?
How are they stored?

BTW, are the square brackets around the curly braces required?
Usually these indicate an array which your JSON doesn’t appear to be.

The square brackets were just an attempt to fix my problem. I need to store the data in a table in the following way:

image

The data is stored in a table through stream analytics in the following way instead:

Is it possible to alter the string in the json data package that is send to Azure?

I have simplified my data, since I’m not sure whether I’m allowed to share the stuff I’m
working on :slight_smile:

In what ways do you want to alter the string?
Can you give an example, please?

2 suggestions;

  1. Don’t use string object, C-char array is much better- format using snprintf();
    char dataStr(MAXDATA);
    e.g. snprintf(dataStr, MAXDATA, “{[{“whatever0”:”%3.2f"},{“whatever1”:"%3.2f"}]}", val0, val1); OR better use the JsonParserGeneratorRK library.
  2. Use a tool to check the JSON is correct. When you publish and follow in the Console that will only format the JSON payload if it is correct.
1 Like

If you consider the 2 images I attached in the previous message, you might get an idea of what I’d like to do :slight_smile: I publish an event where I have mad a string I argue that is in json format. But the Azure endpoint doesn’t divide the message as desired.

Okay, your first suggestion with the snprintf function looks like it would divide the data in the correct way. Have you tried this method before? I’ll check out the JsonparserGeneratorRK library to see if I can figure something out in 8 hours time :slight_smile: It’s Bed time now where I am.

Yes, about 250,000 times a day. To help with the parsing of the JSON payload in the events by the web app event ingestion system, SQLserver I think, the format I use is paired {“N”:“var name”, “V”:“var_value”} to make this process as efficient as possible. How you structure the JSON, which must be correct, will depend upon what the web server does with it.

1 Like

Okay, so using the snprintf() function gives me the folowing error:

I have made the following line as you suggested:

snprintf(jsonData, sizeof(jsonData), "{[{ \"whatever0\" : "%3.2f" }, {\"whatever1\" : "%3.2f"}, {\"whatever2\" : "%3.2f"}, {\"whatever3\" : "%3.2f"}, {\"whatever4\" : "%3.2f"}]}", val[0], val[1], val[2], val[3], val[4]);

And I have another question, why 3.2f and not just .2f in “%3.2f”?

You need to escape the double quotes for the values just the same as you did for the keys.

I’d go with \"%.2f\".
Unless you have a fixed length format where you want all your decimal separators vertically alligned, then something like `%7.2f" (assuming 7 being the max length of the biggest number including potential negative sign) would be the way to go.

Alright, I’ve tried that, but my endpoint still receive all of it as a string ´. Is there a way to asure that it will be able to seperate the different variables?

image

The above image is what i receive in my endpoint table. As you can see it
is still received as a string. I read in another thread that particle.publish only
passes strings, so is the real issue? That the publish function is limited
compared to what I’d like it to do?

That is true, but JSON is typically serialised as JSON anyway so that is not the issue.

It’s most likely the the format of the JSON and potentially also the setup of your webhook.
Can you also show the setup of your webhook?

BTW, have you seen this workshop

It also addresses Azure webhook setup.

Also, have you searched the forum for “azure json” and looked at the proposed solutions?
It’s always good to have a thorough browse through the forum threads that touch on your issues at hand.

I’m not sure what you mean with the webhook? I have made an Azure IoT Hub integration, in the particle console.

I’ve browsed the forum again, and found an old post, which might solve my problem. The thread is:

RWB seems to know alot about this!

1 Like

@ScruffR has covered your queries about the floating point number format? - I just quickly copied what I use - mea culpa - I actually don’t hand craft the JSON string, I define a number of const char arrays for sequences like variable name where they are used many times and insert them as a string using %s format control. Also, “%.2f” should be \"%.2f\". Please post how you get on with Azure IoT integration - we would all be interested if you can share your (eventual) success!

1 Like

Actually, I’ve found an old topic from 2016, and currently await response from one of the dudes who answered it. I have half of the solution already and get my data divided in the query in Azure. However, one of the images showing what to write in the query to make ends meet, isn’t there anymore. The topic I’m referering to is:

If I get an answer an it proves succesfull, I’ll make a small document describing what I’ve done.

I’ve used “%.2” and removed the square brackets and one set of the curly brackets and this
works. However, I still get a whole string in Azure.

Alright, so the solution of passing the json string data, which can be made with a char is to alter the json data which is send from the particle console. In my case, the custom json file looks like this:

image

If anyone is interested in the string I send in the publish function, feel free to write a message to me :slight_smile:

1 Like