Google Firebase Cloud Messaging HTTP Protocol Payload Data json key - ios

I have problem with sending some key in data Payload to iOS device
Here's my json to send https://fcm.googleapis.com/fcm/send -> #POST
{
"time_to_live": 216000,
"registration_ids": [
"device token"
],
"content-available" : true,
"notification": {
"body": "Some msg",
"title": "title"
},
"data": {
"code":11,
"orderStatus": {
"status": 1,
"id": 5102
}
}
}
Headers in POST request:
Authorization=key=myFirebaseKey
Content-Type=application/json
Problem with key orderStatus, in client side I get this key as a String
orderStatus = "{\"id\":5102,\"status\":1}";
Can I tell Firebase to send this key as an Object?
Or all keys in data must be simple key=value keys?
I mean value only string integer etc...?

The documentation for downstream JSON message syntax says this about the key-value pairs in the data object:
The key should not be a reserved word ("from" or any word starting
with "google" or "gcm"). Do not use any of the words defined in this
table (such as collapse_key).
Values in string types are recommended. You have to convert values in
objects or other non-string data types (e.g., integers or booleans) to
string.

Related

How to correctly send array with objects in JSON Swift iOS

Hello I am beginner and trying to understand how to send an array with objects. Does the server understand what an array is like Int, Strings or Booleans? Do you have to send the array in a string for JSON? What I do not understand?
var productsResult = ""
let encoder = JSONEncoder()
let productObject = ProductUsed(name: "Custom name", reason: "This Reason", apply_way: "Intravenous infusion", dosing: "2x", date_start: "22-02-1999", date_end: "22-03-2003")
do {
let result = try encoder.encode([productObject])
if let resultString = String(data: result, encoding: .utf8) {
print(resultString)
productsResult = resultString
}
} catch {
print(error)
}
json["products_used"] = productsResult
And I sent to server with parameters like this:
parameters: ["pregnancy_week": 0, "body_height": 198, "initials": "John Appleseed", "heavy_effect": false, "sex": "Male", "pregnancy": false, "month_of_birth": 3, "reaction": "No option checked", "additional_info": "Eeee", "products_used": "[{\"date_end\":\"22-03-2003\",\"dosing\":\"2x\",\"date_start\":\"22-02-1999\",\"apply_way\":\"Intravenous infusion\",\"name\":\"Custom name\",\"reason\":\"This Reason\"}]", "description": "Eeee", "result": "Recovery without lasting consequences", "year_of_birth": 1983, "day_of_birth": 11, "issue_date": "15-11-2020", "body_weight": 78]
but printed "resultString" in log and looks good...
[{"date_end":"22-03-2003","dosing":"2x","date_start":"22-02-1999","apply_way":"Intravenous infusion","name":"Custom name","reason":"This Reason"}]
What's wrong in my code and why I have " \ " between words in "products_used" key?
JSON, unlike XML, does not specify the structure and type explicitly. This means that the server must know what JSON data to expect.
In JSON there are a few value types (https://www.w3schools.com/js/js_json_syntax.asp):
a string
a number
an array
a boolean
null
a JSON object (a dictionary with tags like { "first" : "John", "last" : "Doe" }). This allows nesting.
A JSON object is a set of tag-value pairs. The tag and value are separated by : and pairs are separated by ,.
An array is a list of JSON values. So for example [ "hello", world" ] is a JSON array with 2 strings and [ 12, 54 ] is a JSON array with two numbers.
Your parameter list ["pregnancy_week": 0, "body_height": 198, ... is not an array but a dictionary instead. A Swift dictionary is translated to an JSON object, not a JSON array.
The \ you see printed acts as escape character. This escape character is used to allow you to have a " inside the string.
That's just a few things that I hope will help understand things a bit better. Your questions are pretty basic, which is fine and it's great you want to understand things. But instead of us explaining everything here, I think it would be best if you'd read about the structure of JSON and how JSON works in Swift on your own.

FSharp.Data.JsonProvider optional property

I'm using the FSharp.Data.TypeProvider but I have an optional property that is not rendered as such.
I have this example JSON:
[
{
"error": {
"message": "Error validating access token: Session has expired on Friday, 24-Jul-20 16:00:00 PDT. The current time is Friday, 24-Jul-20 16:06:14 PDT.",
"type": "OAuthException",
"code": 190,
"error_subcode": 463,
"fbtrace_id": "A0yQG8l8ggauD5PMbYSnEyA"
}
},
{
"error": {
"message": "Error validating access token: Session has expired on Friday, 24-Jul-20 16:00:00 PDT. The current time is Friday, 24-Jul-20 16:06:14 PDT.",
"type": "OAuthException",
"code": 190,
"error_subcode": 463,
"fbtrace_id": "A0yQG8l8ggauD5PMbYSnEyA"
}
},
{
"data": [
{
"id": "17841511906082530"
}
]
}
]
The field error is read as Optional<Error> meanwhile the field data is Datum [], It should be Optional<Data>, this error seems to have something to do with the array, if I change the value of data to another type, like a number, it correctly infers the type.
Maybe is some cache that's wrong? How could I reset the TypeProvider cache?
OR am I doing something else wrong?
This issue is also reported in the github project: https://github.com/fsharp/FSharp.Data/issues/1322
That's expected behavior.
In your JSON string, data is an array, an empty array indicates None, thus the field Data is read as array. That makes sense.
The fact that each element of that array belongs to type Datum also makes sense because Datum is the single form of Data (plural form). Indeed, if you rename data to dogs you will see it is read as Dog [].
Update: I don’t think we can tell the type provider to infer the field data as Option<Datum[]>. To check whether it is actually an empty array or not existed, we need to examine the JsonValue field:
match node.JsonValue with
| JsonValue.Record [|("data", _)|] ->
// yep, we have field data here
| _ ->
// nope, field data is missed

restassured - parsing response and searching by value

I have following REST API response:
"items":
[
{
"empid": "1234",
"name": "Santosh",
"hiredby": "Mark",
"date": "2017-01-31,00:19:41 PST",
},
{
"empid": "5678",
"name": "Kumar",
"hiredby": "Bob",
"date": "2017-01-31,08:30:31 PST"
}
]
My query is : - How do i get empid based on querying name as Kumar.
For example: I need to find "Kumar" name and get his empid. (that is, search by name and get his empid as response) I'm able to get the response and store it in Response object. but, from response object how can i traverse and query to get the required value.
Also,
I tried by retrieving as:
String name = get(REST_ENDPOINT).then().body("items.name",hasItems("Kumar")).extract().path("items.empid").toString();
when i print the response i get collection of the empid like [1234,5678], where as my expectation is to get only 5678.
Do I need to parse via JSONArray and JSONObject and iterate the response?
Please suggest.
You can use something like this
response1.jsonPath().getList("collect { it.credentials.findAll { it.credentialType == 'Ban User Name'}.credentialId }.flatten()")

Can Multidimensional Hash Values be inserted into InfluxDB?

In InfluxDB, can you post multidimensional hash values to the database?
For example, this hash:
{
"field1": "value1",
"field2": {
"field2a": "value2a",
"field3a": "value3a"
}
}
If you can, how do you do this? When I try via the http admin interface, it neither errors out or returns success.
Multidimensional values are not supported by InfluxDB. Points have fields that are a set of key-value pairs. Currently, values can only be of type float, integer, boolean, or string. InfluxDB has no concept of nested key-values. The relevant source is here.
A workaround would be to store the JSON as a string literal by escaping all double quote characters (e.g. \"). Then implementing whatever functionality required in the client.
curl -h -XPOST 'http://localhost:8086/write' -d '
{
"database": "test",
"retentionPolicy": "default",
"points": [
{
"name": "json_blob",
"tags": {
"tag1": "tag-value-a",
"tag2": "tag-value-b"
},
"fields": {
"value": "{\"field1\":\"value1\",\"field2\":{\"field2a\":\"value2a\",\"field3a\":"value3a\"}}"
}
}
]
}'

prolems in converting from json formatted String to json object

I have a web servlet that returns a json which is stored in my bb mobile app as string in json format....
Now I want to parse the string to extract values from it...
using
JSONObject jsobject = new JSONObject(jsonString);
returns an error:
json must start with {
My JSON generated is in this format
[
{"LASTNAME":"akre","FIRSTNAME":"swapnil"},
{"LASTNAME":"akre","FIRSTNAME":"swapnil"},
{"LASTNAME":"akre","FIRSTNAME":"swapnil"}
]
which is corect format as verified by jsonlint.com....
yes, json must start with { and end with } , what you can do is you put your json array in
{}, so it will be parsed correctly. JsonLint parses the partial json as well thats why it is showing it as correct. You can try like following
yes, json must start with { and end with } , what you can do is you put your json array in {}, so it will be parsed correctly as jsonobject. JsonLint parses the partial json as well thats why it is showing it as correct
{
"data": [
{
"LASTNAME": "akre",
"FIRSTNAME": "swapnil"
},
{
"LASTNAME": "akre",
"FIRSTNAME": "swapnil"
},
{
"LASTNAME": "akre",
"FIRSTNAME": "swapnil"
}
]
}
I'm assuming you're using this library.
Your JSON is an Array, so you'll have to use the JSONArray class to parse it

Resources