How to compare two different JSON response structure in java?

心不动则不痛 提交于 2020-01-06 17:58:35

问题


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"
    }
  }
}

回答1:


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.



来源:https://stackoverflow.com/questions/21495272/how-to-compare-two-different-json-response-structure-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!