带有输入变量的JMeter JSON路径提取器

带有输入变量的JMeter JSON路径提取器,jmeter,jsonpath,extractor,Jmeter,Jsonpath,Extractor,可以在JSON路径提取器表达式中使用输入变量吗 我尝试了expression$.[${someInputVariable}].name,但不起作用 数据格式: [ {name=a}, {name=b}, {name=c} ] 例如,如果${inputVariable}设置为1,我希望表达式返回“b” 到目前为止,我找到的所有示例都有硬编码的搜索值,例如$[1]。name或$…[?(@.name=='Smith')]首先,如果JSON是错误的,它应该是: [{"name":"a"}, {

可以在JSON路径提取器表达式中使用输入变量吗

我尝试了expression
$.[${someInputVariable}].name
,但不起作用

数据格式:

[ 
{name=a}, 
{name=b}, 
{name=c}
]
例如,如果
${inputVariable}
设置为1,我希望表达式返回“b”


到目前为止,我找到的所有示例都有硬编码的搜索值,例如
$[1]。name
$…[?(@.name=='Smith')]
首先,如果JSON是错误的,它应该是:

 [{"name":"a"}, {"name":"b"},{"name":"c"}]
要提取所需内容,只需使用以下函数:

 ${__eval($.[${someInputVariable}].name)}

首先,您认为JSON是错误的,它应该是:

 [{"name":"a"}, {"name":"b"},{"name":"c"}]
要提取所需内容,只需使用以下函数:

 ${__eval($.[${someInputVariable}].name)}

我看了一下JSONPostProcessor源代码:


不能将变量放入json路径表达式中,因为这些变量不会被计算。顺便说一下,如果你需要帮助,你可以阅读这篇文章。它为json路径提取器提供了更多的示例和替代方案。

我已经查看了JSONPostProcessor源代码:

不能将变量放入json路径表达式中,因为这些变量不会被计算。顺便说一下,如果你需要帮助,你可以阅读这篇文章。它为json路径提取器提供了更多的示例和替代方案。

答案完全错误:json路径表达式中的变量经过计算,至少可以从JMeter 3.1中使用(我没有检查早期版本)

甚至不需要添加${{uuu eval()}。您的示例不起作用,因为您使用了错误的表达式。正确的应该是
$[${someInputVariable}].name
。$和[]之间没有点。JSON也是错误的

下面是一个示例jmx,其中包含根据
${someInputVariable}
的值填充
${NAME-VARIABLE}
的数据。要检查其工作方式,只需将其保存为jmx,请在JMeter中运行,并在“查看结果树”元素中的“调试后处理器”叶的“响应数据”选项卡上检查变量值

<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="3.1" jmeter="3.1 r1770033">
  <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.tearDown_on_shutdown">true</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>
      <Arguments guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
        <collectionProp name="Arguments.arguments">
          <elementProp name="someInputVariable" elementType="Argument">
            <stringProp name="Argument.name">someInputVariable</stringProp>
            <stringProp name="Argument.value">2</stringProp>
            <stringProp name="Argument.metadata">=</stringProp>
          </elementProp>
          <elementProp name="JSONToBeParsed" elementType="Argument">
            <stringProp name="Argument.name">JSONToBeParsed</stringProp>
            <stringProp name="Argument.value">[{&quot;name&quot;:&quot;a&quot;},{&quot;name&quot;:&quot;b&quot;},{&quot;name&quot;:&quot;c&quot;}]</stringProp>
            <stringProp name="Argument.metadata">=</stringProp>
          </elementProp>
        </collectionProp>
      </Arguments>
      <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>
        <boolProp name="ThreadGroup.scheduler">false</boolProp>
        <stringProp name="ThreadGroup.duration"></stringProp>
        <stringProp name="ThreadGroup.delay"></stringProp>
        <longProp name="ThreadGroup.start_time">1535106337000</longProp>
        <longProp name="ThreadGroup.end_time">1535106337000</longProp>
      </ThreadGroup>
      <hashTree>
        <DebugSampler guiclass="TestBeanGUI" testclass="DebugSampler" testname="Debug Sampler" enabled="true">
          <boolProp name="displayJMeterProperties">false</boolProp>
          <boolProp name="displayJMeterVariables">true</boolProp>
          <boolProp name="displaySystemProperties">false</boolProp>
        </DebugSampler>
        <hashTree>
          <JSONPostProcessor guiclass="JSONPostProcessorGui" testclass="JSONPostProcessor" testname="JSON Extractor with constant" enabled="true">
            <stringProp name="JSONPostProcessor.referenceNames">NAME-CONSTANT</stringProp>
            <stringProp name="JSONPostProcessor.jsonPathExprs">$[1].name</stringProp>
            <stringProp name="JSONPostProcessor.match_numbers"></stringProp>
            <stringProp name="Sample.scope">variable</stringProp>
            <stringProp name="Scope.variable">JSONToBeParsed</stringProp>
          </JSONPostProcessor>
          <hashTree/>
          <JSONPostProcessor guiclass="JSONPostProcessorGui" testclass="JSONPostProcessor" testname="JSON Extractor with variable" enabled="true">
            <stringProp name="JSONPostProcessor.referenceNames">NAME-VARIABLE</stringProp>
            <stringProp name="JSONPostProcessor.jsonPathExprs">$[${someInputVariable}].name</stringProp>
            <stringProp name="JSONPostProcessor.match_numbers"></stringProp>
            <stringProp name="Sample.scope">variable</stringProp>
            <stringProp name="Scope.variable">JSONToBeParsed</stringProp>
          </JSONPostProcessor>
          <hashTree/>
          <DebugPostProcessor guiclass="TestBeanGUI" testclass="DebugPostProcessor" testname="Debug PostProcessor" enabled="true">
            <boolProp name="displayJMeterProperties">false</boolProp>
            <boolProp name="displayJMeterVariables">true</boolProp>
            <boolProp name="displaySamplerProperties">true</boolProp>
            <boolProp name="displaySystemProperties">false</boolProp>
          </DebugPostProcessor>
          <hashTree/>
        </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>
    <WorkBench guiclass="WorkBenchGui" testclass="WorkBench" testname="WorkBench" enabled="true">
      <boolProp name="WorkBench.save">true</boolProp>
    </WorkBench>
    <hashTree/>
  </hashTree>
</jmeterTestPlan>
答案是完全错误的:json路径表达式中的变量是经过计算的,至少可以从JMeter 3.1中使用(我没有检查早期版本)

甚至没有必要添加${{uu_eval()}。您的示例不起作用,因为您使用了错误的表达式。正确的表达式应该是
$[${someInputVariable}]。name
。在$和[]之间没有点。JSON也是错误的

下面是一个示例jmx,其中包含根据
${someInputVariable}
的值填充
${NAME-VARIABLE}
的数据。要检查其工作方式,只需将其保存为jmx,请在JMeter中运行,并在“查看结果树”元素中的“调试后处理器”叶的“响应数据”选项卡上检查变量值

<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="3.1" jmeter="3.1 r1770033">
  <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.tearDown_on_shutdown">true</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>
      <Arguments guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
        <collectionProp name="Arguments.arguments">
          <elementProp name="someInputVariable" elementType="Argument">
            <stringProp name="Argument.name">someInputVariable</stringProp>
            <stringProp name="Argument.value">2</stringProp>
            <stringProp name="Argument.metadata">=</stringProp>
          </elementProp>
          <elementProp name="JSONToBeParsed" elementType="Argument">
            <stringProp name="Argument.name">JSONToBeParsed</stringProp>
            <stringProp name="Argument.value">[{&quot;name&quot;:&quot;a&quot;},{&quot;name&quot;:&quot;b&quot;},{&quot;name&quot;:&quot;c&quot;}]</stringProp>
            <stringProp name="Argument.metadata">=</stringProp>
          </elementProp>
        </collectionProp>
      </Arguments>
      <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>
        <boolProp name="ThreadGroup.scheduler">false</boolProp>
        <stringProp name="ThreadGroup.duration"></stringProp>
        <stringProp name="ThreadGroup.delay"></stringProp>
        <longProp name="ThreadGroup.start_time">1535106337000</longProp>
        <longProp name="ThreadGroup.end_time">1535106337000</longProp>
      </ThreadGroup>
      <hashTree>
        <DebugSampler guiclass="TestBeanGUI" testclass="DebugSampler" testname="Debug Sampler" enabled="true">
          <boolProp name="displayJMeterProperties">false</boolProp>
          <boolProp name="displayJMeterVariables">true</boolProp>
          <boolProp name="displaySystemProperties">false</boolProp>
        </DebugSampler>
        <hashTree>
          <JSONPostProcessor guiclass="JSONPostProcessorGui" testclass="JSONPostProcessor" testname="JSON Extractor with constant" enabled="true">
            <stringProp name="JSONPostProcessor.referenceNames">NAME-CONSTANT</stringProp>
            <stringProp name="JSONPostProcessor.jsonPathExprs">$[1].name</stringProp>
            <stringProp name="JSONPostProcessor.match_numbers"></stringProp>
            <stringProp name="Sample.scope">variable</stringProp>
            <stringProp name="Scope.variable">JSONToBeParsed</stringProp>
          </JSONPostProcessor>
          <hashTree/>
          <JSONPostProcessor guiclass="JSONPostProcessorGui" testclass="JSONPostProcessor" testname="JSON Extractor with variable" enabled="true">
            <stringProp name="JSONPostProcessor.referenceNames">NAME-VARIABLE</stringProp>
            <stringProp name="JSONPostProcessor.jsonPathExprs">$[${someInputVariable}].name</stringProp>
            <stringProp name="JSONPostProcessor.match_numbers"></stringProp>
            <stringProp name="Sample.scope">variable</stringProp>
            <stringProp name="Scope.variable">JSONToBeParsed</stringProp>
          </JSONPostProcessor>
          <hashTree/>
          <DebugPostProcessor guiclass="TestBeanGUI" testclass="DebugPostProcessor" testname="Debug PostProcessor" enabled="true">
            <boolProp name="displayJMeterProperties">false</boolProp>
            <boolProp name="displayJMeterVariables">true</boolProp>
            <boolProp name="displaySamplerProperties">true</boolProp>
            <boolProp name="displaySystemProperties">false</boolProp>
          </DebugPostProcessor>
          <hashTree/>
        </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>
    <WorkBench guiclass="WorkBenchGui" testclass="WorkBench" testname="WorkBench" enabled="true">
      <boolProp name="WorkBench.save">true</boolProp>
    </WorkBench>
    <hashTree/>
  </hashTree>
</jmeterTestPlan>

你试过uuu evalVar或uuu eval函数吗?你试过uuu evalVar或uuu eval函数吗?这很好用。谢谢,@UBIK。我挣扎了好几个小时。这很好用。谢谢,@UBIK。我挣扎了好几个小时。