I am trying to write a GraphQL mutation which contains a JSON parameter.
signup( firstName: String! lastName: String tel: String location: JSON email: String! gender: Gender nic: String! userType: UserType isActive: Boolean password: String! )
I Create a JSON object and parse it into sever.
val location = JSONObject()
location.put("latitude","1.232")
location.put("longitude","1.34234")
But it gives errors
Reason: 'location' Not valid JSON (line 1, column 11):\nmutation ($data: UserCreateInput!)
All other parameters(String and enum) are correct. I Checked it.
What I am guessing is going on is this:
Someone on your team has created a custom Scalar JSON with a corresponding coercing. Your GraphQL request variable location should most likely be a string that that gets parsed to json at your server level.
The fact that the error says "location" is not a valid JSON means that your actual GraphQL request variable is coming through like this:
{
"variables": "{"location": "\"location\": {\"latitude\": 12.34, \"longitude\": 12.34}",
...rest
}
GraphQL will pass a value "\"location\": {\"latitude\": 12.34, \"longitude\": 12.34}" to your custom json parser, which is not a valid json
Related
i'm using google gson converter to serialize an json response from an api call into an kotlin object,
one of the json field is a json object
so this didn'T work:
#Expose
#SerializedName("properties")
val properties: List<ThingProperty>,
i got the following error:
Api call failed: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1
but this work fine:
#Expose
#SerializedName("properties")
val properties: JsonObject,
but i then received a JsonObject and not a list of "second level" kotlin object
So the question is, using serialization or something like that,
how can i just received the original json and convert it properly, for this field, into a list of object based on the jsonObject
note, i received my data using retrofit2
Edit:
there is a json example for this item:
"properties":{
"ActiveEvent":{
"name":"ActiveEvent",
"value":false,
"visible":true,
"title":"Active Event",
"type":"boolean",
"#type":"BooleanProperty",
"readOnly":true,
"links":[{
"rel":"property",
"href":"/things/hydroqc-Maison/properties/ActiveEvent"
}]
},
"PreHeatEvent":{
"name":"PreHeatEvent",
"value":false,
"visible":true,
"title":"Pre-Heat Event",
"type":"boolean",
"#type":"BooleanProperty",
"readOnly":true,
"links":[{
"rel":"property",
"href":"/things/hydroqc-Maison/properties/PreHeatEvent"
}]
},
"PostHeatEvent":{
"name":"PostHeatEvent",
"value":false,
"visible":true,
"title":"Post-Heat Event",
"type":"boolean",
"#type":"BooleanProperty",
"readOnly":true,
"links":[{
"rel":"property",
"href":"/things/hydroqc-Maison/properties/PostHeatEvent"
}]
},
"NextEvent":{
"name":"NextEvent",
"value":null,
"visible":true,
"title":"Next Event",
"type":"string",
"readOnly":true,
"links":[{
"rel":"property",
"href":"/things/hydroqc-Maison/properties/NextEvent"
}]
},
I am parsing the below sample json using retrofit in android:
{
"success": true,
"timestamp": 1664080564,
"base": "EUR",
"date": "2022-09-25",
"rates": {
"AED": 3.559105,
"AFN": 86.151217,
"ALL": 116.321643,
"AMD": 404.265711
}
}
As you can see there is no array in this json data, but I want the values of rates as a list or a map so that I can get "AED, AFN, ALL, AMD" as an array too. How can i achieve that using retrofit?
You can define rates as a Map<String, Double> in your data class and Retrofit will automatically parse the rates in form of a Map.
data class MyModel(
val success: Boolean,
val timestamp: Long,
val base: String,
val date: String,
val rates: Map<String, Double>
)
so that I can get "AED, AFN, ALL, AMD" as an array too.
For this you can simply use rates.keys to get all the keys.
Suppose if I've a string as
"[{"key1": value1}, {"key2": value2}, and so on...]"
is there a way to convert it into array:
[{"key1": value1}, {"key2": value2}, and so on...]
Actually I've to post a JSON request in my android app which should be in the following format:
{
"user_id": "5665",
"restaurant_id": "5",
"total_cost": "660",
"food":[
{"food_item_id": "50"},
{"food_item_id": "51"}
]
}
As in the above code, I've to use the array for food key which is stuck in string which I get using the following code:
val gson = Gson()
val menuIds = gson.toJson(listOfMenuIds)
You can create a dataclass for food_items to represent a item in the "food" array.
Then create another dataclass which has "user", "restaurant_id", ... so on... as members, including a member var foods: List
Then pass the second class to the Gson converter
I'm trying to send some data from my node.js server to an android client via FCM(Firebase Cloud Messaging). I get the following error: "data must only contain string values" when sending it. My data contains 2 JSONObjects. Do I have to convert them to strings or what is the way to go here? Thanks.
var message = {
notification:{
"title": "Alert!",
"body": position[0] + " has left Area: " + activeGeofences[i][1].name
},
data:{
Geofence: activeGeofences[i][1],
Position: position[1]
},
token: activeGeofences[i][0]
};
To convert any JSON objects to a string, you can use JSON.stringify(). On the receiving side you can then use JSON.parse() (or your platform's equivalent) to parse the string back into a tree structure.
You can also do this, since it seemed more accurate to me
data: {
key1: "value1",
key2: "value2",
}
you just have to make sure that value1 or value2 or any n key's value pair is string if its a int or anything else it would throw the error. This could save you from parsing thing.
Inside data object data type should be always strings.
If you will pass type a number data type an error will occur.
let message = {
notification: {
title: payload.title,
body: payload.message,
},
data: {
name: 'sds',
type: '2',
_id: 'sdsd',
},
token: deviceToken,
};
So I made an api in laravel and it returns a response like this:
{
"message": "The given data was invalid.",
"errors": {
"email": [
"The email has already been taken."
],
"mobile": [
"The mobile has already been taken."
]
}
}
Can somebody show me how to get the specific values from errors?
You may create model representing your error json and use Gson to parse it. Here is some short example.
data class Errors(
val email: List<String>,
val phone: List<String>
)
data class YourErrorModel(
val message: String,
val errors: Errors
)
fun parseError(response: Response<*>): YourErrorModel? {
val errorBody = response.errorBody()?.string() ?: return null //No error body present
return Gson().fromJson(errorBody, YourErrorModel::class.java)
}
Also don't forget to handle nullable types in your response. And i suggest you to return just string, not array if that is exact error for field.
How about this :
JSONObject errorObject = yourJSONObject.optJSONObject("errors");
if (errorObject != null){
JSONArray emailMsgArray = errorObject.getJSONArray("email");
JSONArray mobileMsgArray = errorObject.getJSONArray("mobile");
String emailMsg= emailMsgArray.getString(0);
String mobileMsg= mobileMsgArray .getString(0);
}