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
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());
I am trying to create Body-parameter(JSON) for my POST Api , which is a JSON request . All I have is the JSON Schema . I am trying to come up with a list of different JSON test data covering Positive and negative flows for it .
Is there any option to generate / create the JSON data programmatic using Java ? . I have attached a small Json schema (just for understanding purpose) but my actual schema is more complicated with lot of Array's and Nested Json's .
My Json Schema :
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "http://example.com/example.json",
"type": "object",
"title": "The Root Schema",
"description": "The root schema comprises the entire JSON document.",
"required": [
"FirstName",
"LastName",
"Age",
"Interest"
],
"properties": {
"FirstName": {
"$id": "#/properties/FirstName",
"type": "string",
"title": "The Firstname Schema",
"description": "An explanation about the purpose of this instance.",
"default": "",
"examples": [
"Vijay"
]
},
"LastName": {
"$id": "#/properties/LastName",
"type": "string",
"title": "The Lastname Schema",
"description": "An explanation about the purpose of this instance.",
"default": "",
"examples": [
"Karthik"
]
},
"Age": {
"$id": "#/properties/Age",
"type": "integer",
"title": "The Age Schema",
"description": "An explanation about the purpose of this instance.",
"default": 0,
"examples": [
30
]
},
"Interest": {
"$id": "#/properties/Interest",
"type": "array",
"title": "The Interest Schema",
"description": "An explanation about the purpose of this instance.",
"default": [],
"items": {
"$id": "#/properties/Interest/items",
"type": "string",
"title": "The Items Schema",
"description": "An explanation about the purpose of this instance.",
"default": "",
"examples": [
"Food",
"movie",
"Learning",
"VideoGames"
]
}
}
}
}enter code here
My TestData looks like :
{
"FirstName":"Vivi",
"LastName":"Karrri",
"Age":30,
"Interest":["Food","movie","Learning","VideoGames"]
}
Any suggestions how can we achive this ?
Note : I am using Springboot and I have complete POJO for the request object
You can generate fake java-objects and then map them to JSON.
POJOs
If you already have the POJOs matching the schema, then we can skip this step.
If no, to generate a POJO from the schema, for example, can be used this library:
jsonschema2pojo.
Fake objects
Generating of objects with fake data can be done with a special library, some of them are listed here:
easy-random
podam
java-faker
Generating JSON
It's prettry simple and can be done with Jackson:
ObjectMapper objectMapper = new ObjectMapper();
ObjectWriter prettyPrinter = objectMapper.writerWithDefaultPrettyPrinter();
String json = prettyPrinter.writeValueAsString(yourFakeObject);
If you have json schema then you can directly generate a sample JSON message from it.
https://github.com/jignesh-dhua/json-generator
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.
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
at the moment I'm parsing the string with eval()-method and it works fine, but want to parse it with the native JSON methods of a browser. Is there good way to parse the string with jquery.parseJSON() or with any other jQuery method to an array of javascript objects? The string looks like
[
{
"title": "01 Books",
"groupId": "01",
"groupName": "Books",
"key": "1",
"isFolder": true,
"expand": false,
"isLazy": true
},
{
"title": "02 Music",
"groupId": "02",
"groupName": "Music",
"key": "2",
"isFolder": true,
"expand": false,
"isLazy": true
}
]
build with net.sf.json.JSONArray of Java.
jquery.parseJSON() can just parse strings like {"Key":"Value","Key":"Value",....} I guess?
thx for your help!
You have a typo. It is jQuery, not jquery. Capitalization is important.
jQuery.parseJSON()
or
$.parseJSON()
This is supposed to parse it into a json object.