Elasticsearch watcher email array value - arrays

I am working on ELK watcher to create an alert that sends an array of value transformed using 'transform' mapping.
"transform": {
"script": "return [ err_yest : ctx.payload.aggregations.errorcount.buckets.collect { [err_count:it.doc_count, list_errors: it.errs.buckets.collect{[emsg:it.key,emsc:it.doc_count]}] } ]"
},
Is there a way to print the array values in the body of email alert using any looping method? I tried groovy scripting, but got an error saying it's unsupported. All I could do is manually printing the values in array like below.
"body" : {
"html": "<table width='400px' border='1'><thead><tr><th colspan='4'>Error Messages</th></tr><tr><th colspan='2'>Yesterday</th><th colspan='2'>Today</th></tr></thead><tbody><tr><td>{{ctx.payload.err_yest.0.list_errors.0.emsc}}</td><td align='center'>{{ctx.payload.err_yest.0.list_errors.0.emsg}}</td><td>{{ctx.payload.err_yest.1.list_errors.0.emsc}}</td><td align='center'>{{ctx.payload.err_yest.1.list_errors.0.emsg}}</td></tr><tr><td>{{ctx.payload.err_yest.0.list_errors.1.emsc}}</td><td align='center'>{{ctx.payload.err_yest.0.list_errors.1.emsg}}</td><td>{{ctx.payload.err_yest.1.list_errors.1.emsc}}</td><td align='center'>{{ctx.payload.err_yest.1.list_errors.1.emsg}}</td></tr><tr><td>{{ctx.payload.err_yest.0.list_errors.2.emsc}}</td><td align='center'>{{ctx.payload.err_yest.0.list_errors.2.emsg}}</td><td>{{ctx.payload.err_yest.1.list_errors.2.emsc}}</td><td align='center'>{{ctx.payload.err_yest.1.list_errors.2.emsg}}</td></tr></tbody></table>"
},

