Am new to rest assured
I have a json response like this
{
"queryPath": "/api/",
"nId": "f084f5ad24fcfaa9e9faea0",
"statusCode": 707
"statusMessage": "Success",
"results": {
"data": [
{
"id": "10248522500798",
"capabilities": [
"men",
"women"
],
"name": "errt2"
},
{
"id": "418143778",
"capabilities": [
"dog",
"cat"
],
"name": "Livin"
}
]
}
}
String type = "men"
Using rest assured i need to check the type value men with capabilities array values
[
"men",
"women"
]
and
"capabilities": [
"dog",
"cat"
]
If the type value `men` not contains in any of the capabilities i need to raise error..how it is possible?
i used following code .But it is not working.any other way??
body("results.data.capabilities", hasItems(type));
Am getting error
Expected: (a collection containing "men")
Actual: [[ men,women
], [dog, cat]]
The problem is that your jsonPath
results.data.capabilities
points to both "capabilities" arrays since they are nested at the same level of your JSON. If you can reliably expect the first "capabilities" array to contain "men" then you can specify the first "capabilities" in your jsonPath like so:
results.data.capabilities[0]
Related
Dears,
I am working on creating a simple method which will take String argument which will be a path or other kind "pointer" to attribute/s in JSON and this method will remove those attribute/s.
My problem is I can find values of those attribute/s using JsonPath, but I can't find methods in rest assured (or other libraries) which could remove/delete attributes by given path.
JSON is already added earlier so i need to pull him from RequestSpecification or FilterableRequestSpecification object ex.
RequestSpecification rs = *objFromContext*;
FilterableRequestSpecification frs= (FilterableRequestSpecification) rs;
frs.getBody();
I've tried to work with JSONObject class and remove() but it doesn't work on complex JSONs.
given example JSON
{
"created": "string",
"updated": "string",
"items": [
{
"code": "TEST",
"nested": {
"code": "test",
"name": "name",
"other": [
{
"code": "TEST",
"name": "myName",
"quantity": 1
}
]
},
"itemsProperties": [
{
"code": "value1",
"name": "name",
"value": 123
}
]
},
{
"code": "TEST",
"nested": {
"code": "test",
"name": "name",
"other": [
{
"code": "TEST",
"name": "myName",
"quantity": 1
}
]
},
"itemsProperties": [
{
"code": "value2",
"name": "name",
"value": 123
}
]
}
],
"timer": {
"startDate": "2015-01-01",
"endDate": "2021-01-02"
},
"id": "myId"
}
using JsonPath jp = JsonPath.from(httpRequest.getBody().toString());
and then jp.get(items.itemsproperties.code) i can find value1 and value2.
I stuck in this point: How to remove those attributes from sended body?
I know i can convert body into JSONObject and then go field after field conversion between getJSONArray and GetJSONOBject and remove those fields, but i would like to make this metod much more universal.
Is this possible?
If you want to manipulate json in Rest-Assured JsonPath, then the answer is No. You can't do that. JsonPath help you to extract value from json, that's it, no more.
You have to use different libraries to remove key-value pair.
For example: using JsonPath Jayway
DocumentContext parse = JsonPath.parse(body);
parse.delete("$..itemsProperties..code");
System.out.println(parse.jsonString());
This object is from an API call. I want this object in LinkedHashMap<key, Pair<String, String>> form, as I have to use both keys and values. If this is not possible what is the best possible way to do this. Do I need to convert this array into object also?
Object:
"recurring_frequencies": {
"none": [
"None",
"0"
],
"daily": [
"Daily",
"1"
],
"weekly": [
"Weekly",
"7"
],
"bi-weekly": [
"Bi-weekly",
"14"
],
"monthly": [
"Monthly",
"28"
],
"quarterly": [
"Quarterly",
"91"
],
"semi-annually": [
"Semi-annually",
"182"
],
"annually": [
"Annually",
"365"
]
}
}
Have a look at this question.
I assume that the API returns a JSON string, which can be parsed by JSONObject. Afterwards you can extract all information by iterating through all entries.
Please help me with correct json path. I am trying to extract "500" from "value".
Json (part of it) looks like this:
Vehicle {
"code": "BCA",
"name": "COLL",
"description": "Collision",
"limitTerms": [],
"deductibleTerms": [
{
"code": "qsw",
"name": "",
"value": "500",
"valueCode": "",
"valueDescription": "",
}
],
"otherTerms": [],
},
I want to use the name or description than reach to deductibleTerms and extract value from it.
I tried wrtting json path like that (which is for sure wrong)
"$.vehicle[description='Collision' and .deductibleTerms[*].value]"
The .deductibleTerms[*].value should not be inside the filter [] part of the expression since you are not filtering on it.
$.vehicle[description='Collision'].deductibleTerms[*].value
This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 6 years ago.
I need the count the number of specific array (coreid) present in response JSON Since, response I'm getting is pretty huge, I am using following JSON as example:
{
"errors": [],
"data": {
"products": {
"number": 0,
"size": 2,
"total": 2,
"results": [{
"assetId": "29-test-ENG",
"fields": {
"majoracademic": [
"test stream 1"
],
"learningobjective": [
"test objective 1"
],
"hasproductprice": [
"0"
],
"comingsoon": [
"0"
],
"coreid": [
"12"
],
"previewCount": [
"0"
],
"type": [
"page(s)"
]
}
},
{
"assetId": "01-test-MTH",
"fields": {
"majoracademic": [
"test stream 2"
],
"learningobjective": [
"test objective 2"
],
"hasproductprice": [
"0"
],
"comingsoon": [
"0"
],
"coreid": [
"12"
],
"previewCount": [
"0"
],
"type": [
"page(s)"
]
}
}
]
}
}
}
So Basically I need to count array "coreid" occurrence in this JSON, which is actually 2 but my code is returning 3 times, I'm using the following code:
protected String getTokenValueUnderHierarchyString( String Json) throws ClientProtocolException, IOException {
List<String> list = Arrays.asList(Json.split("coreid"));
System.out.println("list.size()::"+list.size());
}
I printed the values of array, turns out it takes complete JSON object as 1st element.
So even If I type "coreid1, array count it will return is 1. and the value would be complete JSON.
I hope my question is making any sense.
Split is not meant to count occurences. It split (hence the name ;) ) a string at a given separator. You have three because you have some text before the first coreid some text between the two and some text after the last.
Also, your solution will break if the string coreid is present anywhere else. A better approach would be to parse the JSON and then analyse it to find the number of key that are coreid.
I have the following json string. Sometimes, there are multiple definition elements and sometimes there is only one. I am new to JSON parsing and I somehow, in a very primitive way, managed to get the value of the first definition element that occurs. However, I am unable to get all values of definitions since there is no resource that I can find online. Do I have to check if second occurrence exists or is there a better way to do it ?
JSON:
{
"metadata": {
"provider": "Oxford University Press"
},
"results": [
{
"id": "edifice",
"language": "en",
"lexicalEntries": [
{
"entries": [
{
"etymologies": [
"late Middle English: via Old French from Latin aedificium, from aedis dwelling + facere make"
],
"grammaticalFeatures": [
{
"text": "Singular",
"type": "Number"
}
],
"senses": [
{
"definitions": [
"a large, imposing building."
],
"id": "m_en_gb0256160.001",
"registers": [
"formal"
]
},
{
"definitions": [
"a complex system of beliefs:"
],
"examples": [
{
"text": "the concepts on which the edifice of capitalism was built"
}
],
"id": "m_en_gb0256160.002",
"registers": [
"formal"
]
}
]
}
],
"language": "en",
"lexicalCategory": "Noun",
"pronunciations": [
{
"audioFile": "http://audio.oxforddictionaries.com/en/mp3/edifice_gb_1.mp3",
"dialects": [
"British English"
],
"phoneticNotation": "IPA",
"phoneticSpelling": "ˈɛdɪfɪs"
}
],
"text": "edifice"
}
],
"type": "headword",
"word": "edifice"
}
]
}
Java snippet to get definition value:
String locations = data.getJSONArray("results").getJSONObject(0).getJSONArray("lexicalEntries").getJSONObject(0).getJSONArray("entries").getJSONObject(0).getJSONArray("senses").getJSONObject(0).get("definitions").toString();
Everything looks good but you should stop at getJSONArray("senses").
It should be
JSONArray senses= .....getJSONArray("senses");
and then you should loop thru elements of that array. Something like this.
ArrayList<String> definitions = new ArrayList<String>();
for(int i =0; i < senses.length(); i++){
definitions.add(senses.getJSONObject(i).optString("definitions","");
}
But before doing that you should notice that your definitions json is not well formed it should be someting like
"definitions":"blablabl blablab"
Since it is not an array;.