How to write data in txt file using jmeter

142 Views Asked by At

I am able to write the data in my txt file but I am unable to print the Barcode with "" and I want to print the text (Barcode) in double quotes. For eg:

{"Barcode":"148279"},

2

There are 2 best solutions below

0
Nitin On

Use below code.

import groovy.json.JsonOutput

def data = [
    {"Barcode":"148279"}
]
def json_str = JsonOutput.toJson(data)
def json_beauty = JsonOutput.prettyPrint(json_str)
File file = new File(filename)
file.write(json_beauty)
0
Dmitri T On

We cannot properly suggest add "barcode" question marks without knowing how exactly do you "write" your .txt file.

The most straightforward solution is considering string concatenation, to wit just surround the text with quotation marks and that would be it.

If you want to write a custom piece of data into a custom file during JMeter load test you should be using Flexible File Writer as other approaches will result into corrupt data due to race conditions so most probably string concatenation should do the trick for you