How to compare json response with mysql query in JAVA - java

I am trying to comparing Json response with my sql query result.
Json Response:
[
{
"id": "1023313",
"empno": "073130425890",
"empForma": "0",
"status": "success",
"message": ""
},
{
"id": "43781661",
"empno": "073130425890",
"empForma": "0",
"status": "success",
"message": ""
},
{
"id": "63535011",
"empno": "000004501951",
"empForma": "2",
"status": "success",
"message": ""
},
{
"id": "184570",
"status": "success",
"message": "No Data Available"
}
]
I have stored SQL Query Output in string:
queryResult:
[1023313 073130425890 0, 43781661 073130425890 0, 63535011 000004501951 2]

Comparing strings will be difficult and error prone.
Better parse both your json and query result to POJOs and use equals method to compare them.

Related

How to find JSON path if the JSON tree has unique Key in the path of Key whose value is to be extracted?

I wanted extract value of first "jobId" but "76ff0515-a200-444a-b5b3-e81e81360b75":" this value is updated every other day.
Can somebody help me in writing JSON path for extracting value of jobId which can be used in Rest Assured testing using selenium?
{ "76ff0515-a200-444a-b5b3-e81e81360b75": { "state": "COMPLETED", "jobId": "76ff0515-a200-444a-b5b3-e81e81360b75", "jobType": "UNMIGRATE_BUSINESS", "message": "UNMIGRATE_BUSINESS- Operation performed on: 3 users", "duration": "00H:00M:03S" }, "c13cc2c0-5ac7-4939-ad2d-901e233f3651": { "state": "COMPLETED", "jobId": "c13cc2c0-5ac7-4939-ad2d-901e233f3651", "jobType": "UNMIGRATE_BUSINESS", "message": "UNMIGRATE_BUSINESS- Operation performed on: 0 users", "duration": "00H:00M:01S" }, "00a286e0-00e4-4373-9a31-a236d5145b3b": { "state": "COMPLETED", "jobId": "00a286e0-00e4-4373-9a31-a236d5145b3b", "jobType": "UNMIGRATE_BUSINESS", "message": "UNMIGRATE_BUSINESS- Operation performed on: 3 users", "duration": "00H:00M:02S" }, "b4ceb8ee-c56d-4a8e-8327-e5cfde452c23": { "state": "ERROR", "jobId": "b4ceb8ee-c56d-4a8e-8327-e5cfde452c23", "jobType": "ADD_BIZ_UUID", "duration": "00H:00M:02S" }, "b0e86631-e921-4606-81df-80f372993f64": { "state": "COMPLETED", "jobId": "b0e86631-e921-4606-81df-80f372993f64", "jobType": "ADD_BIZ_UUID_FOR_ALL", "message": "OK", "duration": "00H:01M:55S" } }

How can I control the data I get from a JSON depending on its format in Java?

I'm trying to parse a JSON file into Java objects. I'm using OkHttpClient to get this JSON. The issue is, that the JSON returned can have two different formats. If the Query parameters on the URL are correct, the JSON format will be like:
{
"debugOutput": {
"pathCalculationTime": 0,
"pathTimes": [],
"precalculationTime": 0,
"renderingTime": 0,
"timedOut": true,
"totalTime": 0
},
"error": {
"id": 0,
"message": "PLAN_OK",
"missing": [],
"msg": "string",
"noPath": true
},
"plan": {
"date": 0,
"from": {},
"itineraries": [],
"to": {}
},
"requestParameters": {
"app_id": "string",
"app_key": "string",
"arriveBy": "string",
"date": "string",
"fromPlace": "string",
"maxWalkDistance": "string",
"mode": "string",
"time": "string",
"toPlace": "string"
}
}
But if anything in the request is not correct, I'll get a JSON file like this one:
{
"status": "error",
"error":"Unexpected error"
}
As the only data I wanna store if the query parameters are correct is the one on the object plan, so far I've done this:
JsonObject jsonObject = JsonParser.parseString(jsonData).getAsJsonObject(); //jsonData is the Json file as a String.
JsonObject planJson = jsonObject.get("plan").getAsJsonObject();
plan = gson.fromJson(planJson.toString(), Plan.class);
But this obviously throws an Exception if the JSON data is like the second format.
How can I know which format will I get?

Add data from Api response into Firebase Database in Android

