Publishing array values through custom JSON, in Azure Integration


#1

Hi folks,

I have a working Electron publishing custom JSON data to the Particle Cloud, with an integration defined to relay message to an Azure IoT Hub.

I’d like to pass an array as the value of one of my custom properties, but it gets passed as a string value.
Seems I would need to remove the quotes around the placeholder, however the UI treats this as a sytax error.

I think this applies to other data types (like number), and also embeded objects.

I’ve searched for other postings on custom JSON and array, and see some discussion about web hooks, but i’m not understanding how this might pertain to an Azure integration?

Can someone boil this down? Is there a way to do this?

Thanks

-John


#2

This is the best place I’ve found for working out JSON formatting issues.
http://www.jsoneditoronline.org/


#3

Thanks Bear, but this really doesn’t have anything to do with JSON exactly, but more of what semantics are of the integration templating.

For example, this JSON:

{
    "thisIsAnArrayOfStrings": [ 'red', 'green', 'blue' ]
}

…and this JSON…

{
    "thisIsJustAString": "[ 'red', 'green', 'blue' ]"
}

Are both valid JSON.

However, the Particle integration will not allow me to configure the first, so I can’t provide an array value from my code. I (THINK) I would have to create a template without the quotes around {{readings}}, and pass the array in the publish method like this:

Particle.publish("sampling", "{ \"readings\": [ 'red', 'green', 'blue' ] }", 60, PRIVATE);

The integration UI does not consider the missing quotes as a valid syntax. (see red X in left margin of original post)

Am I missing something?

-John


#4

Can you post some screenshots of your custom Azure integration in the Particle console?

And the code you’re using in the Particle Publish?


#5

RWB, the azure integration custom JSON is already in the original post.
The reply before yours contained the code for the publish call, repeated here:

Particle.publish("sampling", "{ \"readings\": [ 'red', 'green', 'blue' ] }", 60, PRIVATE);

Is there something else specific I can provide?

-John


#6

This might help, Just copied a snippet from one of mine.

char ledJSON[100];
char ledtemp[100];

