Search code examples
jsonjmeterjson-path-expressionjson-extract

How to extract multiple correlating variables from a JSon


I have to extract multiple correlating variables from a response (which is json) in JMeter. Part of the response is listed below:

[
  {
    "data": {
      "id": "efaa6876-7a8d-4723-9d85-1ed99e822f06",
      "type": "courses",
      "attributes": {
        "created-at": "2019-02-07T16:38:50.735Z",
        "contents-count": 267,
        "units": [
          {
            "id": "31b5fcb1-24ee-441e-a0ee-ca859fc9a89d",
            "position": null,
            "progress": 0,
            "completed": false,
            "show_name": false,
            "node_id": "1",
            "children": [
              {
                "id": "b8ed75a3-0390-4273-82c3-03ee6eba729c",
                "position": null,
                "image": null,
                "progress": 0,
                "completed": false,
                "show_name": true,
                "node_id": "2",
                "children": [],
                "contents": [
                  {
                    "id": "fa1bdc2f-4330-425c-9c10-3734d07125aa",
                    "link": {
                      "url": "#",
                      "target": "_blank",
                      "class": "learning-object-link",
                      "data": {
                        "id": "fa1bdc2f-4330-425c-9c10-3734d07125aa",
                        "user-role": "teacher",
                        "open-method-tablet": "newtab",
                        "open-method-desktop": "modal",
                        "content-open-method": null,
                        "modal-size-method": "fully_responsive",
                        "fixed-width": null,
                        "fixed-height": null,
                        "aspect-ratio": null
                      }
                    },
                    "is_work": false,
                    "is_fun": false,
                    "completed": false,
                    "total_activities": 2,
                    "completed_activities": 0,
                    "progress": 0,
                    "updated_at": false,
                    "attempts": 0,
                    "duration": null
                  },
                  {
                    "id": "ceceabfd-5151-4656-af5d-3392c5a4c04c",
                      "link": {
                      "url": "#",
                      "target": "_blank",
                      "class": "learning-object-link",
                      "data": {
                        "id": "ceceabfd-5151-4656-af5d-3392c5a4c04c",
                        "user-role": "teacher",
                        "open-method-tablet": "newtab",
                        "open-method-desktop": "modal",
                        "content-open-method": null,
                        "modal-size-method": "fully_responsive",
                        "fixed-width": null,
                        "fixed-height": null,
                        "aspect-ratio": null
                      }
                    },
                    "is_work": false,
                    "is_fun": false,
                    "completed": false,
                    "total_activities": 2,
                    "completed_activities": 0,
                    "progress": 0,
                    "updated_at": false,
                    "attempts": 0,
                    "duration": null
                  }
                ]
              },
              {
                "id": "60639cbd-f872-492d-b8e9-db83f8789fcf",
                "position": null,
                "image": null,
                "progress": 0,
                "completed": false,
                "show_name": true,
                "node_id": "3",
                "children": [],
                "contents": [
                  {
                    "id": "1825f834-7099-4bb4-b7a2-fc634faffc86",
                      "link": {
                      "url": "#",
                      "target": "_blank",
                      "class": "learning-object-link",
                      "data": {
                        "id": "1825f834-7099-4bb4-b7a2-fc634faffc86",
                        "user-role": "teacher",
                        "open-method-tablet": "newtab",
                        "open-method-desktop": "modal",
                        "content-open-method": null,
                        "modal-size-method": "fully_responsive",
                        "fixed-width": null,
                        "fixed-height": null,
                        "aspect-ratio": null
                      }
                    },

To proceed with the next request, I have to extract unit id (e.g. 31b5fcb1-24ee-441e-a0ee-ca859fc9a89d), children id (e.g. b8ed75a3-0390-4273-82c3-03ee6eba729c) and contents id (e.g. fa1bdc2f-4330-425c-9c10-3734d07125aa). There are several units, each unit has several children and each children has several contents. Each content id matches just one children id and each children id matches just one unit id. Ids have to be selected on a random basis.

I've tried to extract all ids from the response and use them randomly, but it doesn't work this way.


Solution

  • To extract only Unit Ids, you can use following JSON Path Expressions:

    $..data.attributes.units[?(@.id)].id
    

    Random Value for Unit Id also can be extracted using JMeter JSON Extractor:

    enter image description here