4

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"
  ]
]
2
  • Could you share your JSON data instead of response? Commented Apr 20, 2017 at 14:21
  • @fiskra, this my response data only Commented Apr 21, 2017 at 17:29

1 Answer 1

8
  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

      JMeter JSON Extractor

    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

    JMeter Debug Sampler

  2. 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

    JMeter Response Assertion Against JSON Variable

    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.

Sign up to request clarification or add additional context in comments.

4 Comments

cool stuffs, thanks @dmitri T but i'm using 2.13 version of J-meter. so i don't have JSON Exactor .
You can use JSON Path Extractor available via JMeter Plugins project. You can install JSON Path Extractor (as well as JSON Path Assertion) using JMeter Plugins Manager
Every way I look at their specs, $[*].length() should work, but it just doesn’t. Seems like a bug in JSONPath or jmeter to me. Good work-around.
I implement greater operator with JSR223 Assertion (groovy): return vars.get("week_matchNr").toInteger() > 0;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.