JMeter – Working with JSON

This post explains what is JSON and how to Send JSON Payload in HTTP Request using JMeter.


  1. What is JSON?
    JSON is simply a data format, but one which is more naturally fit for browser data consumption. The reason for this is that JSON is a subset to JavaScript, the de facto programming language used in all browsers. By structuring a data payload as a JSON response, you are effectively bypassing the need to parse an XML document in a browser — typically done via JavaScript of course — to get to the actual data. 
    In this sense, JSON uses a stripped down syntax compliant with the native JavaScript interpreter provided on all browsers. Access and navigation to JSON data is done through the same standard JavaScript notation used to access string, array or hashtable values in a typical JavaScript application. Listing 1.1 illustrates what a JSON payload would look like.
    Listing 1.1 : JSON payload example

    {“item”: {
    “description”: {
    “title”: “Backroad Mountain Bikes”,
    “name”: “Stump Jumper”,
    “color”: “chrome”,
    “image”: “/images/stumpjumper.jpg”,
    “sizes”: “17,18,19″
    },
    “cost”: {
    “salestax”: CA,
    “rate”: 7,
    “base”: 550,
    “custompaint”: 150,
    “warranty”: 50,
    “travelcase”: 150
    },
    “instock”: “yes”
    }}

    This example was taken from an article published in 2006, under the following link:
    JavaScript Object Notation for Ajax Web services
    Ok, so basically JSON works with brackets, square and round paranthesis. It does indeed follow pretty much the JavaScript notation. And it misses the end tags in xml, which can (not sure about it yet) generate extra payload if xml is used (of course, there are some extra chars there…)
    That being said, the only thing left was actually to integrate the JSON strings into my JMeter HTTP Request.
  2. Send JSON Payload in HTTP Request using JMeter
    Nothing simpler than this (yeah, right…now i’m saying it)…The only thing to do is to add the JSON Payload as Variable to be sent with the request (check it in the HTTP Sampler Properties for the HTTP Request that you are sending to the server). In simple words, all you need to do is just go to the Thread group --> add sampler --> HTTP Request --> Then select "HTTP Request" --> Go to the component named "Send Parameters with the Request" -->Click "Add" button --> Paste your JSON payload into the value part, you dont need to give it a name --> Also, add a HTTP Header Manager and add a Header as: name: "Content-Type" and value: "application/json". That's it, now modify it according to your requirements and you are ready to go. You do not need to name your variable. Sending the JSON Payload as value, for an empty variable should do the trick. Just make sure you copy the JSON Payload accordingly.
    After posting the Request, you can analyze the result, by rendering the JSON Result, using JMeter’s integrated JSON Rendere (if you use the Result Tree, select your request, and go to the third tab, the one called “Response Data”, and from the bottom of the window, select “Render JSON”.

    http://alexandru-ersenie.com/2009/08/28/json-and-jmeter/#comments

Comments

Popular posts from this blog

Existence: Absolute & Relative

How to create an XML Log file in Java using JAXB (Customized)