snprintf(ledJSON, sizeof(ledJSON), “{\ledColor”:{“red”:%d,“green”:%d,“blue”:%d}}", 1,2,3);

and another way:

sprintf(ledtemp, “%02d:%02d:%02d”, sun_set_h -12 , sun_set_m, sun_set_s);
sprintf(ledJSON, “{“sunset”:”%s"}\r\n", ledtemp);


#7

Thanks mikemoy,

Sorry to mislead you, I just simplified the Particle.publish() method for illustration. I make use of snprintf() fine, and can compose the JSON payload fine in my code.

My issue is that the JSON object ( {“red”,“green”,“blue”} in your example), winds up being just a quoted string, instead of a nested object - because my custom JSON template does not allow me to omit the quotes around my {{readings}} placeholder.

{
    "ledColor": "{ 'red','green','blue' }"
}

(I used single quote for the inside strings to distinguish from the outer quotes)

Now I’m convinced I’m missing something, because no one has said this can’t work! :fearful:

-John


#8

@JohnKoz Maybe you’re having the same issues we discuss here:


#9

Yeah, unfortunately David summed the answer up a month ago in that post - I’m SOL.
The custom JSOM sophistication is a low priority, and passing an integer, object or array is not possible with the current offering.
I don’t even know where I can go from here, I’ll have to revisit my entire design.


Thanks for the help everyone.

-John


#10

Let’s ask @Dave if there is any progress on this front?


#11

I’ll ping the team about this again, thanks for raising the issue.

Thanks,
David


#12

Has there been any progression on this?
Is there no way to pass anything other than strings with webhooks?


#13

Yes, you can send up the whole block of JSON as JSON if you use the body element instead of the json element in the webhook.


#14

I have read the above tutorial but I still can not send JSON as JSON using the body element. I have a simple array in JSON that I would like to transfer to Azure.

In my electron firmware, I am sending this data (the same as the example in the tutorial):

String str="{\"a\":[123,456,789]}";
Particle.publish("values", str);

In Particle Console event log, I see that this data is received: {“a”:[123,456,789]}

 {"data":"{\"a\":[123,456,789]}","ttl":60,"published_at":"2018-07-27T20:12:32.712Z","coreid":"xxxxxxxxxxxxxxxxxxxxxxx","userid":"xxxxxxxxxxxxxxxxxxxxxxx","version":0,"public":true,"productID":xxxx,"name":"values"}

In Particle Console, I have this under Azure integration advanced JSON:

{
    "headers": {
        "Content-Type": "text/json"
    },
    "event": "{{{PARTICLE_EVENT_NAME}}}",
    "data": "{{{PARTICLE_EVENT_VALUE}}}",
    "device_id": "{{{PARTICLE_DEVICE_ID}}}",
    "published_at": "{{{PARTICLE_PUBLISHED_AT}}}",
    "fw_version": "{{{PRODUCT_VERSION}}}",
    "body": "{\"anArray\":[{{a}}]}"
}

In Azure, I receive this:

{
  "body": "{\"anArray\":[123,456,789]}",
  "fw_version": "0",
  "published_at": "2018-07-27T20:08:32.215Z",
  "device_id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  "data": "{\"a\":[123,456,789]}",
  "event": "values",
  "headers": {
    "Content-Type": "text/json"
  }
}

So in Azure, I receive string instead of a JSON object.
I would like to get it like this:

{
  "body": {"anArray":[123,456,789]},
  ...
}

What have I done wrong?
Or is it possible to send a JSON array using json element?

Thanks,
Jerry


#16

Hi Jerry,

I haven’t circled back around to try this again myself, but I’d like to see you get this working.

In your last post you have examples of JSON and an example of code, but they don’t match. I understand you’re abbreviating the code for the sake of simplicity and illustration (I would do the same), but I’m worried we may be missing some key subtlety that’s the key to making it work.

For example, your code example shows you sending an object with the property “a”, but I think (maybe) this special parsing may only work with the body property. That said, I see your body property in the JSON examples. But again, I don’t see it in the code.

Can you post a more robust code sample, that accurately shows how you’re constructing the publish statement? There seems to be a careful orchestration of escaping, single/double quotes and double/triple braces involved, in conjunction with the body property.

-John


#17

I am having similar issues. Any help would be greatly appreciated. I have an electron device integrated with Azure IoT Hub. Here is my raw json coming in from my electron device (copied from device dashboard).

{"a":[{"t":1577069189,"p":  0.1},{"t":1577069189,"p":  0.1},{"t":1577069190,"p":  0.1},{"t":1577069190,"p":  0.1},{"t":1577069191,"p":  0.1},{"t":1577069191,"p":  0.1},{"t":1577069192,"p":  0.1},{"t":1577069193,"p":  0.0},{"t":1577069193,"p":  0.1},{"t":1577069194,"p":  0.1},{"t":1577069194,"p":  0.1},{"t":1577069195,"p":  0.1},{"t":1577069195,"p":  0.1},{"t":1577069196,"p":  0.1},{"t":1577069196,"p":  0.1},{"t":1577069197,"p":  0.1},{"t":1577069197,"p":  0.1},{"t":1577069198,"p":  0.1},{"t":1577069198,"p":  0.1},{"t":1577069199,"p":  0.1},{"t":1577069199,"p":  0.1},{"t":1577069200,"p":  0.1}]}

Here is my custom json in the azure webhook config:

{
    "headers": {
        "Content-Type": "text/json"
    },
    "event": "{{{PARTICLE_EVENT_NAME}}}",
    "body": "{\"events\":[{{a}}]}",
    "coreid": "{{{PARTICLE_DEVICE_ID}}}",
    "published_at": "{{{PARTICLE_PUBLISHED_AT}}}",
    "userid": "{{{PRODUCT_USER_ID}}}",
    "fw_version": "{{{PRODUCT_VERSION}}}",
    "public": "{{{PARTICLE_EVENT_PUBLIC}}}"
}

And here is what is put in blob storage on the other side of IoT Hub.

{
	"EnqueuedTimeUtc": "2019-12-23T02:47:15.7630000Z",
	"Properties": {},
	"SystemProperties": {
		"connectionDeviceId": "1b0022001247373333353132",
		"connectionAuthMethod": "{\"scope\":\"hub\",\"type\":\"sas\",\"issuer\":\"iothub\",\"acceptingIpFilterRule\":null}",
		"connectionDeviceGenerationId": "637124464289058823",
		"contentType": "application/json",
		"contentEncoding": "utf-8",
		"enqueuedTime": "2019-12-23T02:47:15.7630000Z"
	},
	"Body": {
		"public": "false",
		"fw_version": "0",
		"userid": "blahblahblah",
		"published_at": "2019-12-23T02:47:15.516Z",
		"coreid": "1b0022001247373333353132",
		"body": "{\"events\":[[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]]}",
		"event": "pressure log",
		"headers": {
			"Content-Type": "text/json"
		}
	}
  1. Why are the array objects coming out [object Object]?
  2. Why is the body coming out as string instead of a json object?