0

I have one API, who gives a dynamic response in terms of number arrays. I want to execute next API based on the count that I got in the first API.

API 1 Response:
{ 
  "Test 1": [
{Array 1},
{Array 2},
{Array 3}   ]
}

API 2 Request:
{Array 1},
{Array 2},
{Array 3} 

I tried If controller but my API exectuing one time only though i have 3 array.

1

Assuming you have the following JSON:

{
  "Test 1": [
    {
      "foo": 1
    },
    {
      "bar": 2
    },
    {
      "baz": 3
    }
  ]
}

1. Add JSR223 PostProcessor as a child of the request which produces the above JSON and put the following code into "Script" area:

def json = new groovy.json.JsonSlurper().parse(prev.getResponseData())
def array = json.get('Test 1')
array.eachWithIndex { item, index, jmeterindex = index + 1 ->    
    vars.put('array_' + jmeterindex,new groovy.json.JsonOutput().toJson(item))
}
  1. Add ForEach Controller to your Test Plan (somewhere after the main request) and configure it as follows:

    • Input variable prefix: array
    • Output variable name: current_array
  2. Add HTTP Request sampler as a child of the ForEach Controller. You should be able to refer the array value as ${current_array} where required

Demo:

JMeter Iterate JSON Array With Groovy

References:

Example test plan:

<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="3.2" jmeter="3.2 r1790748">
    <hashTree>
        <TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="Test Plan" enabled="true">
            <stringProp name="TestPlan.comments"></stringProp>
            <boolProp name="TestPlan.functional_mode">false</boolProp>
            <boolProp name="TestPlan.serialize_threadgroups">false</boolProp>
            <elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
                <collectionProp name="Arguments.arguments"/>
            </elementProp>
            <stringProp name="TestPlan.user_define_classpath"></stringProp>
        </TestPlan>
        <hashTree>
            <ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true">
                <stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
                <elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller" enabled="true">
                    <boolProp name="LoopController.continue_forever">false</boolProp>
                    <stringProp name="LoopController.loops">1</stringProp>
                </elementProp>
                <stringProp name="ThreadGroup.num_threads">1</stringProp>
                <stringProp name="ThreadGroup.ramp_time">1</stringProp>
                <longProp name="ThreadGroup.start_time">1503209281000</longProp>
                <longProp name="ThreadGroup.end_time">1503209281000</longProp>
                <boolProp name="ThreadGroup.scheduler">false</boolProp>
                <stringProp name="ThreadGroup.duration"></stringProp>
                <stringProp name="ThreadGroup.delay"></stringProp>
            </ThreadGroup>
            <hashTree>
                <JSR223Sampler guiclass="TestBeanGUI" testclass="JSR223Sampler" testname="Produce JSON" enabled="true">
                    <stringProp name="scriptLanguage">groovy</stringProp>
                    <stringProp name="parameters"></stringProp>
                    <stringProp name="filename"></stringProp>
                    <stringProp name="cacheKey"></stringProp>
                    <stringProp name="script">return &quot;{\n&quot; +
                        &quot;  \&quot;Test 1\&quot;: [\n&quot; +
                        &quot;    {\n&quot; +
                        &quot;      \&quot;foo\&quot;: 1\n&quot; +
                        &quot;    },\n&quot; +
                        &quot;    {\n&quot; +
                        &quot;      \&quot;bar\&quot;: 2\n&quot; +
                        &quot;    },\n&quot; +
                        &quot;    {\n&quot; +
                        &quot;      \&quot;baz\&quot;: 3\n&quot; +
                        &quot;    }\n&quot; +
                        &quot;  ]\n&quot; +
                        &quot;}&quot;</stringProp>
                </JSR223Sampler>
                <hashTree>
                    <JSR223PostProcessor guiclass="TestBeanGUI" testclass="JSR223PostProcessor" testname="JSR223 PostProcessor" enabled="true">
                        <stringProp name="scriptLanguage">groovy</stringProp>
                        <stringProp name="parameters"></stringProp>
                        <stringProp name="filename"></stringProp>
                        <stringProp name="cacheKey"></stringProp>
                        <stringProp name="script">def json = new groovy.json.JsonSlurper().parse(prev.getResponseData())
                            def array = json.get(&apos;Test 1&apos;)
                            array.eachWithIndex { item, index, jmeterindex = index + 1 -&gt;
                            vars.put(&apos;array_&apos; + jmeterindex,new groovy.json.JsonOutput().toJson(item))
                            }</stringProp>
                    </JSR223PostProcessor>
                    <hashTree/>
                </hashTree>
                <ForeachController guiclass="ForeachControlPanel" testclass="ForeachController" testname="ForEach Controller" enabled="true">
                    <stringProp name="ForeachController.inputVal">array</stringProp>
                    <stringProp name="ForeachController.returnVal">current_array</stringProp>
                    <boolProp name="ForeachController.useSeparator">true</boolProp>
                </ForeachController>
                <hashTree>
                    <JSR223Sampler guiclass="TestBeanGUI" testclass="JSR223Sampler" testname="${current_array}" enabled="true">
                        <stringProp name="scriptLanguage">groovy</stringProp>
                        <stringProp name="parameters"></stringProp>
                        <stringProp name="filename"></stringProp>
                        <stringProp name="cacheKey"></stringProp>
                        <stringProp name="script">log.info(vars.get(&apos;current_array&apos;))</stringProp>
                    </JSR223Sampler>
                    <hashTree/>
                </hashTree>
                <ResultCollector guiclass="ViewResultsFullVisualizer" testclass="ResultCollector" testname="View Results Tree" enabled="true">
                    <boolProp name="ResultCollector.error_logging">false</boolProp>
                    <objProp>
                        <name>saveConfig</name>
                        <value class="SampleSaveConfiguration">
                            <time>true</time>
                            <latency>true</latency>
                            <timestamp>true</timestamp>
                            <success>true</success>
                            <label>true</label>
                            <code>true</code>
                            <message>true</message>
                            <threadName>true</threadName>
                            <dataType>true</dataType>
                            <encoding>false</encoding>
                            <assertions>true</assertions>
                            <subresults>true</subresults>
                            <responseData>false</responseData>
                            <samplerData>false</samplerData>
                            <xml>false</xml>
                            <fieldNames>true</fieldNames>
                            <responseHeaders>false</responseHeaders>
                            <requestHeaders>false</requestHeaders>
                            <responseDataOnError>false</responseDataOnError>
                            <saveAssertionResultsFailureMessage>true</saveAssertionResultsFailureMessage>
                            <assertionsResultsToSave>0</assertionsResultsToSave>
                            <bytes>true</bytes>
                            <sentBytes>true</sentBytes>
                            <threadCounts>true</threadCounts>
                            <idleTime>true</idleTime>
                            <connectTime>true</connectTime>
                        </value>
                    </objProp>
                    <stringProp name="filename"></stringProp>
                </ResultCollector>
                <hashTree/>
            </hashTree>
        </hashTree>
        <WorkBench guiclass="WorkBenchGui" testclass="WorkBench" testname="WorkBench" enabled="true">
            <boolProp name="WorkBench.save">true</boolProp>
        </WorkBench>
        <hashTree/>
    </hashTree>
</jmeterTestPlan>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.