Azure Integration - Custom JSON using JSONWriter

Hi!

I’m testing the Azure Integration on Particle IO, and I’m customizing the template going out. So far the template looks good with the shape of the JSON I want to send out, but I’m missing something in the format of the data.

The issue I’m having is that I create a JSON string using the JSONWriter, but when I send it to Azure it adds the " on the content.
For Example, if I send the JSON (this is automatically created using the JSONWriter) :
{“JSONCode”:200, “AI Input”:2}

Azure reads it as:
{"JSONCode":200, "AI Input":2}

How can I customize the template so that it is read properly? One complication is that my JSON is dynamic, so it can be a JSON with 2 keys or 100 keys. I have sent this message over MQTT and it is received properly on the Brokers, but it’s not being written properly on the Azure integration.

Thanks for your help!

-R

Hi Romulo, both look the same here on the forum, maybe there needs to be an escape \ somewhere.
Maybe what Azure receives looks like this?

{“JSONCode”:“200”, “AI Input”:“2”}
I can’t be sure…

What would also help is that you show the code of the JSONwriter you have, and the types of the variables that source those 200 and 2 values.
Thanks

wow! I just noticed what you mentioned…
so I do a particle.publish() with the data that will be sent to Azure, which is the event data, and it shows with the '' sign before the quotation marks.
I hope this shows the ''… I’m adding dashes - to focus where it’s added.
Azure reads it as:
{--“JSONCode--”:200, --“AI Input--”:2}

the type of variables I add to the JSON are doubles and strings. I have arrays of parameters for analog inputs, and I scan through them to write the JSON string. there is an interval to create a JSON including all the parameters, and an interval to create a JSON including only the data which values have changed.
When I send this data to an MQTT broker, it receives it without any problem. When the event is published on Particle IO, it shows a valid JSON file. When I send it to Azure, it adds the '' before the quotation marks " .

image

thanks!

IF (big IF) I understand, you observe quotes around the names of the JSON parameters like JSONCode and AI Input.
I think this is normal, in fact, I just tried in a json online formatter something without quotes and it added the "missing quotes":

my test:

{aloha: 1}

what the site said:

The quotes seem ok, so I would search for a problem in the code that reads it, since you report that it is not read properly. What issue do you see there when you say:

?
Cheers

thanks @gusgonnet … I meant that there are backslash (\) added when read by Azure.

on this image it shows the backslash on Azure. they should not be there, how do I get rid of them? The quotation marks are not the issue
image

from the particle.publish, I see that the message is being written properly. it is not clear to me why when displayed on Azure it shows the backslash.

It’s annoying that when I write a backslash, the post deletes it. I wrote the backslash on the parenthesis on the first line of my last reply, and it was deleted…
\\\\\\\\\\\\\\ — on the left I just wrote a bunch of backslashes, I hope these are not deleted.

Backslashes are special characters in many languages and may also be used as mark-up in posts.

To embed special characters you can use the "Preformatted text" feature image
This way you can write a single \ just fine :wink:
Alternatively you could write two consecutive \ to display one - just as you would in a C string.

I reformatted your post above to show the backslashes you originally wrote.

They need to be there to distinguish between the delimiting ("outer") double quotes and embedded ones. Once a string containing double quotes gets extracted these escape characters should also be removed/treated automatically.

1 Like

alright, now it's clear that this is not an issue and it's only a display thingy, as per the reasons stated by @ScruffR (thanks!).

You are going to see this all over the place when playing with JSON strings :eyes: , in Azure, in Particle, in any cloud or tool...

thanks @ScruffR and @gusgonnet for your support. I usually don’t go that deep into coding so things don’t make sense to me, hence all these questions.

ok, I agree they are special characters. It’s still not clear to me why they are showing on Azure. I also have applications where I send data from an Ewon or from a python app, for example, and the message received by Azure there doesn’t display the backslashes. Why is this only happening when I send data from Particle IO, and only when I’m writing the JSON?

I’ve also run tests on Particle IO where I send a raw string like: dataToSend
on Azure it’s received as: “dataToSend”

if I write a string like: JSONCode: 200, AI Input: 4
on Azure it’s received as: “JSONCode: 200, AI Input: 4”

again, from the image below, it publishes the data properly on the Particle IO Event, but it shows the backslashes on Azure.
image