You need to use Mustache templating
The syntax is something like this :
{{#ctx.payload.err_yest}} {{#list_errors}} {{emsc}} {{/list_errors}}{{/ctx.payload.err_yest}}
This will loop over all the objects in err_yest then loop over all the list_errors for a err_yest object and display esmc

Related

JSON webhook through WP Automator - Custom Syntax

I have this JSON code:
{
"Subscribers": [
{
"EmailAddress":"yikes#to.com",
"CustomFields": [
],
"Lists": [
200575230
]
}
]
}
I tested it on Postaman, it works - it should push email address of every new registered user to the Campaigner.
But, for automation I use WP automator webhooks in Wordpress. For creation of this automation I can't use this above code, but code formatted per Automator "syntax".
In Automator, for example - this code below:
data": {
"customer": 123,
"first_name": "AutomatorWP",
}
Is mapped like this:
data/customer
data/first_name
And for the brackets - "The brackets ([]) place the information inside an array, so you need to place the array index like “data/0/first_name” to use this “first_name” parameter on tags." - Documentation
Does anyone knows or can figure out how to above first code "translate" to this Automator layout?
I tried with:
Subscriber/EmailAddress and Subscriber/0/EmailAddress - but I can't figure out how to send email to the right list, as simple as it is in clean JSON code
Any help appreciated
I tried any possible variation, always same error: Invalid Payload
I don't have any idea anymore, so maybe somebody worked earlier with this Automator "syntax"

Iterating a JSON array in LogicApps

I'm using a LogicApp triggered by an HTTP call. The call posts a JSON message which is a single row array. I simply want to extract the single JSON object out of the array so that I can parse it but have spent several hours googling and trying various options to no avail. Here's an example of the array:
[{
"id": "866ef906-5bd8-44d8-af34-0c6906d2dfd7",
"subject": "Engagement-866ef906-5bd8-44d8-af34-0c6906d2dfd7",
"data": {
"$meta": {
"traceparent": "00-dccfde4923181d4196f870385d99cb84-52b8333f100b844c-00"
},
"timestamp": "2021-10-19T17:01:06.334Z",
"correlationId": "866ef906-5bd8-44d8-af34-0c6906d2dfd7",
"fileName": "show.xlsx"
},
"eventType": "File.Uploaded",
"eventTime": "2021-10-19T17:01:07.111Z",
"metadataVersion": "1",
"dataVersion": "1"
}]
Examples of what hasn't worked:
Parse JSON on the array, error: InvalidTemplate when specifiying an array as the schema
For each directly against the http output, error: No dependent actions succeeded.
Any suggestions would be gratefully received.
You have to paste the example that you have provided to 'Use sample payload to generate schema' in the Parse JSON Connector and then you will be able to retrieve each individual object from the sample payload.
You can extract a single JSON object from your array by using its index in square brackets. E.g., in the example below you'd need to use triggerBody()?[0] instead of triggerBody(). 0 is an index of the first element in the array, 1 - of the second, and so on.
Result:

ARM template array parameter

I have an ARM template with a web app alerting rule, where I want to be able to configure which e-mails get the alerts.
The snippet for the e-mail alerting action is this:
"action": {
"odata.type": "Microsoft.Azure.Management.Insights.Models.RuleEmailAction",
"sendToServiceOwners": false,
"customEmails": [
"email1#example.com",
"email2#example.com"
]
}
The same template is used for setting up production, test, and dev environments. So I would like to use a parameter for the e-mail alerting.
How can I generate an array to be used as the "customEmails" property based on either a comma separated string, or an array type parameter?
I have tried "customEmails": "[array(parameters('AlertEmailRecipients'))]", and also
"customEmails": [
[array(parameters('AlertEmailRecipients'))]
]
but neither work. I don't know how to tell it that the "customEmails" property value should come from a parameter.
I used the following using an array parameter:
parameter declaration:
"customEmails": {
"type": "array",
"metadata": {
"description": "alert email addressess"
}
}
in the parameters file:
"customEmails": {
"value": [
"email1#domain.com",
"email2#domain.com"
]
}
usage:
"customEmails": "[parameters('customEmails')]"
I found a solution. The main problem was that my comma separated list of e-mail addresses had a space after each comma.
The way I have implemented it now is like this:
Define a string parameter with a comma separated list of e-mail addresses. Don't have spaces in the list.
Define a variable like this:
"customEmails" : "[split(parameters('AlertEmailRecipients'), ',')]"
and then reference that variable in the alerting action:
"action": {
"odata.type": "Microsoft.Azure.Management.Insights.Models.RuleEmailAction",
"sendToServiceOwners": false,
"customEmails": "[variables('customEmails')]"
}
The example actually does this, but doesn't make it clear the the list of e-mails can't contain spaces.

FCM Field "data" must be a JSON array

Hi im working with postman to make my json object FCM message:
But when i try to send:
{
"to":"fzvihT7dFUI:APA91bFVhnWAxXVjlWiiHIs9ZUyL1DE2hZO6GpItJtReh3hcKF1kD6mLuQq9fNP9xvS5bOFWUOG-OW-uyOedc1C43m8jfvD4OOfsBYuMbM7t1-dZEy2kQcuv3gJw6dhneVus2AR_hQHQ",
"data":[
{
"time":1501385514224,
"CC":"1030626890"
}
],
"notification":{
"body":"SPO2:95 \nPulso:75",
"title":"El paciente Daniel Ortiz nesecita asistencia"
}
}
The response its:
Field "data" must be a JSON array:
[{"CC":"1030626890","time":1501385514224}]
But i know the [{"CC":"1030626890","time":1501385514224}] its a array, i dont understand the problem.
What i made wrong?
From the Firebase Cloud Messaging documentation, it seems like data should be a JSON object:
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data" : {
"Nick" : "Mario",
"body" : "great match!",
"Room" : "PortugalVSDenmark"
},
}
I'm not sure why the error message talks says it needs to be an array. It's like meant as an "associative array" which is really just another term for a JSON object.
I might be too late but for those who still face this problem , following change saved me.
By adding JSON_FORCE_OBJECT to json_encode() it will add missing braces so it should be something like this:
json_encode($fields ,JSON_FORCE_OBJECT));
That's it.
In case you serialize sql/api results data that each item of the array has a numeric key using php, for example an array like:
$results = [
['name'=>'john', 'durname'=>'doe'],
['chuck'=>'john', 'durname'=>'Norris'],
]
Before turning into FCM data try:
$results = [
["time":1501385514224, "CC":"1030626890"]
["time":1501385514334, "CC":"1030126890"]
]
$fcmData = [
"to"=>"fzvihT7dFUI:APA91bFVhnWAxXVjlWiiHIs9ZUyL1DE2hZO6GpItJtReh3hcKF1kD6mLuQq9fNP9xvS5bOFWUOG-OW-uyOedc1C43m8jfvD4OOfsBYuMbM7t1-dZEy2kQcuv3gJw6dhneVus2AR_hQHQ",
"data":[
"data" => $results
],
"notification" => [
"body"=>"SPO2:95 \nPulso:75",
"title"=>"El paciente Daniel Ortiz nesecita asistencia"
]
]
$fcmData = json_encode($fcmData);
// Send push notification here
In case you are using laravel with brozot/laravel-fcm, a good approach is this one.
Thje bottom line is that fcm/push notification data MUST be an associative array.

Referencing arrays that are nested within multiple objects within MongoDB with Express js

So in my MongoDB Collection I have this structure:
"_id" : "Object("-----------")
"name" : "John Doe"
"tool" : {
"hammer" : {
"name" : "hammer 1",
"characteristics" : [
{
"length" : "9 inches"
},
{
"weight" : "4 pounds"
}
]
I know the data may seem a little strange but I can't put the actual data online so I had to input some dummy data. So essentially what I would like to do is be able to update the array that is nested within those objects. So I would like to be able to update the weight or add a new characteristic that I haven't previously entered into it. So for example, add in "metal" : "steel" as a new entry into the array. Currently I'm using a Rest API built in Node.js and Express.js to edit the db. When I was trying to figure out how to dig down this deep I was able to do it with an array at the highest level, however I haven't been able to figure out how to access an array when its embedded like this. So what I was wondering if anybody knew if it was even possible to edit an array this far down? I can post code from controller.js and server.js file if needed but I figured I'd see if it's even possible to do before I start posting it. Any help would be greatly appreciated!
You can use findAndModify to $push it into the array. You have to specify the path precisely though:
db.tools.findAndModify( {
query: { name: "John Doe"},
update: { $push: { tool.hammer.characteristics: {metal: "steel"} }
} );

Resources