This I want to add data to firebase database through Volley API response in Android.
"data": [{
"id": 1,
"full_name": "abc",
"email": "abc999#gmail.com",
"country_code": "+91",
"phone": 2147483647,
"profile_pic": "",
"type": 0,
"status": 1,
"reset_token": "",
"verify_token": "$2y$10$YXCZ1yteimLatQnAszJTi.HOGDZrr9xjKJtIDNs3uagX3elFUlC.2",
"created_at": "2019-05-07 07:53:29",
"updated_at": "2019-05-08 12:57:45",
"deleted_at": null
}, {
"id": 2,
"full_name": "xyz",
"email": "xyz#gm.com",
"country_code": "+91",
"phone": 2147483647,
"profile_pic": "",
"type": 0,
"status": 1,
"reset_token": null,
"verify_token": "$2y$10$Dtk.BdqBgHFyGcpj9bHyI.JRPJlc90Qmhxx0Imm0Mzzd3x6QchDMi",
"created_at": "2019-05-07 08:34:39",
"updated_at": "2019-05-07 08:34:39",
"deleted_at": null
}, {
"id": 3,
"full_name": "abc",
"email": "abc#gmail.com",
"country_code": "091",
"phone": 123456,
"profile_pic": "",
"type": 0,
"status": 1,
"reset_token": "$2y$10$mT9MqON6gMre0rKtoK0ON.VApZYBZP0PY55uM017Cz74E69qBILjm",
"verify_token": "$2y$10$HMBteSyYTKZ3XgYviUdNORKOw1Bpan5m0UcqIyx3dZrYUsNajou",
"created_at": "2019-05-07 08:47:29",
"updated_at": "2019-05-17 05:55:00",
"deleted_at": null
}
]
Now I want firebase data should look like this
firebasedemo
.
+ Users
.
. . name:"abc"
I am writing this line in Api call For loop
Rootref= FirebaseDatabase.getInstance().getReference();
Rootref.child("Users").child("name").setValue(name);
But I am getting only one record in firebase not all
Any help will be highly appreciated
Try the following:
DatabaseReference rootref = FirebaseDatabase.getInstance().getReference();
rootref.child("Users").push().setValue(name);
From the docs:
public DatabaseReference push ()
Create a reference to an auto-generated child location. The child key is generated client-side and incorporates an estimate of the server's time for sorting purposes. Locations generated on a single client will be sorted in the order that they are created, and will be sorted approximately in order across all clients.

JSON key some time string and some time list value android retrofit [duplicate]

This question already has answers here:
How to handle Dynamic JSON in Retrofit?
(12 answers)
Closed 5 years ago.
Response 1:
{
"id": "85",
"email": "jack#test.com",
"profession": [
{
"category_id": "1",
"name": "ARTIST"
}
],
"genre": [
{
"category_id": "3",
"name": "ROCK"
}
],
"instruments": "No list has found",
}
Response 2:
{
"id": "85",
"email": "jack#test.com",
"profession": [{
"category_id": "1",
"name": "ARTIST"
}],
"genre": [{
"category_id": "3",
"name": "ROCK"
}],
"instruments": [{
"category_id": "3",
"name": "ROCK"
}],
}
In first response instruments key have a String value, In second response instruments have a array. create the pojo class for second response but some times i got first response also it move OnFailure . how can i handle in Retrofit.
Different types of json objects is not possible to parse in single request/response. So you should use this on following way.
if your instruments object is empty means don't set string like "No list has found", Instead of you should send like "instruments": [],
now instruments is list coming but size is 0(zero), that means there is no data, like "No list has found"
so your response should be like this
{
"id": "85",
"email": "jack#test.com",
"profession": [{
"category_id": "1",
"name": "ARTIST"
}],
"genre": [{
"category_id": "3",
"name": "ROCK"
}],
"instruments": [],
"other": [{
"category_id": "4",
"name": "Test"
}],
}
Validate the list size in local and process your way..
Hope this will help you :)...

Elasticsearch 5.1 Bulk Action

I'm try to make a bulk update
Method: Post
Url: /customer/external/_bulk
Json Body:
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
Id 1 is updated but id 2 didnt update. I dont know why?
Response is here:
{
"took": 138,
"errors": false,
"items": [
{
"index": {
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 15,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": false,
"status": 200
}
}
]
}
As #Val mentioned, you should be having the new line character \n at the end of the last line in your json body:
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }\n
as per mentioned in bulk_api. Hope it helps!

Categories