Home > Net >  JMeter groovy: how to get attributes value from json with attributes starting with a number
JMeter groovy: how to get attributes value from json with attributes starting with a number

Time:06-02

I do have the following string which I parse with the JsonSlurper() to json.

{"id":"1111","fields":{"2_attribute":"111111","3_attribute":"11122222","4_attribute":"1111222211"}

I'm using in a JSR223 PreProcessor groovy script to get attribute "2_attribute" value with the following code log.info("attribute2 :" json2.fields.2_attribute); and getting the following exception:

2022-06-01 13:18:25,491 ERROR o.a.j.m.JSR223PreProcessor: Problem in JSR223 script, JSR223 PreProcessor2
javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script197.groovy: 24: Number ending with underscores is invalid @ line 24, column 50 @ line 24, column 50.
   age_type_id:"   json2.fields.2_attribute

How could I manage to get the value? attribute naming cannot change.

Thank you.

CodePudding user response:

I think you need to surround this 2_attribute with quotation marks as in Groovy object names cannot start with a number.

def theValueIamLookingFor = new groovy.json.JsonSlurper().parse(prev.getResponseData()).fields."2_attribute"

Demo:

enter image description here

More information:

  • Related