Details of Jmeter's Json extractor (the most complete in History)

reference material: https://www.bbsmax.com/A/D854lmBw5E/

Online test of Jsonpath: http://jsonpath.com/

Some scenes used in practical work:

  1. Extract a specific value
  2. Extract multiple values
  3. Value by condition
  4. Array value (return list / array of all elements)
  5. Extract multiple values

Jsonpath extractor needs to be installed separately

  • Download plugins-manager.jar Join it join JMETER_HOME/lib/ext directory,
  • Restart JMeter,
  • Click Options > plugins manager top menu,
  • Select the Available Plugins tab,
  • Select Json Plugins and double-click apply changes and restart JMeter.

usage method:

The Json Path extractor should be placed under HTTP Sampler. The parameters that can be set are:

  • Variable name: semicolon independent variable name,
  • JSON Path Expressions: an expression that extracts content from a json response,
  • Match number: - 1 for all, 0 for random, n for nth,
  • Compute concatenation var: create a ${foo_ALL} contains concatenated variables for all extracted values,
  • Default: if the expression does not apply to the json document being processed, use the default value given here.

Next, we use a real case to demonstrate some usage of Json extraction:

Example: there are json as follows

{
    "code": 0,
    "data": {
        "total": 10,
        "pageNo": 1,
        "pageSize": 100,
        "items": [
            {
                "friendHeadLogoUrl": null,
                "msg": "Hello, I admire your talent!",
                "shield": 0,
                "star": 0,
                "friendNickname": "Auto",
                "origin": 202,
                "black": 0,
                "memo": null,
                "updateTime": 1591169560056,
                "type": 2,
                "userId": 1268082752079560705,
                "friendId": 1267735865761759234,
                "createTime": 1591169490879,
                "blackUpdateTime": 0,
                "addUpdateTime": 1591169560056,
                "id": 1268082870707040258,
                "close": 0,
                "status": 2
            },
            {
                "friendHeadLogoUrl": null,
                "msg": "Hello, I admire your talent!",
                "shield": 0,
                "star": 0,
                "friendNickname": "Auto",
                "origin": 202,
                "black": 0,
                "memo": null,
                "updateTime": 1591169560092,
                "type": 2,
                "userId": 1268082752079560705,
                "friendId": 1267735866185383938,
                "createTime": 1591169491390,
                "blackUpdateTime": 0,
                "addUpdateTime": 1591169560092,
                "id": 1268082872854523906,
                "close": 0,
                "status": 2
            },
            {
                "friendHeadLogoUrl": null,
                "msg": "Hello, I admire your talent!",
                "shield": 0,
                "star": 0,
                "friendNickname": "Auto",
                "origin": 202,
                "black": 0,
                "memo": null,
                "updateTime": 1591169560126,
                "type": 2,
                "userId": 1268082752079560705,
                "friendId": 1267735867363983362,
                "createTime": 1591169491862,
                "blackUpdateTime": 0,
                "addUpdateTime": 1591169560126,
                "id": 1268082874830041089,
                "close": 0,
                "status": 2
            },
            {
                "friendHeadLogoUrl": null,
                "msg": "Hello, I admire your talent!",
                "shield": 0,
                "star": 0,
                "friendNickname": "Auto",
                "origin": 202,
                "black": 0,
                "memo": null,
                "updateTime": 1591169560164,
                "type": 2,
                "userId": 1268082752079560705,
                "friendId": 1267735867825356802,
                "createTime": 1591169492296,
                "blackUpdateTime": 0,
                "addUpdateTime": 1591169560164,
                "id": 1268082876654563330,
                "close": 0,
                "status": 2
            },
            {
                "friendHeadLogoUrl": null,
                "msg": "Hello, I admire your talent!",
                "shield": 0,
                "star": 0,
                "friendNickname": "Auto",
                "origin": 202,
                "black": 0,
                "memo": null,
                "updateTime": 1591169560310,
                "type": 2,
                "userId": 1268082752079560705,
                "friendId": 1267735867003273217,
                "createTime": 1591169493876,
                "blackUpdateTime": 0,
                "addUpdateTime": 1591169560310,
                "id": 1268082883277369345,
                "close": 0,
                "status": 2
            },
            {
                "friendHeadLogoUrl": null,
                "msg": "Hello, I admire your talent!",
                "shield": 0,
                "star": 0,
                "friendNickname": "Auto",
                "origin": 202,
                "black": 0,
                "memo": null,
                "updateTime": 1591169560376,
                "type": 2,
                "userId": 1268082752079560705,
                "friendId": 1267735868571942913,
                "createTime": 1591169494629,
                "blackUpdateTime": 0,
                "addUpdateTime": 1591169560376,
                "id": 1268082886439874561,
                "close": 0,
                "status": 2
            }
        ],
        "timestamp": null
    },
    "codeMsg": "success"
}

