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 get a new list from the list of objects?
I need a new list of objects to POST request
this list of objects i get from response:
{
"success": true,
"body": {
"users": [
{
"type": "unknown",
"data": [
{
"id": "8",
"firstName": "Jackson",
"lastName": "Baker",
"group": "false"
},
{
"id": "11",
"firstName": "Charlotte",
"lastName": "Garcia",
"group": "false"
},
{
"id": "7",
"firstName": "Henry",
"lastName": "Thompson",
"group": "false"
},
{
"id": "24",
"firstName": "Elijah",
"lastName": "Miller",
"group": "false"
}
]
}
]
}
}
I need to form a new object from response:
{
"success": true,
"body": {
"users": [
{
"type": "unknown",
"data": [
{
"id": "8",
"group": "false"
},
{
"id": "11",
"group": "false"
},
{
"id": "7",
"group": "false"
},
{
"id": "24",
"group": "false"
}
]
}
]
}
}
This new list of object I need to post a request.
I have a JSON Schema, but i don't know how i can make this structure.
My problem is that I can't get the values from fields. I'm using the path "body.users.data.find {it.id} .id" but it finds the id array but I need a users list with values from two fields.
As the users node and data node are all list type, you need use two loop:
import groovy.json.JsonSlurper
// text is your json response text
def result = new JsonSlurper().parseText(text)
result.body.users.each{it.data.each{it.remove("firstName");it.remove("lastName")}}
// result is the what you wanted
println(result)
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);
}
I'm trying to create POJOs for the following JSON from the API at https://developers.google.com/qpx-express/v1/trips/search
When I copy & paste this into http://www.jsonschema2pojo.org/ I get an error at line 5 "requestId": string, saying "There's a problem: Unrecognized token 'string': was expecting ('true', 'false' or 'null') (line 5, column 24)"
If I change string to an actual example string it works fine. Can this json to pojo converter not handle JSON in this format? It just seems a bit tedious to have to change all the data types to samples instead.
{
"kind": "qpxExpress#tripsSearch",
"trips": {
"kind": "qpxexpress#tripOptions",
"requestId": string,
"data": {
"kind": "qpxexpress#data",
"airport": [
{
"kind": "qpxexpress#airportData",
"code": string,
"city": string,
"name": string
}
],
"city": [
{
"kind": "qpxexpress#cityData",
"code": string,
"country": string,
"name": string
}
],
"aircraft": [
{
"kind": "qpxexpress#aircraftData",
"code": string,
"name": string
}
],
"tax": [
{
"kind": "qpxexpress#taxData",
"id": string,
"name": string
}
],
"carrier": [
{
"kind": "qpxexpress#carrierData",
"code": string,
"name": string
}
]
},
"tripOption": [
{
"kind": "qpxexpress#tripOption",
"saleTotal": string,
"id": string,
"slice": [
{
"kind": "qpxexpress#sliceInfo",
"duration": integer,
"segment": [
{
"kind": "qpxexpress#segmentInfo",
"duration": integer,
"flight": {
"carrier": string,
"number": string
},
"id": string,
"cabin": string,
"bookingCode": string,
"bookingCodeCount": integer,
"marriedSegmentGroup": string,
"subjectToGovernmentApproval": boolean,
"leg": [
{
"kind": "qpxexpress#legInfo",
"id": string,
"aircraft": string,
"arrivalTime": string,
"departureTime": string,
"origin": string,
"destination": string,
"originTerminal": string,
"destinationTerminal": string,
"duration": integer,
"operatingDisclosure": string,
"onTimePerformance": integer,
"mileage": integer,
"meal": string,
"secure": boolean,
"connectionDuration": integer,
"changePlane": boolean
}
],
"connectionDuration": integer
}
]
}
],
"pricing": [
{
"kind": "qpxexpress#pricingInfo",
"fare": [
{
"kind": "qpxexpress#fareInfo",
"id": string,
"carrier": string,
"origin": string,
"destination": string,
"basisCode": string,
"private": boolean
}
],
"segmentPricing": [
{
"kind": "qpxexpress#segmentPricing",
"fareId": string,
"segmentId": string,
"freeBaggageOption": [
{
"kind": "qpxexpress#freeBaggageAllowance",
"bagDescriptor": [
{
"kind": "qpxexpress#bagDescriptor",
"commercialName": string,
"count": integer,
"description": [
string
],
"subcode": string
}
],
"kilos": integer,
"kilosPerPiece": integer,
"pieces": integer,
"pounds": integer
}
]
}
],
"baseFareTotal": string,
"saleFareTotal": string,
"saleTaxTotal": string,
"saleTotal": string,
"passengers": {
"kind": "qpxexpress#passengerCounts",
"adultCount": integer,
"childCount": integer,
"infantInLapCount": integer,
"infantInSeatCount": integer,
"seniorCount": integer
},
"tax": [
{
"kind": "qpxexpress#taxInfo",
"id": string,
"chargeType": string,
"code": string,
"country": string,
"salePrice": string
}
],
"fareCalculation": string,
"latestTicketingTime": string,
"ptc": string,
"refundable": boolean
}
]
}
]
}
}
Valid json requires string values to be in quotes. So quoting the key but not the value like-> "id" : string, does not make much sense. Read carefully "In the request body, supply data with the following structure" You are not supplying any data
Well, the example JSON code on the page uses data types in quotes, so "string" instead of string. You could do just that:
(warning! the following example is nonsense if you want other types than strings!)
{
"kind": "qpxExpress#tripsSearch",
"trips": {
"kind": "qpxexpress#tripOptions",
"requestId": "string",
"data": {
"kind": "qpxexpress#data",
"airport": [
{
"kind": "qpxexpress#airportData",
"code": "string",
"city": "string",
"name": "string"
}
],
"city": [
{
"kind": "qpxexpress#cityData",
"code": "string",
"country": "string",
"name": "string"
}
],
"aircraft": [
{
"kind": "qpxexpress#aircraftData",
"code": "string",
"name": "string"
}
],
"tax": [
{
"kind": "qpxexpress#taxData",
"id": "string",
"name": "string"
}
],
"carrier": [
{
"kind": "qpxexpress#carrierData",
"code": "string",
"name": "string"
}
]
},
"tripOption": [
{
"kind": "qpxexpress#tripOption",
"saleTotal": "string",
"id": "string",
"slice": [
{
"kind": "qpxexpress#sliceInfo",
"duration": "integer",
"segment": [
{
"kind": "qpxexpress#segmentInfo",
"duration": "integer",
"flight": {
"carrier": "string",
"number": "string"
},
"id": "string",
"cabin": "string",
"bookingCode": "string",
"bookingCodeCount": "integer",
"marriedSegmentGroup": "string",
"subjectToGovernmentApproval": "boolean",
"leg": [
{
"kind": "qpxexpress#legInfo",
"id": "string",
"aircraft": "string",
"arrivalTime": "string",
"departureTime": "string",
"origin": "string",
"destination": "string",
"originTerminal": "string",
"destinationTerminal": "string",
"duration": "integer",
"operatingDisclosure": "string",
"onTimePerformance": "integer",
"mileage": "integer",
"meal": "string",
"secure": "boolean",
"connectionDuration": "integer",
"changePlane": "boolean"
}
],
"connectionDuration": "integer"
}
]
}
],
"pricing": [
{
"kind": "qpxexpress#pricingInfo",
"fare": [
{
"kind": "qpxexpress#fareInfo",
"id": "string",
"carrier": "string",
"origin": "string",
"destination": "string",
"basisCode": "string",
"private": "boolean"
}
],
"segmentPricing": [
{
"kind": "qpxexpress#segmentPricing",
"fareId": "string",
"segmentId": "string",
"freeBaggageOption": [
{
"kind": "qpxexpress#freeBaggageAllowance",
"bagDescriptor": [
{
"kind": "qpxexpress#bagDescriptor",
"commercialName": "string",
"count": "integer",
"description": [
"string"
],
"subcode": "string"
}
],
"kilos": "integer",
"kilosPerPiece": "integer",
"pieces": "integer",
"pounds": "integer"
}
]
}
],
"baseFareTotal": "string",
"saleFareTotal": "string",
"saleTaxTotal": "string",
"saleTotal": "string",
"passengers": {
"kind": "qpxexpress#passengerCounts",
"adultCount": "integer",
"childCount": "integer",
"infantInLapCount": "integer",
"infantInSeatCount": "integer",
"seniorCount": "integer"
},
"tax": [
{
"kind": "qpxexpress#taxInfo",
"id": "string",
"chargeType": "string",
"code": "string",
"country": "string",
"salePrice": "string"
}
],
"fareCalculation": "string",
"latestTicketingTime": "string",
"ptc": "string",
"refundable": "boolean"
}
]
}
]
}
}
I got json[] from UI. I have to convert it into Json object how can I convert it with gson. An object which is received is like that:
[{"name":"name","value":"value"},{"name":"first","value":"100"},{"name":"hor","value":"95"},{"name":"conf","value":"95"}],[{"name":"vaRType","value":"INCRE"},{"name":"per","value":"100"},{"name":"hor","value":"95"},{"name":"conf","value":"95"}]
I used JsonObject obj = new JsoParser().parse(jsonStrnig).getAsJsonObject();
but it throws an exception.
Because it is not valid json object.
Your JSON should be the following as currently it's invalid...
[
[
{
"name": "name",
"value": "value"
},
{
"name": "first",
"value": "100"
},
{
"name": "hor",
"value": "95"
},
{
"name": "conf",
"value": "95"
}
],
[
{
"name": "vaRType",
"value": "INCRE"
},
{
"name": "per",
"value": "100"
},
{
"name": "hor",
"value": "95"
},
{
"name": "conf",
"value": "95"
}
]
]