Nishanthravi
Nishanthravi

Reputation: 11

How to compare two different JSON response structure in java?

I have to compare two different JSON structure and want to know the mismatch's taken place in the response of each node.The response compared will be having different attribute name but values are equal.I need to know the way to map the attributes of the two response.

example:

I have to provide the overall comparison like(just a template to compare the attributes)

info array          ------------> Details array
image attribute     ------------> photo attribute     
name attribute      ------------> text attribute     
type attribute      ------------> use attribute
homepage object     ------------> Toppage object

So,that after comparing my result, it should display the mismatch in the value of the attribute in my log.

Still now, i am just using the jmeter for getting the response from the call and writing the java code in the beanshell sampler.The package I am using rite now is json.org.Now, i am comparing each attribute and printing the logs.The purpose of doing this is, we get json data from client but the datas which are required are filtered and constructed again using our code.So tat a structured JSON is formed and I have to verify and print the log error.

Suppose Consider Two response Structure:

JSON A:

{
  "info": [
    {
      "image": "small.jpg",
      "name": "flipkart_small",
      "Type": "logo"
    },
    {
      "image": "medium.jpg",
      "name": "flipkart_medium",
      "Type": "popup"
    },
    {
      "image": "large.jpg",
      "name": "flipkart_large",
      "Type": "banner"
    }
  ],
  "homepage": {
    "Electronic": {
      "name": "Electronic",
      "type": "category",
      "link": "http://flipkart.com/Electronic"
    },
    "image": {
      "src": "Images/samsung_s4.png",
      "name": "s4",
      "width": 250,
      "height": 250,
      "alignment": "center"
    },
    "Description": {
      "text": "Flipkart is a leading destination for online shopping in India.",
      "size": 36,
      "style": "bold",
      "name": "text1",
      "alignment": "justify"
    }
  }
}


JSON B:

{
  "Details": [
    {
      "photo": "small.jpg",
      "text": "flipkart_small",
      "use": "logo"
    },
    {
      "photo": "medium.jpg",
      "text": "flipkart_medium",
      "use": "popup"
    },
    {
      "photo": "large.jpg",
      "text": "flipkart_large",
      "use": "banner"
    }
  ],
  "Toppage": {
    "ElectronicSection": {
      "text": "Electronic",
      "use": "category",
      "url": "http://flipkart.com/Electronic"
    },
    "photo": {
      "path": "Images/samsung_s4.png",
      "name": "s4",
      "w": 250,
      "h": 250,
      "display": "center"
    },
    "information": {
      "desp": "Flipkart is a leading destination for online shopping in India.",
      "length": 36,
      "style": "bold",
      "value": "text1",
      "display": "justify"
    }
  }
}

Upvotes: 0

Views: 2916

Answers (1)

Dmitri T
Dmitri T

Reputation: 168072

First of all I'd recommend using of JSONPathExtractor Post Processor component which provided by JMeter Plugin (you'll need "Extras with Libs set" one).

See Parsing JSON section on how to use JSON Path.

Upvotes: 1

Related Questions