I,m testing API with rest assured , and trying to get Id's from my created product as a String with the following method below
String productId = response.jsonPath().getString("productPrices.productId");
String channelId = response.jsonPath().getString("productPrices.channelId");
but it's returning me an array
when I print out those Ids, the following output is being got;
System.out.println("productId is ===> " +productId);
System.out.println("channelId is ===> " +channelId);
Output is
productId is ===>[jpjeneOM]
channelId is ===>[5bpJKaVE]
My issue is , i need to get those Ids as a String only !!!
pls help me to resolve this issue
thanks in advance
And I have the following payload
{
"id": "jpjeneOM",
"name": "NewProductForTest",
"productTypeId": null,
"active": true,
"shortDescription": "API",
"fullDescription": "API Automation",
"policy": null,
"mostPopular": null,
"longitude": 34.774667,
"latitude": 32.085814,
"calendarColor": "blue",
"address": "Dizengoff Street, Tel Aviv-Yafo",
"cityId": "nlYxZ6az",
"createdAt": "2021-08-06T07:49:14.920615",
"productImages": [
],
"productDocuments": [
],
"productTypes": null,
"productWorkingDates": [
{
"id": "baQkKROA",
"productId": "jpjeneOM",
"fromDate": "2021-05-27",
"toDate": "2022-12-31",
"name": "StrickHours",
"strictHours": true,
"timeSlots": [
{
"id": "1lkvdZa6",
"productWorkingDateId": "baQkKROA",
"dayOfWeek": "Monday",
"startTime": "09:00:00",
"endTime": "20:00:00",
"duration": "01:00:00",
"quantity": 67,
"usedQuantity": 0,
"active": true,
"deletedAt": null
}
],
"deletedAt": null,
"maxUsedTicketsQuantity": 0,
"errorCode": 0
}
],
"productRegions": [
{
"id": "4lrWZgOd",
"productId": "jpjeneOM",
"regionId": "5bpJKaVE",
"regionName": null
}
],
"productPrices": [
{
"id": "1lon0Aa7",
"productId": "jpjeneOM",
"channelId": "5bpJKaVE",
"fromDate": "2021-08-02T00:00:00",
"toDate": "2022-12-31T00:00:00",
"title": "API",
"remarks": "Test",
"officialPrice": 34,
"currencyId": "5bpJKaVE",
"quantityPerDay": 123,
"autoConfirm": true,
"pricePerPerson": false,
"productPriceTypes": [
{
"id": "xl4WJ7aj",
"productPriceId": "1lon0Aa7",
"productCustomerTypeId": "ZOw36ZOx",
"amount": 12,
"currencyId": "5bpJKaVE",
"currencySymbol": null,
"isOfficial": false,
"productCustomerType": null
}
],
"deletedAt": null
}
]
The productPrices in example is an array, and you didn't mention which one to choose, the jsonpath syntax is like productPrices.*.channelId, so you get arrays. You can choose the first one like productPrices.0.channelId or else.
Related
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
}
]
}
]
}
]
}
I need to extract a json in required format from input json. i'm using jayway json path library. How to achieve it ?
Input Json:
{
"ccid": [
{
"id": 13,
"src": {
"sname": "XA-SXXD",
"lname": "John",
"identifier": 2,
"StatusCode": "C"
}
},
{
"id": 14,
"src": {
"sname": "XB-SXXD",
"lname": "Cena",
"identifier": 3,
"StatusCode": "C",
}
}
]
}
Required Format:
[ {
"id": "13",
"sources": {
"sname": "XA-SXXD",
"lname": "John",
"identifier": 2
}
},
{
"id": "14",
"sources": {
"sname": "XB-SXXD",
"lname": "Cena",
"identifier": 3
}
}]
Query that i use:
$.ccid[*].src[?(#.identifier!=null)].['identifier','sname']
Output that i get:
[
{
"identifier" : 2,
"sname" : "XA-SXXD"
},
{
"identifier" : 3,
"sname" : "XB-SXXD"
}
]
Kindly help me to modify my query to get the required format. The string "sources" in the required format can be hardcoded.
I think I managed to solve this :)
$.ccid[*].[?(#.src.identifier!=null)].['id', 'src']
Give it a try.
Input tested on:
{
"ccid": [
{
"id": 13,
"src": {
"sname": "XA-SXXD",
"lname": "John",
"StatusCode": "C"
}
},
{
"id": 14,
"src": {
"sname": "XB-SXXD",
"lname": "Cena",
"identifier": null,
"StatusCode": "C",
}
}
]
}
Output received:
[
{
"id" : 13,
"src" : {
"sname" : "XA-SXXD",
"lname" : "John",
"StatusCode" : "C"
}
}
]
Only problem is saw is if identifier tag is not available it's essentially treated as not-null. Hence we are getting 13 as an output. But if value is explicitly null then it's fine. So the needs to be enhanced a bit more.
Hope this helps.
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);
}
'{"id":1,"last":"Doe","first":"John","location":{"city":"Oakland","state":"CA","postalCode":"94607"},"active":true}',
'{"id":2,"last":"Doe","first":"Jane","location":{"city":"San
Francisco","state":"WA","postalCode":"94105"},"active":false}',
'{"id":3,"last":"Doe","first":"Jane","location":{"city":"San
Francisco","state":"CA","postalCode":"94105"},"active":true}'
I am looking for getting json contain which includes '{"id":1}'. OR
I am looking for getting json contain which includes '{"location":{"state":"CA"},"active":true}' Likewise...
So for '{"id":1}' it should return
{{"id":1,"last":"Doe","first":"John","location":{"city":"Oakland","state":"CA","postalCode":"94607"},"active":true}
and for '{"location":{"state":"CA"},"active":true}' it should return
{"id":1,"last":"Doe","first":"John","location":{"city":"Oakland","state":"CA","postalCode":"94607"},"active":true}
{"id":3,"last":"Doe","first":"Jane","location":{"city":"San
Francisco","state":"CA","postalCode":"94105"},"active":true}
Try this one: (.*"state":"CA".*"active":true.*)
Demo: https://regex101.com/r/hP6gS1/238
It works.
You can change tag name and values as per your requirements. It extracts your required JSON content.
Python version
my_json = """[
{
"id": 1,
"last": "Doe",
"first": "John",
"location": {
"city": "Oakland",
"state": "CA",
"postalCode": "94607"
},
"active": true
}, {
"id": 2,
"last": "Doe",
"first": "Jane",
"location": {
"city": "San Francisco",
"state": "WA",
"postalCode": "94105"
},
"active": false
}, {
"id": 3,
"last": "Doe",
"first": "Jane",
"location": {
"city": "San Francisco",
"state": "CA",
"postalCode": "94105"
},
"active": true
}
]"""
import json
my_list = json.loads(my_json)
result_list = list(filter(
lambda d:
d["id"] == 1 or \
(d["location"]["state"] == "CA" and d["active"]),
my_list
))
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"
}
]
}
]}