1. Extract a specific value

Jsonpath result
$.data.total 10
$..total 10
$..items[1].userId 1268082752079560705

2. Extract multiple values

  • For example, to extract all friendids in the items list

$..friendId

$..[*].friendId

$.data.items[*].friendId

The results are as follows:

Result[0]=1267735865761759234
Result[1]=1267735866185383938
Result[2]=1267735867363983362
Result[3]=1267735867825356802
Result[4]=1267735864650268673
Result[5]=1267735865363300353
Result[6]=1267735866688700417
Result[7]=1267735867003273217
Result[8]=1267735868232204289
Result[9]=1267735868571942913

be careful:

  1. When using, you need to read with subscript, such as ${friend_id_1},${friend_id_2}........

  2. Specifically, if you want to get the number of return elements, you can use ${friend_id_matchNr}

  3. You can also use the foreach controller loop to read, which is not described here.

3. Extract values by condition

Sometimes you only need to extract parameters under certain conditions. For example, now you want to extract friendId whose creation time is 1591169490879

$.data.items[?(@.createTime ==1591169490879)].friendId

4. Array extraction

Extract the value of a key of any node, such as friendId
$..friendId

The results are as follows:

friend_all_1=1267735865761759234
friend_all_10=1267735868571942913
friend_all_2=1267735866185383938
friend_all_3=1267735867363983362
friend_all_4=1267735867825356802
friend_all_5=1267735864650268673
friend_all_6=1267735865363300353
friend_all_7=1267735866688700417
friend_all_8=1267735867003273217
friend_all_9=1267735868232204289
friend_all_ALL=1267735865761759234,1267735866185383938,1267735867363983362,1267735867825356802,1267735864650268673,1267735865363300353,1267735866688700417,1267735867003273217,1267735868232204289,1267735868571942913
friend_all_matchNr=10

A kind of ALL subscript is to form a list of ALL elements. For example, in the subsequent interface, sometimes parameters are passed in the form of arrays. This function is very useful. If you need to return this function, you need to check Compute concatenation var in the json extractor

5. Extract multiple values

json expression:

$..['msg','friendId']

The variables returned are as follows:

two_1={"msg":"Hello, I admire your talent!","friendId":1267735865761759234}
two_10={"msg":"Hello, I admire your talent!","friendId":1267735868571942913}
two_2={"msg":"Hello, I admire your talent!","friendId":1267735866185383938}
two_3={"msg":"Hello, I admire your talent!","friendId":1267735867363983362}
two_4={"msg":"Hello, I admire your talent!","friendId":1267735867825356802}
two_5={"msg":"Hello, I admire your talent!","friendId":1267735864650268673}
two_6={"msg":"Hello, I admire your talent!","friendId":1267735865363300353}
two_7={"msg":"Hello, I admire your talent!","friendId":1267735866688700417}
two_8={"msg":"Hello, I admire your talent!","friendId":1267735867003273217}
two_9={"msg":"Hello, I admire your talent!","friendId":1267735868232204289}
two_ALL={"msg":"Hello, I admire your talent!","friendId":1267735865761759234},{"msg":"Hello, I admire your talent!","friendId":1267735866185383938},{"msg":"Hello, I admire your talent!","friendId":1267735867363983362},{"msg":"Hello, I admire your talent!","friendId":1267735867825356802},{"msg":"Hello, I admire your talent!","friendId":1267735864650268673},{"msg":"Hello, I admire your talent!","friendId":1267735865363300353},{"msg":"Hello, I admire your talent!","friendId":1267735866688700417},{"msg":"Hello, I admire your talent!","friendId":1267735867003273217},{"msg":"Hello, I admire your talent!","friendId":1267735868232204289},{"msg":"Hello, I admire your talent!","friendId":1267735868571942913}
two_matchNr=10

Tags: JSON

Posted on Fri, 05 Jun 2020 03:06:38 -0400 by sean paul