简体   繁体   中英

JMeter JSON Extractor using -1 value for foreach controller with inconsistent array

JMeter JSON Extractor using -1 value for foreach controller with inconsistent array

I have this JSON response

[
  {
    "userId": 1,
    "id": 1,
    "title": " How to Shape of Character Design",
    "write": "Jun Bale",
    "date": "20/12/20",
    "time": "10:00AM",
    "body": " Because he takes  nsuscipit accepted result lightly with  nreprehenderit discomfort may be the entire  nnostrum of the things that happens is that they are extremely ",
    "image": "https://source.unsplash.com/rDEOVtE7vOs/1600x900",
    "tag": null
      },
  {
    "userId": 1,
    "id": 2,
    "write": "Henry Cavil",
    "date": "21/12/20",
    "time": "08:00AM",
    "title": " How to Write About Your Life? Start Here .",
    "body": " it is the time of  nseq are not criticize consumer happy that the pain or  nfugiat soothing pleasure forward or no discomfort may rejecting some  nWho, not being due, we may be able to open the man who did not, but there is no ",
    "image": "https://source.unsplash.com/WNoLnJo7tS8/1600x900",
    "tag": null
  },
  {
    "userId": 1,
    "id": 3,
    "write": "Katrina Taylor",
    "date": "24/12/20",
    "time": "06:49PM",
    "title": " How to Survive as a Freelancer in 2020 ",
    "body": " innocent, but the law  nvoluptatis blinded the election or the  nvoluptatis pains or prosecutors who is to pay nmolestiae and is willing to further or to and from the toil of an odious term ",
    "image": "https://source.unsplash.com/vMV6r4VRhJ8/1600x900",
    "tag": {
            "country": "British"
          }
  }
]

I am extracting all the values using JSON extractor. But the issue I am facing with the tags.country is that its not available for all the array items. I am using this JSON Path $.[*].tag.country using match No -1 and then I am aiming to use this in the for each loop. But because country is only available in 1 item i am not sure how can I relate this with the corresponding item. I could write some code but just exploring if there's an easier option available.

I want the total of 3 instances of country regardless whether it has a value or not I am planning to put Default value as " Country-NotFound " and then use this value later in the process.

I don't think you can achieve it in a single shot using JSON Extractor as JSONPath will only return the values where country attribute is not null

I would recommend going for JSR223 PostProcessor and the following code:

def response = new groovy.json.JsonSlurper().parse(prev.getResponseData())
0.upto(response.size() - 1, { index ->
    if (response.get(index).tag == null) {
        vars.put('tag_' + (index + 1), 'Country-NotFound')
    } else {
        vars.put('tag_' + (index + 1), response.get(index).tag.country)
    }
})

Given your JSON response it should produce the following JMeter Variables:

tag_1=Country-NotFound
tag_2=Country-NotFound
tag_3=British   

which are suitable for iterating using ForEach Controller

More information:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM