Json full response in Json object or String - java

hi I am using okhttp for getting my data from the server and I am getting the response by response.body().string() that is printing full data. But when i am converting the response to jsonobject or String(Storing in the variable) and printing data it is not displaying full data of the response.i am unable to display
"id": 1,
"user_id": 1,
"first_name": "hhhh",
"last_name": "Thakur",
while other is working fine
My json data in form
{
"customers": [
{
"id": 1,
"user_id": 1,
"first_name": "hhhh",
"last_name": "Thakur",
"website": "",
"status": 0,
"created": "2017-10-10T07:29:45+00:00",
"customer_info": [
{
"id": 1,
"customer_id": 1,
"created": "2017-10-10T07:29:45+00:00"
}
],
"customer_address": [
{
"id": 1,
"customer_id": 1,
"name": "hhhh Thakur",
"address": "V.PO Chadwal Distt Kathua, Teh Hiranagar Jammu, Jammu and Kashmir in",
"city": "Shimla",
"state": "Himachal Pradesh",
"zip": "171004",
"country": "India",
"fax": "06723",
"type": 1,
"as_ship": 1,
"created": "2017-10-10T07:29:45+00:00"
},
{
"id": 2,
"customer_id": 1,
"name": "Neha Sharma",
"address": "V.PO Chadwal Distt Kathua, Teh Hiranagar Jammu, Jammu and Kashmir in",
"city": "India",
"state": "Jammu and Kashmir",
"zip": "180012",
"country": "India",
"fax": "664984",
"type": 2,
"as_ship": 1,
"created": "2017-10-10T07:29:45+00:00"
}
]
},
{
"id": 2,
"user_id": 1,
"first_name": "arun",
"last_name": "kumar",
"website": "www.isitwa.com",
"status": 0,
"created": "2017-10-11T10:14:38+00:00",
"customer_info": [
{
"id": 2,
"customer_id": 2,
"created": "2017-10-11T10:14:38+00:00"
}
],
"customer_address": [
{
"id": 3,
"customer_id": 2,
"name": "",
"address": "",
"city": "",
"state": "",
"zip": "",
"country": "",
"fax": "",
"type": 1,
"as_ship": 1,
"created": "2017-10-11T10:14:38+00:00"
},
{
"id": 4,
"customer_id": 2,
"name": "",
"address": "",
"city": "",
"state": "",
"zip": "",
"country": "",
"fax": "",
"type": 2,
"as_ship": 1,
"created": "2017-10-11T10:14:38+00:00"
}
]
}
]}
}

That's because your JSON is invalid... Paste your JSON codes here, https://codebeautify.org/jsonviewer , it will allow you to manipulate the formating, and take a better look into it...
I'm not an expert in Java but listing a nested array ( no matter, JSON or plain array ) can be done in two ways:
foreach ( $array as $item) {
foreach ( $item as $key => $data) {
echo $data;
}
}
for ( $i=0; $i < $array.lenght(); $i++ ) {
for ( $j=0; $j < $array.lenght-1; $j++) {
print_r $array[$i][$j];
}
}
Using foreach method is better if you don't know how long, or nested your array is...

Your json data is not well formatted ,try to print below one:
{
"a": [
{
"id": 1,
"user_id": 1,
"first_name": "John",
"last_name": "",
"b": [
{
"id": 1,
"customer_id": 1,
"created": "2017-10-10T07:29:45+00:00"
}
],
"c": [
{
"id": 1,
"customer_id": 1,
"created": "2017-10-10T07:29:45+00:00"
}
]
}],
"b":[{
"id": 1,
"user_id": 1,
"first_name": "John",
"last_name": "",
"b": [
{
"id": 1,
"customer_id": 1,
"created": "2017-10-10T07:29:45+00:00"
}
],
"c": [
{
"id": 1,
"customer_id": 1,
"created": "2017-10-10T07:29:45+00:00"
}
]
}
]}

