bbx402
bbx402

Reputation: 143

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

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.

Upvotes: 0

Views: 617

Answers (1)

Dmitri T
Dmitri T

Reputation: 168157

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:

Upvotes: 1

Related Questions