java json string to url query string - java

Two days i spend to search efficent method to convert json string to url query string, for example
SRC:
"searchCriteria": {
"filterGroups": [
{
"filters": [
{
"field": "myfiled",
"value": "myvalue",
"conditionType": "eq"
}
]
}
],
"sortOrders": [
{
"field": "string",
"direction": "string"
}
],
"pageSize": 0,
"currentPage": 0
}
to:
http://host/api/?searchCriteria[filterGroups][][filters][][field]=myfield&searchCriteria[filterGroups][][filters][][value]=myvalue&searchCriteria[filterGroups][][filters][][conditionType]=eq&..
Some suggestions ?
A.

Thank U for answer, but ...
I've tryed to pass directly with payload json string and api give me an error, when I put instead query string like in my example, api respond in right way.
In PHP is very simple: json can be converted in array and so with function "http_build_query" it's done ! I'm suprised that not exist similar method .
Alb

Related

How to remove an attribute from RequestSpecification/FilterableRequestSpecification body?

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());

RestAssured, JSON: verify absence of a value in a field

I have the following example URI:
localhost/users
and this JSON coming as response when I send a request to it:
[
{
"id": 1,
"name": "Joe"
,
{
"id": 3,
"name": "Ben"
,
{
"id": 4,
"name": "Jim"
}
}
]
How do I verify with RestAssured, for example that there are no user with id = 2?
Would it be something along the lines of this semi-pseudo:
given().spec(requestSpecification)
.when().get("/users")
.and().body("id==2", ????);
Any advice/guidance will be appreciated, thank you.
try this:
given().spec(requestSpecification)
.when().get("/users")
.and().body("id", is(not(equalTo(2))));
Usage rest-assured documentation section Use the response to verify other parts of the response provides more detail.
is(not(Matcher)) is provided by hamcrest

Is there a way to dynamically set variables values for the post request with jmeter beanshell preprocessor

I need to dynamically build the following post request JSON body with jmeter beanshell preprocessor. I am referring to the following question which has a solution for my problem with looped strings. I would need to do this with json-property(variables) an array of JSON objects with different name and values. Thanks a lot.
{
"processDefinitionId":"optaplannerkey:1:dbc4af8f-7e04-11e9-afa3-1ecac26bb5e0",
"businessKey":"optaplannerkey",
"returnVariables":true,
"variables": [
{
"name": "TaskDescription",
"value": "Fixing the issue with sink"
},
{
"name": "TaskCategory",
"value": "plumbing"
},
{
"name": "Priority",
"value": "Medium"
},
{
"name": "Status",
"value": "New"
},
{
"name": "SkillsRequired",
"value": "Plumbing Skills"
},
{
"name": "DueDate",
"value": "2019-05-24T11:23:08.030+05:30"
}
]
}
Use dummy sampler with the parameterized json request and CSV Data Set Config for the dynamic input. Below, I have paremeterized only two for demo.
Then, Use JSR223 Post processor with the following code:-
vars.put("responseVar",prev.getResponseDataAsString());
This will put response body in "responseVar" variable. Fetch it using ${responseVar}
Hope this helps.

Correct json Path

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

rest assured to match value from a root json array

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]

Categories

Resources