Response selector

I’m having trouble using the selector of the response. I want to extract the id of items that have “Open” as its status. Here is my gjson path

res.json(‘data.#(status==“Open”)#.id’) → keep showing ‘undefined’, what am I missing here?

{"data": [
        {
            "completedAt": null,
            "createdAt": "2020-12-10T10:53:26.945Z",
            "createdBy": "13814035-1dd2-11b2-82c4-35be246034a1",
            "details": [
                {
                    "content": "always",
                    "createdAt": "2020-12-10T10:53:47.360Z",
                    "createdBy": "bc56d608-fff6-45d0-b093-785ff060f617",
                    "id": "ba5a0403-64b6-4b2a-ad4f-083306df48dd",
                    "modifiedAt": "2020-12-10T10:53:47.360Z",
                    "modifiedBy": "",
                    "questionId": "bd40afb7-a279-4226-b524-1f6fbf8a7640",
                    "responseId": "2c592e1b-e2d6-4e17-97f0-f23efd5082de",
                    "score": 5
                },
                {
                    "content": "always",
                    "createdAt": "2020-12-10T10:53:49.240Z",
                    "createdBy": "bc56d608-fff6-45d0-b093-785ff060f617",
                    "id": "19db53ae-3288-4222-bdd2-65af5b9d2f47",
                    "modifiedAt": "2020-12-10T10:53:49.240Z",
                    "modifiedBy": "",
                    "questionId": "7e9c7672-6804-49e1-bbe9-c8aa72c9f7fd",
                    "responseId": "2c592e1b-e2d6-4e17-97f0-f23efd5082de",
                    "score": 5
                },
                {
                    "content": "always",
                    "createdAt": "2020-12-10T10:53:50.666Z",
                    "createdBy": "bc56d608-fff6-45d0-b093-785ff060f617",
                    "id": "0f0c59e7-b10f-4f2d-b3d4-779056dc241e",
                    "modifiedAt": "2020-12-10T10:53:50.666Z",
                    "modifiedBy": "",
                    "questionId": "345ecd25-8757-43f2-acbd-608de5612f55",
                    "responseId": "2c592e1b-e2d6-4e17-97f0-f23efd5082de",
                    "score": 5
                },
                {
                    "content": "always",
                    "createdAt": "2020-12-10T10:53:52.559Z",
                    "createdBy": "bc56d608-fff6-45d0-b093-785ff060f617",
                    "id": "d3e04358-c014-48b9-9763-9856e4805aa2",
                    "modifiedAt": "2020-12-10T10:53:52.559Z",
                    "modifiedBy": "",
                    "questionId": "b37ae883-ee3f-4470-b7e4-25b1d3ac874a",
                    "responseId": "2c592e1b-e2d6-4e17-97f0-f23efd5082de",
                    "score": 5
                }
            ],
            "from": "2020-08-14",
            "id": "2c592e1b-e2d6-4e17-97f0-f23efd5082de",
            "latestQuestionId": "b37ae883-ee3f-4470-b7e4-25b1d3ac874a",
            "maxScore": "79.4",
            "mode": "touch",
            "modifiedAt": "2020-12-10T10:53:56.862Z",
            "modifiedBy": "",
            "no": "2940",
            "outcomeStatus": "Mild",
            "patientId": "bc56d608-fff6-45d0-b093-785ff060f617",
            "questionTime": [
                "10:00",
                "22:00"
            ],
            "score": 20,
            "status": "Submitted",
            "survey": {
                "createdAt": "2020-10-21T07:47:03.542Z",
                "createdBy": null,
                "description": null,
                "domainVersionId": "72de0eb0-db37-4e12-bd6a-73e563e02600",
                "id": "dc146f39-5203-4369-ba5f-e92c8b041ad1",
                "introduction": "The DEMO Depression item banks assess self-reported negative mood (sadness, guilt), views of self (selfcriticism, worthlessness), and social cognition (loneliness, interpersonal alienation), as well as decreased positive affect and engagement (loss of interest, meaning, and purpose)",
                "modifiedAt": "2020-10-21T07:47:03.542Z",
                "modifiedBy": null,
                "name": "DEMO - 29 Depression (v2.1)",
                "publishedAt": null,
                "publishedBy": null,
                "status": "Active",
                "surveySchedule": {
                    "createdAt": "2020-10-21T07:47:04.074Z",
                    "createdBy": null,
                    "from": "2020-08-14",
                    "id": "dc146f39-5203-4369-ba5f-e92c8b041ad1",
                    "modifiedAt": "2020-12-09T10:00:02.175Z",
                    "modifiedBy": null,
                    "questionPerSession": 4,
                    "questionTime": [
                        "10:00",
                        "22:00"
                    ],
                    "to": "2020-12-30"
                },
                "surveyTypeId": "8f5dbfd9-c138-4dee-b65d-98c102d47efe",
                "userId": null
            },
            "surveyId": "dc146f39-5203-4369-ba5f-e92c8b041ad1",
            "tScore": "58.5",
            "to": "2020-12-30"
        },
        {
            "completedAt": null,
            "createdAt": "2020-12-10T10:51:57.458Z",
            "createdBy": "13814035-1dd2-11b2-82c4-35be246034a1",
            "details": [
                {
                    "content": "always",
                    "createdAt": "2020-12-10T10:52:10.648Z",
                    "createdBy": "bc56d608-fff6-45d0-b093-785ff060f617",
                    "id": "d6d65183-ed9a-4c0c-b2d5-129fa9aa9828",
                    "modifiedAt": "2020-12-10T10:52:10.648Z",
                    "modifiedBy": "",
                    "questionId": "bd40afb7-a279-4226-b524-1f6fbf8a7640",
                    "responseId": "885c9e53-4b0b-468b-b6a0-504dc0737c82",
                    "score": 5
                },
                {
                    "content": "always",
                    "createdAt": "2020-12-10T10:52:12.112Z",
                    "createdBy": "bc56d608-fff6-45d0-b093-785ff060f617",
                    "id": "db7d89dd-05d5-4e1f-8bc6-43d5533455f3",
                    "modifiedAt": "2020-12-10T10:52:12.112Z",
                    "modifiedBy": "",
                    "questionId": "7e9c7672-6804-49e1-bbe9-c8aa72c9f7fd",
                    "responseId": "885c9e53-4b0b-468b-b6a0-504dc0737c82",
                    "score": 5
                },
                {
                    "content": "always",
                    "createdAt": "2020-12-10T10:52:15.324Z",
                    "createdBy": "bc56d608-fff6-45d0-b093-785ff060f617",
                    "id": "ed02bb73-593e-4884-891e-388617bb3741",
                    "modifiedAt": "2020-12-10T10:52:15.324Z",
                    "modifiedBy": "",
                    "questionId": "345ecd25-8757-43f2-acbd-608de5612f55",
                    "responseId": "885c9e53-4b0b-468b-b6a0-504dc0737c82",
                    "score": 5
                },
                {
                    "content": "always",
                    "createdAt": "2020-12-10T10:52:17.485Z",
                    "createdBy": "bc56d608-fff6-45d0-b093-785ff060f617",
                    "id": "bb350dbb-e2fc-4773-a8af-e6ee4722bcfd",
                    "modifiedAt": "2020-12-10T10:52:17.485Z",
                    "modifiedBy": "",
                    "questionId": "b37ae883-ee3f-4470-b7e4-25b1d3ac874a",
                    "responseId": "885c9e53-4b0b-468b-b6a0-504dc0737c82",
                    "score": 5
                }
            ],
            "from": "2020-08-14",
            "id": "885c9e53-4b0b-468b-b6a0-504dc0737c82",
            "latestQuestionId": "b37ae883-ee3f-4470-b7e4-25b1d3ac874a",
            "maxScore": "79.4",
            "mode": "touch",
            "modifiedAt": "2020-12-10T10:52:21.155Z",
            "modifiedBy": "",
            "no": "4604",
            "outcomeStatus": "Mild",
            "patientId": "bc56d608-fff6-45d0-b093-785ff060f617",
            "questionTime": [
                "10:00",
                "22:00"
            ],
            "score": 20,
            "status": "Submitted",
            "survey": {
                "createdAt": "2020-10-21T07:47:03.542Z",
                "createdBy": null,
                "description": null,
                "domainVersionId": "72de0eb0-db37-4e12-bd6a-73e563e02600",
                "id": "dc146f39-5203-4369-ba5f-e92c8b041ad1",
                "introduction": "The DEMO Depression item banks assess self-reported negative mood (sadness, guilt), views of self (selfcriticism, worthlessness), and social cognition (loneliness, interpersonal alienation), as well as decreased positive affect and engagement (loss of interest, meaning, and purpose)",
                "modifiedAt": "2020-10-21T07:47:03.542Z",
                "modifiedBy": null,
                "name": "DEMO - 29 Depression (v2.1)",
                "publishedAt": null,
                "publishedBy": null,
                "status": "Active",
                "surveySchedule": {
                    "createdAt": "2020-10-21T07:47:04.074Z",
                    "createdBy": null,
                    "from": "2020-08-14",
                    "id": "dc146f39-5203-4369-ba5f-e92c8b041ad1",
                    "modifiedAt": "2020-12-09T10:00:02.175Z",
                    "modifiedBy": null,
                    "questionPerSession": 4,
                    "questionTime": [
                        "10:00",
                        "22:00"
                    ],
                    "to": "2020-12-30"
                },
                "surveyTypeId": "8f5dbfd9-c138-4dee-b65d-98c102d47efe",
                "userId": null
            },
            "surveyId": "dc146f39-5203-4369-ba5f-e92c8b041ad1",
            "tScore": "58.5",
            "to": "2020-12-30"
        }]}

I can’t easily debug that, but the query looks ok to me. I just don’t see any status=="Open" entries in the data you posted, so maybe that’s it?

Here is the status, I can extract the status using data.#.status, but once I used parentheses for condition, it turned undefined

I just ran this script against a simple echo server:

import http from "k6/http";

export default function() {

    var body = `{"data": [
{
    "completedAt": null,
        "createdAt": "2020-12-10T10:53:26.945Z",
        "createdBy": "13814035-1dd2-11b2-82c4-35be246034a1",
        "details": [
                    {
                                            "content": "always",
                                            "createdAt": "2020-12-10T10:53:47.360Z",
                                            "createdBy": "bc56d608-fff6-45d0-b093-785ff060f617",
                                            "id": "ba5a0403-64b6-4b2a-ad4f-083306df48dd",
                                            "modifiedAt": "2020-12-10T10:53:47.360Z",
                                            "modifiedBy": "",
                                            "questionId": "bd40afb7-a279-4226-b524-1f6fbf8a7640",
                                            "responseId": "2c592e1b-e2d6-4e17-97f0-f23efd5082de",
                                            "score": 5
                                        },
                    {
                                            "content": "always",
                                            "createdAt": "2020-12-10T10:53:49.240Z",
                                            "createdBy": "bc56d608-fff6-45d0-b093-785ff060f617",
                                            "id": "19db53ae-3288-4222-bdd2-65af5b9d2f47",
                                            "modifiedAt": "2020-12-10T10:53:49.240Z",
                                            "modifiedBy": "",
                                            "questionId": "7e9c7672-6804-49e1-bbe9-c8aa72c9f7fd",
                                            "responseId": "2c592e1b-e2d6-4e17-97f0-f23efd5082de",
                                            "score": 5
                                        },
                    {
                                            "content": "always",
                                            "createdAt": "2020-12-10T10:53:50.666Z",
                                            "createdBy": "bc56d608-fff6-45d0-b093-785ff060f617",
                                            "id": "0f0c59e7-b10f-4f2d-b3d4-779056dc241e",
                                            "modifiedAt": "2020-12-10T10:53:50.666Z",
                                            "modifiedBy": "",
                                            "questionId": "345ecd25-8757-43f2-acbd-608de5612f55",
                                            "responseId": "2c592e1b-e2d6-4e17-97f0-f23efd5082de",
                                            "score": 5
                                        },
                    {
                                            "content": "always",
                                            "createdAt": "2020-12-10T10:53:52.559Z",
                                            "createdBy": "bc56d608-fff6-45d0-b093-785ff060f617",
                                            "id": "d3e04358-c014-48b9-9763-9856e4805aa2",
                                            "modifiedAt": "2020-12-10T10:53:52.559Z",
                                            "modifiedBy": "",
                                            "questionId": "b37ae883-ee3f-4470-b7e4-25b1d3ac874a",
                                            "responseId": "2c592e1b-e2d6-4e17-97f0-f23efd5082de",
                                            "score": 5
                                        }
                ],
                "from": "2020-08-14",
                "id": "2c592e1b-e2d6-4e17-97f0-f23efd5082de",
                "latestQuestionId": "b37ae883-ee3f-4470-b7e4-25b1d3ac874a",
                "maxScore": "79.4",
                "mode": "touch",
                "modifiedAt": "2020-12-10T10:53:56.862Z",
                "modifiedBy": "",
                "no": "2940",
                "outcomeStatus": "Mild",
                "patientId": "bc56d608-fff6-45d0-b093-785ff060f617",
                "questionTime": [
                                    "10:00",
                                    "22:00"
                                ],
                "score": 20,
                "status": "Submitted",
                "survey": {
                                    "createdAt": "2020-10-21T07:47:03.542Z",
                                    "createdBy": null,
                                    "description": null,
                                    "domainVersionId": "72de0eb0-db37-4e12-bd6a-73e563e02600",
                                    "id": "dc146f39-5203-4369-ba5f-e92c8b041ad1",
                                    "introduction": "The DEMO Depression item banks assess self-reported negative mood (sadness, guilt), views of self (selfcriticism, worthlessness), and social cognition (loneliness, interpersonal alienation), as well as decreased positive affect and engagement (loss of interest, meaning, and purpose)",
                                    "modifiedAt": "2020-10-21T07:47:03.542Z",
                                    "modifiedBy": null,
                                    "name": "DEMO - 29 Depression (v2.1)",
                                    "publishedAt": null,
                                    "publishedBy": null,
                                    "status": "Active",
                                    "surveySchedule": {
                                                            "createdAt": "2020-10-21T07:47:04.074Z",
                                                            "createdBy": null,
                                                            "from": "2020-08-14",
                                                            "id": "dc146f39-5203-4369-ba5f-e92c8b041ad1",
                                                            "modifiedAt": "2020-12-09T10:00:02.175Z",
                                                            "modifiedBy": null,
                                                            "questionPerSession": 4,
                                                            "questionTime": [
                                                                                        "10:00",
                                                                                        "22:00"
                                                                                    ],
                                                            "to": "2020-12-30"
                                                        },
                                    "surveyTypeId": "8f5dbfd9-c138-4dee-b65d-98c102d47efe",
                                    "userId": null
                                },
                "surveyId": "dc146f39-5203-4369-ba5f-e92c8b041ad1",
                "tScore": "58.5",
                "to": "2020-12-30"
            },
    {
                    "completedAt": null,
                    "createdAt": "2020-12-10T10:51:57.458Z",
                    "createdBy": "13814035-1dd2-11b2-82c4-35be246034a1",
                    "details": [
                                        {
                                                                "content": "always",
                                                                "createdAt": "2020-12-10T10:52:10.648Z",
                                                                "createdBy": "bc56d608-fff6-45d0-b093-785ff060f617",
                                                                "id": "d6d65183-ed9a-4c0c-b2d5-129fa9aa9828",
                                                                "modifiedAt": "2020-12-10T10:52:10.648Z",
                                                                "modifiedBy": "",
                                                                "questionId": "bd40afb7-a279-4226-b524-1f6fbf8a7640",
                                                                "responseId": "885c9e53-4b0b-468b-b6a0-504dc0737c82",
                                                                "score": 5
                                                            },
                                        {
                                                                "content": "always",
                                                                "createdAt": "2020-12-10T10:52:12.112Z",
                                                                "createdBy": "bc56d608-fff6-45d0-b093-785ff060f617",
                                                                "id": "db7d89dd-05d5-4e1f-8bc6-43d5533455f3",
                                                                "modifiedAt": "2020-12-10T10:52:12.112Z",
                                                                "modifiedBy": "",
                                                                "questionId": "7e9c7672-6804-49e1-bbe9-c8aa72c9f7fd",
                                                                "responseId": "885c9e53-4b0b-468b-b6a0-504dc0737c82",
                                                                "score": 5
                                                            },
                                        {
                                                                "content": "always",
                                                                "createdAt": "2020-12-10T10:52:15.324Z",
                                                                "createdBy": "bc56d608-fff6-45d0-b093-785ff060f617",
                                                                "id": "ed02bb73-593e-4884-891e-388617bb3741",
                                                                "modifiedAt": "2020-12-10T10:52:15.324Z",
                                                                "modifiedBy": "",
                                                                "questionId": "345ecd25-8757-43f2-acbd-608de5612f55",
                                                                "responseId": "885c9e53-4b0b-468b-b6a0-504dc0737c82",
                                                                "score": 5
                                                            },
                                        {
                                                                "content": "always",
                                                                "createdAt": "2020-12-10T10:52:17.485Z",
                                                                "createdBy": "bc56d608-fff6-45d0-b093-785ff060f617",
                                                                "id": "bb350dbb-e2fc-4773-a8af-e6ee4722bcfd",
                                                                "modifiedAt": "2020-12-10T10:52:17.485Z",
                                                                "modifiedBy": "",
                                                                "questionId": "b37ae883-ee3f-4470-b7e4-25b1d3ac874a",
                                                                "responseId": "885c9e53-4b0b-468b-b6a0-504dc0737c82",
                                                                "score": 5
                                                            }
                                    ],
                    "from": "2020-08-14",
                    "id": "885c9e53-4b0b-468b-b6a0-504dc0737c82",
                    "latestQuestionId": "b37ae883-ee3f-4470-b7e4-25b1d3ac874a",
                    "maxScore": "79.4",
                    "mode": "touch",
                    "modifiedAt": "2020-12-10T10:52:21.155Z",
                    "modifiedBy": "",
                    "no": "4604",
                    "outcomeStatus": "Mild",
                    "patientId": "bc56d608-fff6-45d0-b093-785ff060f617",
                    "questionTime": [
                                        "10:00",
                                        "22:00"
                                    ],
                    "score": 20,
                    "status": "Submitted",
                    "survey": {
                                        "createdAt": "2020-10-21T07:47:03.542Z",
                                        "createdBy": null,
                                        "description": null,
                                        "domainVersionId": "72de0eb0-db37-4e12-bd6a-73e563e02600",
                                        "id": "dc146f39-5203-4369-ba5f-e92c8b041ad1",
                                        "introduction": "The DEMO Depression item banks assess self-reported negative mood (sadness, guilt), views of self (selfcriticism, worthlessness), and social cognition (loneliness, interpersonal alienation), as well as decreased positive affect and engagement (loss of interest, meaning, and purpose)",
                                        "modifiedAt": "2020-10-21T07:47:03.542Z",
                                        "modifiedBy": null,
                                        "name": "DEMO - 29 Depression (v2.1)",
                                        "publishedAt": null,
                                        "publishedBy": null,
                                        "status": "Active",
                                        "surveySchedule": {
                                                                "createdAt": "2020-10-21T07:47:04.074Z",
                                                                "createdBy": null,
                                                                "from": "2020-08-14",
                                                                "id": "dc146f39-5203-4369-ba5f-e92c8b041ad1",
                                                                "modifiedAt": "2020-12-09T10:00:02.175Z",
                                                                "modifiedBy": null,
                                                                "questionPerSession": 4,
                                                                "questionTime": [
                                                                                            "10:00",
                                                                                            "22:00"
                                                                                        ],
                                                                "to": "2020-12-30"
                                                            },
                                        "surveyTypeId": "8f5dbfd9-c138-4dee-b65d-98c102d47efe",
                                        "userId": null
                                    },
                    "surveyId": "dc146f39-5203-4369-ba5f-e92c8b041ad1",
                    "tScore": "58.5",
                    "to": "2020-12-30"
                }]}`

    var res = http.post("http://127.0.0.1:8080/", body)

    console.log(res.json('data.#(status=="Submitted")#.id'));
}

package main

import (
	"bytes"
	"io"
	"log"
	"net/http"
)

func main() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		buf := new(bytes.Buffer)
		_, err := io.Copy(buf, r.Body)
		if err != nil {
			panic(err)
		}
		_, err = io.Copy(w, buf)

		if err != nil {
			panic(err)
		}
	})

	log.Fatal(http.ListenAndServe(":8080", nil))
}

And I get the:

INFO[0000] 2c592e1b-e2d6-4e17-97f0-f23efd5082de,885c9e53-4b0b-468b-b6a0-504dc0737c82  source=console

So the problems is as @ned said , you just don’t have data that is what you are looking for, if you search for “Submitted” status it works, because there is actually data with such status.

Oh sorry, I forgot to mention that the real response is much longer than the attached one. My following code snippet print out the following result

console.log("Available status");
console.log(res.json('data.#.status'));
console.log("IDs of submitted surveys");
console.log(res.json('data.#(status=="Submitted")#.id'));
INFO[0004] Available status                              source=console
INFO[0004] Open,Open,Open,Open,Open,Submitted,Submitted,Submitted,Submitted,Submitted,Submitted,Submitted,On Hold,Submitted,Submitted,Submitted,Submitted,Submitted,Submitted,Submitted  source=console
INFO[0004] IDs of submitted surveys                      source=console
INFO[0004] undefined                                     source=console

@phuhnguyen you are probably using an old version of k6 - update to v0.29.0 and this should be fixed by the update of the underlying library that was done in it.