FCM Field "data" must be a JSON array - arrays

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.

Related

Postgresql - JSON array operators

I am trying read a JSON array in Postgresql with the below mentioned JSON content but I couldn't achieve it. I am able to read the JSON objects like "select data ->> 'items' from events". Please let me know an approach where I can read an array, objects inside arrays & inner arrays etc.
JSON Content:
{"apiVersion":"2.0",
"data":{
"updated":"2010-01-07T19:58:42.949Z",
"totalItems":800,
"startIndex":1,
"itemsPerPage":1,
"items":[
{"id":"hYB0mn5zh2c",
"uploaded":"2007-06-05T22:07:03.000Z",
"updated":"2010-01-07T13:26:50.000Z",
"uploader":"GoogleDeveloperDay",
"category":"News",
"title":"Google Developers Day US - Maps API Introduction",
"description":"Google Maps API Introduction ...",
"tags":[
"GDD07","GDD07US","Maps"
],
"thumbnail":{
"default":"http://i.ytimg.com/vi/hYB0mn5zh2c/default.jpg",
"hqDefault":"http://i.ytimg.com/vi/hYB0mn5zh2c/hqdefault.jpg"
},
"player":{
"default":"http://www.youtube.com/watch?vu003dhYB0mn5zh2c"
},
"content":{
"1":"rtsp://v5.cache3.c.youtube.com/CiILENy.../0/0/0/video.3gp",
"5":"http://www.youtube.com/v/hYB0mn5zh2c?f...",
"6":"rtsp://v1.cache1.c.youtube.com/CiILENy.../0/0/0/video.3gp"
},
"duration":2840,
"aspectRatio":"widescreen",
"rating":4.63,
"ratingCount":68,
"viewCount":220101,
"favoriteCount":201,
"commentCount":22,
"status":{
"value":"restricted",
"reason":"limitedSyndication"
},
"accessControl":{
"syndicate":"allowed",
"commentVote":"allowed",
"rate":"allowed",
"list":"allowed",
"comment":"allowed",
"embed":"allowed",
"videoRespond":"moderated"
}
}
]
}
}
just use one arrow
# select blob->'data'->'items'->0->'title' from so_test;
?column?
----------------------------------------------------
"Google Developers Day US - Maps API Introduction"
(1 row)
exchange blob with column name

How to create a JSON array as string from groovy objects?

I have some data in Groovy's objects which I need to convert to a JSON string array. The final result should be..
[
{
"keys":{
"passcode": "12345"
},
"values":{
"EmailAddress": "john#doe.com",
"message": "Hello, is it me you are looking for?"
}
}
]
I found this post but the accepted answer which uses JSON Builder didn't make sense and the second answer using JSON converter didn't work...
def deJson = [
keys: [
passcode: secretCode
],
values: [
EmailAddress:emailData.to[0],
message: content.message
]
] as grails.converters.JSON
This created a JSON object when I needed an array.
Can anyone suggest how I can use JSON Builder or JSON converter to create an array string like I have above?
JSON is specific to Grails (as mentioned in one of the comments in the answer from the previous post). You should have followed the question itself in the post, that has the answer.
Using groovy.json.JsonBuilder, the expected way to serialize would be:
def jsonObj = [
keys: [
passcode: secretCode
],
values: [
EmailAddress:emailData.to[0],
message: content.message
]
]
// In order to get a json array, use a list containing the object
new JsonBuilder([jsonObj]).toPrettyString()

Elasticsearch watcher email array value

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

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"} }
} );

Convert json string to array in php

Im using curl the retrieve a json string from a log file. I was able to retrieve the string but it comes in this format.
[{"candy":"lollipop","timestamp":1385504260,"color":"red"}]
[{"candy":"laffytaffy","timestamp":1385504260,"color":"blue"}]
When i try to convert it to an array using decode, its as if decode isn't working. I was hoping someone could assist me in figuring this out.
First, this json seems to be wrong. It should has this format.
[{"candy":"lollipop","timestamp":1385504260,"color":"red"},
{"candy":"laffytaffy","timestamp":1385504260,"color":"blue"}]
Maybe it's the problem.
#Carlos is right your JSON is invalid. You have 2 separate arrays instead of an array of objects.
It should look like this instead:
[
{
"candy": "lollipop",
"timestamp": 1385504260,
"color": "red"
},
{
"candy": "laffytaffy",
"timestamp": 1385504260,
"color": "blue"
}
]
Try like this
<?php
$jsonData = '[{"candy":"lollipop","timestamp":1385504260,"color":"red"},
{"candy":"laffytaffy","timestamp":1385504260,"color":"blue"}]';
$decodedJson = array();
$decodedJson = json_decode($jsonData);
print_r ($decodedJson);
?>

Resources