how to extract JSON path and find array length using java? for my below response data. I need to validate array length should be equal to '7' in Jmeter assertion.
[
[
"Week",
"Event Count"
],
[
"3/13/17",
"1"
],
[
"3/20/17",
"1"
],
[
"3/27/17",
"1"
],
[
"4/3/17",
"1"
],
[
"4/10/17",
"1"
],
[
"4/17/17",
"1"
]
]
Add JSON Extractor as a child of the request which produces the above JSON response and configure it as follows:
Variable names: anything meaningful, i.e. week
JSON Path Expressions: $[*]
Match No: -1
This will produce the following JMeter Variables (you can validate them using Debug Sampler):
week_1=["Week","Event Count"]
week_2=["3\/13\/17","1"]
week_3=["3\/20\/17","1"]
week_4=["3\/27\/17","1"]
week_5=["4\/3\/17","1"]
week_6=["4\/10\/17","1"]
week_7=["4\/17\/17","1"]
week_matchNr=7
You are particularly interested in the latter one
Add Response Assertion as a child of the same request and configure it as follows:
Apply to: JMeter Variable -> week_matchNr
Pattern Matching Rules: Equals
Patterns to Test: 7
This way your sampler will pass if number of matches will be equal to 7 and otherwise it will fail. See How to Use JMeter Assertions in Three Easy Steps article to learn more about using assertions in JMeter tests.
Related
I have a csv structure like this
and I also have one json response
[
{
"ID" : "1",
"Name" : "abc",
"Mobile" : "123456"
},
{
"ID" : "2",
"Name" : "cde",
"Mobile" : "123345"
}
]
I need the output like this
If your intention is to convert directly the JSON then that baeldung solution that you were given is good.
Otherwise, the way i see it and based on the info you're giving, you will need to have a representation of that JSON in a java object that will either represent some kind of request coming from somewhere or data you're getting from your database in order to be written on a csv.
Check out these, might be useful:
https://www.codejava.net/frameworks/spring-boot/csv-export-example
https://zetcode.com/springboot/csv/
Good morning,
I am struggling to use GSON to parse some JSON output from a particular web service. Here is some sample output:
[
{
"count": 1,
"headings": [
"name",
"hosts",
"Model",
],
"kind": "Cluster",
"offset": 0,
"results": [
[
"cluster1",
[
"host1",
"host2"
],
[
"Virtual Machine",
"Virtual Machine"
]
]
]
}
]
The "results" portion is the part I am having trouble processing. Basically since the results have mixed types, Lists and strings, I can't write an object that represents it. I've been reading that this may require a deserializer. I am slightly out of my depth on this and would appreciate any insight into how to solve this.
My classes that I am currently using looks like this:
public class ModelDefinition
{
public Integer count ;
public ArrayList<String> headings ;
public String kind ;
public Integer next_offset ;
public Integer offset ;
public String results_id ;
public String next ;
}
public class LongModelDefinition extends ModelDefinition
{
public ArrayList<String[][]> results ;
}
I understand why it isn't working, but I'm not sure how to fix it.
You will not be able to use GSON or another JSON bindings for this ... without resorting to custom serializers / deserializers. The problem is that this:
[
[
"cluster1",
[
"host1",
"host2"
],
[
"Virtual Machine",
"Virtual Machine"
]
]
]
cannot be represented by a statically typed Java data structure. At the 2nd level, you would need a list or array type whose elements are either strings or sublists / subarrays. Java doesn't support this. You could use Object[] or List<Object> and type casting, but bindings are not designed to coe with that kind of thing.
I see three alternatives:
1) You could alter the schema for this data; e.g.
[
[
[
"cluster1"
],
[
"host1",
"host2"
],
[
"Virtual Machine",
"Virtual Machine"
]
]
]
2) You could use a JSON parser that produces JSONObject / JSONArray objects, and deal with the non-uniformity yourself. (How you deal with it would be up to you. It will depend on what the "results" section actually means, and what you need to do with it.)
3) You could implement a GSON-based binding using custom (i.e. hand written) serializers / deserializers.
Response Body:
{
"Items":[{
"ID": 12,
"Name": "n1",
"Inactive": false
},
{
"ID": 16,
"Name": "n2",
"Inactive": false
}, ...etc
]
}
Using JsonPath lib, I was able to get list of all values of specific element (say, "Inactive") with below expression.
'$.. Inactive' ---> [false, false,..etc]
But, I am not sure how I can apply matcher using JsonPathAssert for asserting the above list so that it should only contain 'false'.
I am using Java, JsonPath. Can someone pls help me in this?
After some research, I was able to assert the list - which should only contain 'false' as mentioned below.
org.hamcrest.MatcherAssert.assertThat(actualJson.toString(), JsonPathMatchers.hasJsonPath("$..Inactive", Matchers.everyItem(Matchers.equalTo(false))));
I have to compare two json response as successful response in JMETER.
For example either I will get a response with values,
[
{
"id": 423082,
"createdBy": 10000,
"createdOn": "03/11/2016 12:04 PM"
},
{
"id": 423083,
"createdBy": 10001,
"createdOn": "06/11/2016 12:04 PM"
}
]
OR I will get an empty json []
Both are successful results for me.
How can I compare these results in Response Assertion or JSON Path Extractor as successful results.
Create one regular expression extractor with regex \[(.*?)\]and give some variable name like checkpoint now apply a response assertion and pass ${checkpoint_g0}, so that if your response does not have any value than also it'll check for [] empty json otherwise it'll compare whole response
I've an INPUT that is of the form of a jquery variable.
./Simple Root/
./Root 1/Child 1/
./Root 1/Child 2/
./Thumbs.db
./timepass
I would like to convert it into a neat JSON structure like (Update : Here is a JSON structure that I would like to convert http://www.jstree.com/docs/json/)
'data' : [
'Simple root node',
{
'text' : 'Root node 2',
'state' : {
'opened' : true,
'selected' : true
},
'children' : [
{ 'text' : 'Child 1' },
'Child 2'
]
}
]
NOTE: I havn't mentioned all the cases where the root itself has standalone children,nested children and so on. Basically, the slashes '\' would determine all that.
I wasn't able to think of any solution apart from the regex matcher
var patt1 = /^[^\/]*\/[^\/]*\/?/i;
Can someone please guide me