Related

JSON Object Navigation to nested value

I am new to JSON and trying to manipulate JSON for some validation
My JSON looks like this . I need to pick the JSON object based on the refcode and then get count of different object in that, and navigate deeper inside to get the key value pair. Can someone guide me how I can navigate.
{
"components": [
{
"id": 12,
"text": "ABC",
"refCode": "CO_ABC",
"patternCode": "0",
"components": [
{
"id": 1234,
"text": "types",
"refCode": "CO_TYPES",
"questions": [
{
"questionId": 122324,
"questionText": "Is this you",
"questionSequence": 1,
"questionRefCode": "QN_STAY",
"hasPreselectedAnswer": false,
"responsesMetadata": {
"cardinality": "single",
"patternCode": "5",
"dataType": "STRING",
"numberMin": null,
"numberMax": null
},
"choices": [
{
"choiceId": 5456,
"choiceRefCode": "YES",
"choiceText": "Yes",
"sequence": 1
},
{
"choiceId": 8798,
"choiceRefCode": "NO",
"choiceText": "No",
"sequence": 2
}
],
"editable": true,
"accessible": true
}
]
},
{
"id": 13,
"text": "State of stay",
"refCode": "CO_STATE",
"questions": [
{
"questionId": 1,
"questionText": "Which state do you stay",
"questionSequence": 2,
"questionRefCode": "QN_STATE",
"hasPreselectedAnswer": false,
"responsesMetadata": {
"cardinality": "multiple",
"patternCode": "1",
"dataType": "STRING",
"numberMin": null,
"numberMax": null
},
"choices": [
{
"choiceId": 1,
"choiceRefCode": "CH_AZ",
"choiceText": "Arizona",
"sequence": 1
},
{
"choiceId": 2,
"choiceRefCode": "CH_PA",
"choiceText": "Pennsylvania",
"sequence": 2
}
],
"accessible": true
}
]
}
]
}
]
}

How to programmatically create a filter query in Elasticsearch with filtring in a nested object

