How to parse a message in Azure IOT hub logic app - azure

I am trying to understand how to get content into a logic app email (or into the conditioning search in the logic app).
I have an IOT device sending a standard message to the hub (the demo temperature app). This works fine.
I have added a service bus and routing and again all ok.
I then want a logic app to send an email either using some of the content of the message, or even to use the conditional processing in the logic app.
I am using the connector in the logic app to the service bus.
When I try emailing dependant on the conditional "Content contains Startup" the email goes successfully but only down the false route.
When I try to put a JSON parse step in is returns invalid as the content isn't in JSON format.
The Error message:
InvalidJSON. The 'content' property of actions of type 'ParseJson'
must be valid JSON. The provided value
'e3sid2ZNZXNzYWdlVHlwZSI6IFN0YXJ0dXAsInRlbXBlcmF0dXJlIjogMzcuMzMsImh1bWlkaXR5IjogNjEuMjV9'
cannot be parsed: 'Unexpected character encountered while parsing
value: e. Path '', line 0, position 0.'.

It is a little hard to understand exactly where you problem is occurring but it sort of looks like your string is in base64.
You can try converting it using:
base64ToString(triggerBody()?['whateveryourvariablenameis'])
Check this out for screen shots and more information:
Azure Logic App service bus message content

Related

Azure logic apps - How to read message received from topic and insert into Salesforce

I am creating a Azure Logic App. The workflow is triggered by "When a message is received in a topic subscription (auto-complete)".
The message is simple JSON format:
{
"FirstName":"Test",
"LastName":"User",
"CreatedAt":"2022-03-26T19:27:07.6885324-04:00"
}
I want to insert the record into Salesforce. But I am unable to insert the record as I am not able to extract properties from message.
You can use Parse_JSON in this case. As the result we receive from the trigger is of base64, we need to use
decodeBase64(triggerBody()?['ContentData'])
Here is the screenshot of my logic app:
RESULT:

Send Query or SP output as HTML mail from Azure data factory

I'm trying to implement Azure data factory pipeline. From there trying to execute one SP and need to send output in HTML format from Azure data factory through Logic App. Facing issue while sending output to Web task in ADF.
Any hint ?
Error details
Error code
2108
Troubleshooting guide
Failure type
User configuration issue
Details
{"error":{"code":"InvalidRequestContent","message":"The request content is not valid and could not be deserialized: 'Unexpected character encountered while parsing value: <. Path '', line 0, position 0.'."}}
Source
In Body option of the Setting TAB in Web Activity, Instead of adding the JSON in the "Dynamic Content", add JSON as usual in the box.
Refer below screen shot:

Why IoT Hub message is not decoded corretly in storage JSON blob?

I am sending a string with an Azure Sphere dev kit using the provided function:
AzureIoT_SendMessage("Hello from sample App")
The message is sent to an IoT Hub and then routed to a storage blob with JSON encoding. If I look at the blob storage I get the following:
{"EnqueuedTimeUtc":"2019-05-22T12:33:42.2320000Z","Properties":{},"SystemProperties":{"connectionDeviceId":"fbea*****************6d**********************9c0","connectionAuthMethod":"{\"scope\":\"device\",\"type\":\"x509Certificate\",\"issuer\":\"external\",\"acceptingIpFilterRule\":null}","connectionDeviceGenerationId":"63************22","enqueuedTime":"2019-05-22T12:33:42.2320000Z"},"Body":"SGVsbG8gZnJvbSBzYW1wbGUgQXBw"}
The field "body" does not show at all the string sent ("Hello from sample App") but it shows "SGVsbG8gZnJvbSBzYW1wbGUgQXBw". Why is this happening? And how can I fix it?
I found that if I format the storage as AVRO (instead of JSON) the string is rendered correctly however the message becomes (literally) a blob and it cannot be used in streaming service such as powerBI (for example). However the message can be found, with some other mess stuff, in the blob (see picture below with the default string message)
See Microsoft's IoT Hub message routing documentation - specifically the Azure Storage section. It says "When using JSON encoding, you must set the contentType to application/json and contentEncoding to UTF-8 in the message system properties. Both of these values are case-insensitive. If the content encoding is not set, then IoT Hub will write the messages in base 64 encoded format."
This blog post further expands on the topic explaining that content type and encoding need to be set as specific headers.
On setting the headers:
If you are using the Azure IoT Device SDKs, it is pretty straightforward to set the message headers to the required properties. If you are using a third-party protocol library, you can use this table to see how the headers manifest in each of the protocols that IoT Hub supports​:
FWiW, using PowerQuery (PQ) within PowerBi, I was able to decode the {body:xxxencoded_base64...} portion of the JSON file sent to blob by Azure IoT Hub.
My steps in PowerBI:
- Connect to blob account, container collecting the JSON files.
- Within PQ, click the double-arrow expand on the initial binary column
- For me, Column10 was the {body:xxx} column. Using PQ's Replace Values function, I removed the prefix "{body:" and the final "} - leaving just the encoded string.
- Create a new column in PQ, using this M-code: =Binary.FromText([Column10],BinaryEncoding.Base64)
- It's now a new Binary column, click the double-arrow and expand the binary. It'll reveal the decoded JSON table, complete w/ all your IoT telemetry.
HTHs
See also pending feature request for IoT-Hub:
feature request

