lotus auto
lotus auto

Reputation: 51

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"
  ]
]

Upvotes: 4

Views: 5376

Answers (1)

Dmitri T
Dmitri T

Reputation: 168072

  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.

Upvotes: 8

Related Questions