I am struggling creating a filter query for Elastisearch in Java Spring. I have this Elasticsearch mapping:
"mappings": {
"properties": {
"id": {"type": "keyword"},
"name": {"type": "text"},
"city": {"type": "keyword"},
"cars": {
"type": "nested",
"properties": {
"id": {"type": "long"},
"brand": {"type": "text"},
"category": {"type": "text"}
}
}
}
Data in Elasticsearch looks like this:
[
{
"id": 1,
"name": "Car seller 1",
"city": "Detroit",
"cars": [
{"id": 1, "brand": "bmw", "category": "suv"},
{"id": 2, "brand": "bmw", "category": "sedan"},
{"id": 3, "brand": "bmw", "category": "hatchback"}
]
},
{
"id": 2,
"name": "Car seller 2",
"city": "Detroit",
"cars": [
{"id": 1, "brand": "vw", "category": "suv"},
{"id": 2, "brand": "vw", "category": "sedan"},
{"id": 3, "brand": "vw", "category": "suv"}
]
},
{
"id": 3,
"name": "Car seller 3",
"city": "Las Vegas",
"cars": [
{"id": 1, "brand": "ford", "category": "suv"},
{"id": 2, "brand": "ford", "category": "sedan"},
{"id": 3, "brand": "ford", "category": "hatchback"}
]
}
]
Now, let's say that from my FE will came an request to search documents by some filters:
1. city: ["Detroit"]
2. cars.category: ["suv"]
What I want to achieve is a filter query that will return documents of sellers, but only with cars that match the filter (if there is such a filter).
For example, for mentioned filters query should return this:
[
{
"id": 1,
"name": "Car seller 1",
"city": "Detroit",
"cars": [
{"id": 1, "brand": "bmw", "category": "suv"}
]
},
{
"id": 2,
"name": "Car seller 2",
"city": "Detroit",
"cars": [
{"id": 1, "brand": "vw", "category": "suv"},
{"id": 3, "brand": "vw", "category": "suv"}
]
}
]
So far, I was able to create query with java builders provided by Elastic, which can filter documents properly, but it returning all sellerĀ“s cars.
public Flux<Seller> filterSearch(Map<String, List<String>> filters) {
List<String> listOfFields = new ArrayList<>(filters.keySet());
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
listOfFields.forEach(field -> {
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
if (field.contains("cars.")) {
filters.get(field).forEach(value -> boolQueryBuilder.should(QueryBuilders.termQuery(field, value)));
boolQueryBuilder.minimumShouldMatch(1);
NestedQueryBuilder nestedQueryBuilder = new NestedQueryBuilder("cars", boolQueryBuilder, NestedQueryBuilder.parseScoreMode("none"));
nestedQueryBuilder.innerHit(new InnerHitBuilder());
queryBuilder.must().add(nestedQueryBuilder);
} else {
filters.get(field).forEach(value -> boolQueryBuilder.should(QueryBuilders.matchPhraseQuery(field, value)));
queryBuilder.must().add(boolQueryBuilder);
}
});
return executeSearchQuery(queryBuilder);
}
Thanks for your help.
I think you should try this:
if (field.contains("cars.")) {
...
} else {
filters.get(field).forEach(value -> boolQueryBuilder.should(QueryBuilders.matchPhraseQuery(field, value)).minimumShouldMatch(1));
queryBuilder.must().add(boolQueryBuilder);
}

How to calculate the sum of a Respond (Json) using response.path?

Seem to be struggling to calculate the number of squad members who have the role type of 'PLAYER' by performing my restassured logic against my Json response.
Currently I have a simple setup which hits an endpoint and then executes my query, I can see that the endpoint and response is valid however it seems there is a problem when attempting to filter via my query in order to calculate the total sum of squad members who have a role of 'PLAYER'.
My Rest-assured code:
#Test
public void locatePlayerCalculateSum() {
Response response = given()
.spec(footballCompetitions_requestSpecification)
.when().get(EndPoint.TEAMS + EndPoint.SQUAD);
int sum = response.path("squad.collect { it.role == \"PLAYER\" }.sum()");
System.out.println(sum);
}
Exception Message: java.lang.IllegalArgumentException: No signature of method: java.lang.Boolean.plus() is applicable for argument types: (java.lang.Boolean) values: [true]
Possible solutions: is(java.lang.Object), or(java.lang.Boolean), implies(java.lang.Boolean), and(java.lang.Boolean), use([Ljava.lang.Object;), split(groovy.lang.Closure)
Example JSON response:
"id": 66,
"area": {
"id": 2072,
"name": "England"
},
"activeCompetitions": [
{
"id": 2021,
"area": {
"id": 2072,
"name": "England"
},
"name": "Premier League",
"code": "PL",
"plan": "TIER_ONE",
"lastUpdated": "2019-01-03T23:39:45Z"
},
{
"id": 2001,
"area": {
"id": 2077,
"name": "Europe"
},
"name": "UEFA Champions League",
"code": "CL",
"plan": "TIER_ONE",
"lastUpdated": "2018-12-13T18:55:02Z"
}
],
"name": "Manchester United FC",
"shortName": "Man United",
"tla": "MNU",
"crestUrl": "http://upload.wikimedia.org/wikipedia/de/d/da/Manchester_United_FC.svg",
"address": "Sir Matt Busby Way Manchester M16 0RA",
"phone": "+44 (0161) 8688000",
"website": "http://www.manutd.com",
"email": "enquiries#manutd.co.uk",
"founded": 1878,
"clubColors": "Red / White",
"venue": "Old Trafford",
"squad": [
{
"id": 3188,
"name": "David De Gea",
"position": "Goalkeeper",
"dateOfBirth": "1990-11-07T00:00:00Z",
"countryOfBirth": "Spain",
"nationality": "Spain",
"shirtNumber": 1,
"role": "PLAYER"
},
{
"id": 3202,
"name": "Sergio Romero",
"position": "Goalkeeper",
"dateOfBirth": "1987-02-22T00:00:00Z",
"countryOfBirth": "Argentina",
"nationality": "Argentina",
"shirtNumber": null,
"role": "PLAYER"
},
{
"id": 7942,
"name": "Lee Grant",
"position": "Goalkeeper",
"dateOfBirth": "1983-01-27T00:00:00Z",
"countryOfBirth": "England",
"nationality": "England",
"shirtNumber": 13,
"role": "PLAYER"
},
{
"id": 3206,
"name": "Marcos Rojo",
"position": "Defender",
"dateOfBirth": "1990-03-20T00:00:00Z",
"countryOfBirth": "Argentina",
"nationality": "Argentina",
"shirtNumber": 16,
"role": "PLAYER"
},```
This should work:
#Test
public void locatePlayerCalculateSum() {
Response response = given()
.spec(footballCompetitions_requestSpecification)
.when().get(EndPoint.TEAMS + EndPoint.SQUAD);
int sum = response.path("squad.count { it.role == 'PLAYER' }");
System.out.println(sum);
}

How to write a REST API method for which consume json format for list of objects

I have written a method as following.
#POST
#Path("/add")
#Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
#Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8")
public boolean createMeasurement(List<MeasurementBean> list ,#HeaderParam(AUTHORIZATION) String authString){
Gson gson = new Gson();
LoginSession loginSession=null;
try{
loginSession = LoginUtility.validateKey(authString);
ResultRO<List<HashMap<String, Object>>> resultRO = loginSession
.execute(new Callable<ResultRO<List<HashMap<String, Object>>>>() {
#Override
public ResultRO<List<HashMap<String, Object>>> call() throws Exception {
String key[] = idKey.split(":");
String type = key[0];
long testId = Long.parseLong("2");
return doGetCreateMeasurement(list ,2);
}
});
return gson.toJson(true);
}catch(Exception ex){
ex.printStackTrace();
}
return null;
}
I am not able to consume the list of objects from json format.
and my json is in following format
{
"beans": [{
"id": 1133,
"testConditionGroupId": 0,
"testId": 0,
"type": 0,
"completeFlag": 0,
"invalidFlag": 0,
"retentionFlag": 0,
"delBeforeFlag": 0,
"dontReplicateFlag": 0,
"releaseFlag": 0,
"timeBase": 0.0,
"name": "MeaResult_TEST_XYZABCD",
"version": "V.1.0.1",
"description": "M_Test",
"mimeType": "MIME",
"stardDate": "",
"endDate": "",
"attributeList": [{
"id": 0,
"name": "Test_Attribute1",
"value": "A",
"unit": "ms"
}, {
"id": 0,
"name": "Test_Attribute2",
"value": "B",
"unit": "ms"
}],
"subMatrixList": [{
"id": 0,
"name": "Test_SubMatrixBean1_New",
"version": "V.1.0.0",
"mimeType": "MimeTyep",
"NumberOfvalues": 3,
"noOfRows": 1,
"localColumnList": [{
"id": 0,
"name": "TestName_1_New",
"version": "V.1.0.0",
"mimeType": "MimeTyep",
"sequence_Representation": "explicit",
"independent": 1,
"global_Flag": 15,
"raw_Datatype": 1,
"dataValues": ["1", "2", "3"],
"flags": [15, 15, 15],
"generation_Parameters": [1.0, 2.0, 3.0],
"externalComponents": [],
"attributeList": [],
"meaQuantity": {
"id": 0,
"name": "TestName_1_New",
"unit": "ms",
"quantityName": "MeaQuantity_Quantity",
"description": "MeaQuantity_Test",
"localName": "TestName_1_New",
"size": 10,
"dataType": 1,
"min": 1.0,
"max": 10.0,
"attributeList": [{
"id": 0,
"name": "Test_Attribute1",
"value": "A",
"unit": "ms"
}, {
"id": 0,
"name": "Test_Attribute2",
"value": "B",
"unit": "ms"
}]
}
}],
"attributeList": [{
"id": 0,
"name": "Test_Attribute1",
"value": "A",
"unit": "ms"
}, {
"id": 0,
"name": "Test_Attribute2",
"value": "B",
"unit": "ms"
}]
}],
"meaQuantityList": [{
"id": 0,
"name": "TestName_1_New",
"unit": "ms",
"quantityName": "MeaQuantity_Quantity",
"description": "MeaQuantity_Test",
"localName": "TestName_1_New",
"size": 10,
"dataType": 1,
"min": 1.0,
"max": 10.0,
"attributeList": [{
"id": 0,
"name": "Test_Attribute1",
"value": "A",
"unit": "ms"
}, {
"id": 0,
"name": "Test_Attribute2",
"value": "B",
"unit": "ms"
}]
}]
}]
}
but when I am trying to POST request using postman it gives following error
WARNING: No message body reader has been found for request class MeasurementBeans, ContentType : application/json.
and also method is not executing.
You are accepting a List<MeasurementBean> in the createMeasurement() method and the JSON you have mentioned is an object.
{
"beans": []
//..
}
Just make it as an array by removing the starting {, "beans:" and ending }, i.e you just need to pass the array.

How to access JSON element which is an array

I have the GSON library in my project, I need the ext_id. I am able to access "images" using the JSON.get("images"), however I am unable to get into the images array. Can someone please help me with this.
{
"images": [{
"attributes": {},
"ext_id": "467152316",
"flag_count": 0,
"height": 2064,
"id": "da2d2307b6b72afcfbd6eab8287fad49fce442d6",
"platform": "Web",
"state_name": "public",
"type_name": "ypid",
"user_id": 7297580,
"user_type": "obile",
"width": 1161,
"verified": 1,
"blob_media_type": "image",
"created_at": "2016-08-19 19:29:13 +0000",
"deleted": 0,
"end_date": "2216-07-02 19:29:13 +0000",
"primary": 0,
"recurring": 0,
"start_date": "2016-08-19 19:29:13 +0000",
"state": 0,
"tags": [],
"type_id": 1,
"updated_at": "2016-08-19 19:29:13 +0000",
"user": "Fanta C.",
"business": {
"name": "Porto's Bakery",
"ypid": "467152316",
"listing_id": "467152316",
"primary_collection": "food",
"heading_code": "8004219",
"mip_url": "/glendale-ca/mip/portos-bakery-467152316",
"phone_number": "(818) 956-5996",
"address": "315 N Brand Blvd",
"heading_text": "Dessert Restaurants",
"city": "Glendale",
"state": "CA",
"zip": "91203",
"is_paid": false,
"tier": 999,
"source": "MDM",
"rating_attributes": [{
"name": "Atmosphere",
"id": 1031
}, {
"name": "Food",
"id": 1023
}, {
"name": "Service",
"id": 1038
}]
}
}],
"count": 1
}
You can try this one.
public void parse(String jsonString) {
JsonElement jelement = new JsonParser().parse(jsonString);
JsonObject jobject = jelement.getAsJsonObject();
JsonArray jarray = jobject.getAsJsonArray("images");
for(int i=0;i<jarray.size();i++){
JsonObject jsonObject = jarray.get(i).getAsJsonObject();
String result = jsonObject.get("ext_id").toString();
System.out.println(result);
result = jsonObject.get("height").toString();
System.out.println(result);
}
}
You can also look here
JsonArray jsonArray = (JsonArray)jObject3.get("images");
JsonObject jObject4 =(JsonObject) jsonArray.get(0);
System.out.println(jObject4.get("ext_id"));
System.out.println(jObject4.get("full_image_path"));

Categories

Resources