0
votes

I get the following response from server in JSON:

{
"global_id": 00001,
"name": "IMG_0747.JPG",  
"time_taken":   {  
"segment_1_time": 1,
"segment_2_time": 1,  
"segment_3_time": 27,  
"segment_4_time": 1,  
"segment_5_time": 16,  
"segment_6_time": 20,  
 }  
}

I want to extract the values of segment_1_time, segment_2_time,....,segment_6_time. To do so, I am using the following values in Regular Expression Extractor (for segment 1 only):

Reference Name: s1time  
Regular Expression: "segment_1_time": (\d+)  
Template: $1$  
Match No: 1  
Default Value: 0  

I wrote this script in Bean Shell Post Processor to get value of segment_1_time in order to use it for further processing:

String time_s1= vars.get("s1time");
log.info("Segment 1 time= "+time_s1);  

For some reason, I always get NULL in time_s1. I also tried using var time_s1 instead of String time_s1 but it gives null value too. Where am I going wrong?

2

2 Answers

0
votes

Regexp - not a good way to parce JSON. JSON Path extractor - is a simple way to do it. Read this article (scroll down to 'Parsing JSON' title) to understand how to do it

0
votes

Please make the following changes in your regular expression to make it work

Regular Expression: .segment_1_time":(.+?),

"Regular expression extractor does not return value if you give a space"

hope this will help