Getting content from service bus in logic apps

I am new to Azure logic apps. I have a service bus and pass a json object message to that service bus then I set up an action in logic apps to listen to my service bus. So everytime a new message come in to that service bus my logic apps will pick it up and send it to http.
My question is how can I grab the property from the message in service bus and pass it to my http action. I tried this
“Id” : “#{json(triggerBody()[‘ContentData’]).id}”
but it's not working
Who and how is sending the message on the queue?
I read a json message property (DestinationPath) in this way:
#{json(base64ToString(triggerBody()?['ContentData'])).DestinationPath}
Here is how my Logic App looks like
and in my case the message is sent from an Azure webjob as a BrokeredMessage:
string jsonMessage = JsonConvert.SerializeObject(myObject);
Stream streamMessage = new MemoryStream(Encoding.UTF8.GetBytes(jsonMessage));
BrokeredMessage msg = new BrokeredMessage(streamMessage);
client.Send(msg);
My precise setup to decrypt the base 64 message using the interface. Easy enough to type into the expression builder.
json(base64ToString(triggerBody()?['ContentData']))
ContentData of Service Bus messages is Base64 encoded, so you need to decode it first, e.g.
“Id” : “#{json(base64ToString(triggerBody()?[‘ContentData’])).id}”
Logic app now has expressions to decode Base 64 encoded value.
My requirement was to decode the encoded ServiceBus message to an Azure Function. I solved this using Logic App Expression, decodeBase64() which can accept a dynamic content of type string, in this case the 'Content'- Content of the message, and returns the decoded json string.
decodeBase64(triggerBody()?['ContentData'])
Find attached the screen shots for reference.
In the place holder for input to the action, include an expression and choose decodeBase64()
Get back to Dynamic Content tab to select 'Content' available from previous step, on hitting OK the expression would get generated

Mobile Service Configuration: MS_NotificationHubConnectionString Contains Invalid Setting Key "entitypath"

I'm trying to create a new Azure Mobile Service, however when trying to call a Custom API it generates the following error in the logs of the service.
An error occurred creating push for user scripts:
azure.notificationHubService could not be created. HubName:
"servicenamehub" ConnectionString
"Endpoint=sb://servicenamehub-ns.servicebus.windows.net/;
SharedAccessKeyName=DefaultFullSharedAccessSignature;
SharedAccessKey={accesskey};EntityPath=servicenamehub": Error from
create-Error: Invalid connection string setting key "entitypath".
The error only seems to generate when making an API call, not when making a call on a table.
The MS_NotificationHubConnectionString is where this connection string is stored, however it was auto generated along with the service hub and isn't editable in the service configuration.
The EntityPath key doesn't appear in the MS_NotificationHubConnectionString of any of my older services. The Mobile Service has a JavaScript back end.
How do I prevent this error or remove the EntityPath key from the connection string?
Currently, here is a workaround: we login Kudu console site of the Mobile Service backend, modify MS_NotificationHubConnectionString in the script which will create notification hub service directly in source code.
login in Kudu console site, whose url should be https://<your_mobile_service_name>.scm.azure-mobile.net/DebugConsole
In the file system list in the page, enter to the path D:\home\site\wwwroot\node_modules\azure-mobile-services\runtime\push, edit the file pushadapter.js
Add following code to the start of the function PushAdapter in this script around line 22:
var string = options.MS_NotificationHubConnectionString;
var index = string.indexOf('EntityPath');
options.MS_NotificationHubConnectionString = index>0?string.slice(0,string.indexOf('EntityPath')-1):string;
Any further concern, please feel free to let me know.

Resources