Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to extract json path and find array length?

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"
  ]
]
like image 751
lotus auto Avatar asked Oct 21 '25 18:10

lotus auto


1 Answers

  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.

like image 91
Dmitri T Avatar answered Oct 23 '25 08:10

Dmitri T



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!