How to handle dynamic json in flutter? - json

{
"status": "error",
"msg": "Please fill all the fields",
"error": {
"device_token": [
"The device token field is required.",
]
"mobile_number": [
"The mobile number field is required."
]
}
}
Here is my json response .How can i handle my error as the error object is dynamic

Keep your JSON content in a variable and access your data as following.
Also, don't forget the comma "," after the "device_token" key
var res = {
"status": "error",
"msg": "Please fill all the fields",
"error": {
"device_token": [
"The device token field is required."
],
"mobile_number": [
"The mobile number field is required."
]
}
};
var deviceToken = (res["error"] as Map)["device_token"].first;
var mobileNumber = (res["error"] as Map)["mobile_number"].first;
print(deviceToken);
print(mobileNumber);

I'd recommend reading JSON and Serialization in the Flutter docs.

I use json_seializable. It is a good way to solve this problem.
Just to mark your models with decorators and generate each method automatically. BTW, this is recommended on flutter documentation.
And also it is really important to add types to your variables and use the Dart compiler's power

Related

People API getting error when Age Ranges is specified

I have tried with People contact create API but I am getting error. I believe the ENUM data is correct
This happens for Skills and Age ranges
{
"error": {
"code": 400,
"message": "person.age_ranges is a read only field.",
"status": "INVALID_ARGUMENT"
}
}
People.People.createContact( {"skills": [
{
"value": "Playing"
}
],
"ageRanges": [
{
"ageRange": "TWENTY_ONE_OR_OLDER"
}
],
})
Answer:
The ageRanges[] field can not be set as it is a read-only field.
More Information:
As per the Person resource in the documentation:
ageRanges[]
object (AgeRangeType)
Output only. The person's age ranges
As this is an output only value, it can not be written to by calling people.createContact.

Looping through Laravel validation errors object in Reactjs

I'm using laravel validation request.
I need to loop the object in the key "errors".
Im using React, but just want understand how to loop it with JavaScript. This is the json object:
{
"message": "The given data was invalid.",
"errors": {
"email": [
"The email field is required."
],
"name": [
"The name field is required."
],
"description" : [
"The description field is required."
]
}
}
Use Object.keys which returns an array of object properties.
Object.keys(validation.errors).forEach(key => {
console.log(validation.errors[key])
})

How to access the json data in react native mentioned in below pattern?

The json data is in the below pattern. And the Json data is coming from backend and getting through api and storing in a state variable.
{
"message": "user created successfully",
"status": "success",
"student": {
"class": "10",
"email": "user#gmail.com",
"name": "User",
"password": "user",
"phone_number": "some phone number",
"school": "1",
"section": "a"
}
}
I have stored the data which is returned through api in a state variable.
constructor(){
super();
this.state = {
jsonData: ''
}
}
And tried accessing using below fashion.
this.state.jsonData.status
but not able to access. How can I access the status value in react?
Please check type of jsonData in state when you call it using typeof or instanceof.
It maybe by you store fetched data in string type without check and manipulating.
If it is string type, convert it using JSON.parse

json-schema-validator custom message

I am using json-schema-validator2.2.6 library to validate my json against json schema. The problem is that it gives generic error messages that are not relevant to me. I want to send custom message or code to user.
Do we have any option like this :
"properties": {
"myKey": {
"type": "string"
**"errorMessage" : "My error message"**
},
}
Or any other way by which I can provide custom error message?
You can create Custom Error Messages in JSON Schema. Sort Of!(In NodeJS). Lets take an Example -
We have to check a key 'DOB' in JSON which should is a required field and it should be in format 'dd-mmm-yyyy'.
Now we have to use two validation in JSON. First, It should be present and it should follow the pattern of `dd-mmm-yyyy'
Now JSON Schema would be
{
"id": "DOBChecker",
"type": "object",
"properties": {
"DOB": {
"type": "string",
"required": true,
"pattern": "/^(([1-9]|0[1-9]|1[0-9]|2[1-9]|3[0-1])[-](JAN|FEB|MAR|APR|MAY|JUN|JULY|AUG|SEP|OCT|NOV|DEC)[-](\d{4}))$/i",
"message": {
"required": "Date of Birth is Required Property",
"pattern": "Correct format of Date Of Birth is dd-mmm-yyyy"
}
}
}
Now If you have got the error while validations. You will get the whole schema back at errors key array and in that access schema object. The Schema Object will contain exactly same keys as the schema defined above.
You can now access it . The failed Validation name will be in the 'name' key. Now you can access your custom Message using
schema.message[name]

apigee - Json payload extract variables

I am confused why the extractVariables rule I am using is returning data as it is. See Below.
The json to Parse is:
{
"callNotificationSubscriptionList": {
"playAndCollectInteractionSubscription": [],
"recognitionInteractionSubscription": [],
"playAndRecordInteractionSubscription": [],
"callDirectionSubscription": [],
"callEventSubscription": [
{
"clientCorrelator": "112345",
"resourceURL": "http:someurl",
"callbackReference": {
"notifyURL": "someotherurlt",
"notificationFormat": "XML"
},
"filter": {
"data1": "data abc",
"data2": "data def",
"data3": "data xyz"
}
}
]
}
}
The rule:
<JSONPayload>
<Variable name="callNotSubL">
<!-- <JSONPath>$.callNotificationSubscriptionList</JSONPath> -->
<JSONPath>$.*</JSONPath>
</Variable>
</JSONpayload>
When I use the value that is commented out, I get no response variable data. If I set the "ignoreUnresolvedVariables" parm to "false", I am returned a failure, so it has no data. Thus, I tried "$.*" With this, I am returned:
[
{
"callbackReference": {
"notifyURL": "someotherurlt",
"notificationFormat": "XML"
},
"filter": {
"data1": "data abc",
"data2": "data def",
"data3": "data xyz"
}
}
]
could this be because the EntryNames are so long? I admit they are long, but they are well under the default values in the JSON Threat Potection Policy.
I did pump this json though a web based JSONPayload parser and $.callNotificationSubscriptionList worked fine as did $.callNotificationSubscriptionList.callEventSubscription[0] which is what I am really after. But, if I can't get the top level right, I can't get the sub-levels at all.
I solved this issue using #Santanu's comments:
It seems that the JSON to parse getting through the policy is not same as what you are expecting? Can you try to assign entire payload to a variable using the AssignVariable policy before the JSON path extraction policy, and check the value of that variable in the debug view? That would help understand what payload value is actually passing through when you are trying to apply the json path extraction policy.
the <Source> tag was "request" and this was a response extract. I removed the <Source> tag and all is well