Home > other >  JMeter failed to keep JSON order by using fastjson JSONObject.parseObject
JMeter failed to keep JSON order by using fastjson JSONObject.parseObject

Time:11-14

Backgroud:

In Jmeter, I need to get the json post data, and then parse to the JSONObject

But the key order should the same

The json post data is: { "one": "1", "two": "2", "three": "3", "four": "4" }

After JSON Object parse, it also should be: { "one": "1", "two": "2", "three": "3", "four": "4" }

Solution:

In pure java, I could use the following code to keep the order

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;

String requestBody = "{ \"one\": \"1\", \"two\": \"2\", \"three\": \"3\", \"four\": \"4\" }";
log.info("********************requestBody: "   requestBody);

// Expected: keep the JSONObject key oder same as the body data
// But following code not working in JMeter JSR223 Sampler, it works fine in pure JAVA
JSONObject maps = JSONObject.parseObject(requestBody, Feature.OrderedField);
log.info("********************maps expected: "   maps);

Issue:

But the above code is not working in JMETER JSR223 Sampler, I get the following error:

Static method parseObject( java.lang.String, com.alibaba.fastjson.parser.Feature ) not found in class'com.alibaba.fastjson.JSONObject'...

Reproduce:

Save the following code as .jmx file to reproduce in JMeter

You need to download the fastjson 1.2.83, and put to the jmeter lib folder: enter image description here

Since JMeter 3.1 it's recommended to use Groovy language for scripting mainly because Groovy performance is much better comparing the other scripting options. Moreover Groovy supports Java better and Beanshell doesn't support any Java 5 feature.

  • Related