-1

Below is the returned sample json

[
"id":
[
    {
        "reports":
            [
                {
                    "metadata":
                        {
                            "materialName" : "materialName1"
                            "materialType" : "materialType1"    
                        },
                        "xvalue": null,
                        "yvalue": null,
                },
                {
                    "metadata":
                        {
                            "materialName" : "materialName2"
                            "materialType" : "materialType2"    
                        },
                        "xvalue": null,
                        "yvalue": null,
                },
            ],
            "someValue" : "someValue1"
            "id" : "id1"
    },
    "reports":
            [
                {
                    "metadata":
                        {
                            "materialName" : "materialName3"
                            "materialType" : "materialType3"    
                        },
                        "xvalue": null,
                        "yvalue": null,
                },
                {
                    "metadata":
                        {
                            "materialName" : "materialName4"
                            "materialType" : "materialType4"    
                        },
                        "xvalue": null,
                        "yvalue": null,
                },
            ],
            "someValue" : "someValue1"
            "id" : "id2"
  ]

]

I have below Json expression in Json Path Extractor in JMeter

$.data[0].materials[?(@.id)].reports[*].metadata['materialName','materialType']

The above expression returned as below for every 'id' in a json array

materialName1, materialType1

materialName2, materialType2

materialName3, materialType3

materialName4, materialType4

but I want to extract the value of 'id' along with 'materialName' and 'materialType' as mentioned below

id1, materialName1, materialType1

id1, materialName2, materialType2

id2, materialName3, materialType3

id2, materialName4, materialType4

etc.

please help me on this.

Thanks, Jatin

Jatin
  • 21
  • 4

2 Answers2

0

Add JSR223 sampler and log it directly with:

log.info(variable)

Variables can be resolved in any sampler comment section with ${variable} for testing purposes.

enter image description here

Alex Karamfilov
  • 634
  • 1
  • 6
  • 12
  • I want to fetch id along with materialName and material type using the mentioned Json expression. Currenlty I dont get an idea that how to fetch as I am only getting materialname and materialtype for each id but I want id to be returned as well – Jatin Oct 12 '22 at 09:58
  • You can have more than one extractor if you need to get more values from json. Its hard to answer without seeing json. – Alex Karamfilov Oct 12 '22 at 10:02
  • Added a sample json for your reference – Jatin Oct 12 '22 at 10:48
  • Yes I know that I can have multiple extractors but tell how it will give a combination of all the subid's for every id in the response. Like I have asked in modified comment. Please dont tell me that json I mention is incorrect as it is just for your reference. – Jatin Oct 12 '22 at 12:28
0

JMeter provides __logn() function allowing printing whatever you want to jmeter.log file

Also in case of > 1 matches returned by JSON Extractor JMeter generates the following variables

var_1=foo
var_2=bar
...
var_matchNr=2

so you will be able to print them to jmeter.log file as:

1.upto(vars.get('var_matchNr') as int, { index ->
    log.info(vars.get('var_' + index))
})

if you want to print to STDOUT instead - replace log.info with println

More information on Groovy scripting in JMeter: Apache Groovy: What Is Groovy Used For?

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • I have updated my question to better explain what I wanted. Please check – Jatin Oct 12 '22 at 10:06
  • what you posted is not a valid json hence you won't be able to parse it with JSON Extractor – Dmitri T Oct 12 '22 at 11:35
  • It's easy given you [ask a good question and provide minimal reproducible example](https://stackoverflow.com/help/how-to-ask), I'm not going to waste my time trying to restore the correct valid JSON out of the stuff you provided – Dmitri T Oct 12 '22 at 13:31