Jmeter Tutorial: How to Send a JSON File as Request in Body

In this JMeter Tutorial, we explain how to send one or multiple JSON files in the body of the HTTP request.

When testing a RESTful API, we normally send a POST request to the REST API in JSON format. The request parameters in JSON format needs to be sent in the body of the HTTP request.

First, you will need a text file with the JSON request. We will use this file to send to our REST API in the body of the POST request.

Suppose, the file is located in /Users/testing-excellence/Perf/blog/json_request.txt

jmeter-json-request-post

Our JMeter script will look something like this:

jmeter-post-request-json

In other words, we need to select the Body Data in the HTTP Sampler and insert the following line of code

${__FileToString(/Users/testing-excellence/Perf/blog/${__eval(${json_file})}.txt,,)}

Obviously, the path will be different on your machine.

The above line takes the contents of the file named json_file.txt and sends the contents in the body of the request to the REST API.

As JMeter is mainly used for performance and load testing, we can load test our REST API by sending multiple JSON requests.

To do that we make use of the JMeter’s CVS Data Set Config element.

In the CSV file, we have a column with the names of the JSON request files. This CSV file serves as a reference to the JSON files.

Suppose we have 10 JSON files, with filenames 100.txt, 101.txt, 103.txt….110.txt

jmeter-post-json-request

 

And the CSV file will look like:

jmeter-csv-json-request

 

Then in JMeter, we add the CSV Data Set Config element to our test plan

json-csv-request-post-jmeter

The variable is JSON_FILE which takes the values of 100, 101, 102, etc… from the CSV file.

Then we need to reference this variable in our __FileToString()  JMeter function, i.e.

jmeter-json-file-csv-request

When we execute this JMeter test, it will loop through the CVS, taking each file name and extract the contents of each file to send as a JSON in the body of the request.

8 Replies to “Jmeter Tutorial: How to Send a JSON File as Request in Body”

  1. Hello Amir,

    Thanks for sharing, found very useful. However a basic question, Isn’t it we need to change the count Number of Threads (Users) based on total number of sample txt to be processed. E.g In your example if i need to parse/send/processed 10 JSON.txt then i need to keep the Number of Thread (Users) in my Thread group to 10?

    Just thought to get your opinion on this.

    1. Hi Hussain,
      Thanks for your comment. I don’t believe that to be the case. You can loop through a CSV file even with 1 thread (user) as long as you have the loop count set to forever (but obviously controlling the duration via the scheduler in Jmeter).

      10 Threads would mean 10 concurrent users (after the initial ramp-up period) each of which can execute a line in CSV file or in this example a Json txt file.

      I think the same principle applies to sending over the Json Requests text files.

      1. I have related question. How about if we have 2 threads and 10 JSON files.
        Does that mean when thread one finishes, it will pick up the next JSON file and when thread 2 finishes it will pick the next available JSON file?

  2. When i do this I get an error” Cannot convert object of type \u0027System.Int32\u0027 to type \u0027System.Collections.Generic.IDictionary`2[System.String,System.Object]\u0027″,”StackTrace”:” at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object\u0026 convertedObject)\r\n at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object\u0026 convertedObject)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)”,”ExceptionType”:”System.InvalidOperationException”}” is this because the txt is read as a string and cannot be passed as a parameter?

  3. Hello Amir,

    How do we handle if the body of JSON request have parameters which are data driven (parameters reading from a CSV file) ?

    The body reads the parameters as ${param1} and not the value read from CSV file.

    Could you pls advice.

  4. Amir,
    thank you for the article.

    Using two sets of files, one with JSON request, one with file names looks a bit cumbersome.

    Is there a way to have a file with multiple jsons, one per each line,
    loop over the file and post once per each JSON line?

  5. Hello,

    Many thanks for this article.
    I have a little error when i call eval , jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: “import org.apache.jmeter.util.JMeterUtils; im….

    :'(

Leave a Reply