I believe there is something about the event_data created by the JSONWriter and how it’s read on Azure that is adding this. Is there an explanation to this?

thanks!

-R

Hi,
I don’t know to much about Azure but that’s normal IMHO and is not about double quotes ‘"’ it’s rather related to your value of key named “event_data”
which are strings not JSON yet you have to deal with that string parse it to JSON.
To visualize this a little I’m going to provide some examples:


res_Aur2

here is a screenshot of a JS function which call for Particle variable named STATUS as you can see is nice formatted JSON with my custom data located at key named RESULT but this is a string not JSON yet so I have to parse it to get JSON and be able to get values of keys
please note that particle console read this as normal valid JSON already and displays for variable STATUS just parsed RESULT key value
console_particle

and here is simple HTML js to deal with similar data as you have

<!DOCTYPE html>
<html>
<body>

<h2>convert JSON value String to JSON</h2>
<script>

var JSONTest = {"event_type": "test", "event_data": "{\"Jsoncode\":200, \"Ai_input\":2}","timestamp": "2022-07-05T19:39:33"};

console.log("JSONTest full",JSONTest); // it is alredy valid JSON.

console.log("JSONTest first el value: ",JSONTest.event_type); // it is still valid JSON so we can access the value of element directly 

console.log("JSONTest parse string at key -> e_n_data: ",JSON.parse(JSONTest.event_data)); // it is string object we have to parse it to JSON.

console.log("JSONTest_last_el_value: ",JSONTest.timestamp); // the same story as first el.



</script>
</body>
</html>

and the console result:

Not sure as I mentioned before but maybe the Azure has some kind of functionality to do something similar to JS JSON.parse(); :thinking:

Best,

1 Like

thanks @dreamER for your detailed reply.

this is the template I’m using on the Particle IO integration:
image

the value of the key “event_data” is: “{{{PARTICLE_EVENT_VALUE}}}”
{{{PARTICLE_EVENT_VALUE}}} is the result of the Event named JSON_Rec.

I’ve modified this function for testing purposes – JSON_Rec is a simple publish that publishes any string I write into a function. for example:

image

when published, it shows this on the Events console raw and prettified:
image

this input goes to Azure as follows. the value of key ‘event_data’ is supposed to be the same string I wrote into the function:
image

Originally, I have a function that creates a more complex valid JSON string.

My intention is that this valid JSON string is reproduced exactly the same in Azure. I don’t know why I’m not achieving that.

Now, if I run a simpler string on the function:

image

I get this published (not a valid JSON string):
image

and on Azure I get this:
image

I might be missing something on the custom template on the Azure integration, or I might be missing something on the way the JSONWriter works. But from these tests, I’m pretty sure Azure is displaying the exact raw content that I’m sending. so the deduction is, something on the Particle IO side needs to be fixed.

Thanks again for your support, it’s amazing the time all of you take to help confused programmers like me!

-R

You cannot have a JSON field containing a string with unescaped double quotes. The parser could just not know which of the double quotes would indicate the actual end of the string. It will always take the first (unescaped) double quote after any opening double quote as the end of that string.

Just try validate your intended JSON with any online validator

{
  "event_data": "{"AI Name": "flowswitch"}"
}

(take note of the syntax highlighting)

I’m pretty sure that you won’t find one that will accept such a JSON string.

However if you remodeled your template to produce something like this instead

{
  "event_data": { "AI Name": "flowswitch"}
}

(take note of the syntax highlighting)
it would work.

To achieve that try to remove the enclosing double quotes around {{{PARTICLE_EVENT_VALUE}}}.

BTW, I’d also recommend against using embedded blanks in your JSON keys - even when this will be accepted by (some) JSON parsers, it’s just bound to break at least with at some point :wink:

1 Like

thanks @ScruffR ! that’s exactly what I was missing.

I had made some tests with 2 brackets, 1 bracket, etc, but the best I’d got so far was the string with the backslashes. In my mind I had already tried without the quotation marks…

this is what I sent on the publish:

then on Azure (this tastes like a steak and a cold beer to me):

thanks again!

-R

2 Likes

:man_facepalming:t3:
So in fact was double quotes :man_shrugging:t3:
Glad that you short this one out :+1:

2 Likes

hahahah there’s some truth to that… but to be fair, I had not provided the custom JSON template before your comment on the double quotes.

thanks @dreamER !

-R

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