jmeterconvertto-json

Is there a way in Jmeter JDBC request to convert "result variable" data into Json format?


I am storing Jmeter JDBC REquest data in result variable which come out in following format : [[alt_id:535644, cd:A, cube:1.000, stat_dt:null, ts:9999-12-31T00:01]]

I tried the following to convert it to JSON but not successful :

def resl = vars.getObject('jdbc_dcRes');
log.info("Jdbc result : " + resl);
def json = new groovy.json.JsonBuilder(resl).toPreetyString();
OR
def json = new groovy.json.JsonBuilder(resl).toString();
log.info("Jdbc result Converted into Json : " + json);

Getting : 2022-02-11 15:31:51,971 ERROR o.a.j.JMeter: Uncaught exception in thread Thread[dc Service 1-1,6,main] java.lang.StackOverflowError: null at java.util.concurrent.ConcurrentHashMap.putVal(Unknown Source) ~[?:1.8.0_111] at java.util.concurrent.ConcurrentHashMap.putIfAbsent(Unknown Source) ~[?:1.8.0_111] at java.lang.ClassLoader.getClassLoadingLock(Unknown Source) ~[?:1.8.0_111] at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_111] at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) ~[?:1.8.0_111] at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_111] at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_111] at java.lang.Class.forName0(Native Method) ~[?:1.8.0_111] at java.lang.Class.forName(Unknown Source) ~[?:1.8.0_111] at groovy.lang.MetaClassRegistry$MetaClassCreationHandle.createWithCustomLookup(MetaClassRegistry.java:144) ~[groovy-3.0.7.jar:3.0.7] at groovy.lang.MetaClassRegistry$MetaClassCreationHandle.create(MetaClassRegistry.java:139) ~[groovy-3.0.7.jar:3.0.7] at org.codehaus.groovy.reflection.ClassInfo.getMetaClassUnderLock(ClassInfo.java:272) ~[groovy-3.0.7.jar:3.0.7]

.............

Any pointer to overcome this would be helpful. Thanks


Solution

  • For me the following code snippet works just fine:

    def resl = vars.getObject('jdbc_dcRes')
    
    def json = new groovy.json.JsonBuilder(resl).toPrettyString()
    
    log.info('Jdbc result Converted into Json :' + json)
    

    Demo:

    enter image description here

    I'm using normal MySQL database 8.0.28 and the following query:

    select * from help_category limit 2
    

    So you might want to try to replicate the above and if it will be successful - most probably the problem is with the data from your DB which cannot be converted to JSON using Groovy's JsonBuilder without extra transformation (i.e. you have specific data types which cannot be mapped 1-to-1 to JSON data types)

    See Debugging JDBC Sampler Results in JMeter to learn more about anatomy of the beast called "Result variable name" in JMeter's JDBC Request sampler