Submit and vote on feature ideas.

Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

Diff JSON comparision by ignoring the order.

Parasofttoudaya
Parasofttoudaya Posts: 232 ✭✭

Hi Team

is there any way to ignore the order while comparing the json.

problem statement :
Posting an array of data from excel
while getting the response of same data, i am getting the order mismatch.
for example, 0th position goes to 1st position and vice versa.

Thanks
Udaya

Comments

  • Parasofttoudaya
    Parasofttoudaya Posts: 232 ✭✭
    edited February 2018

    Adding additional information:
    LeftHandside or actual:
    [
    {
    "product" : "One"
    "color" : "red",
    "valu" : "#f00"
    },
    {
    "product" : "Two"
    "color" : "green",
    "valu" : "#0f0"
    },
    {
    "product" : "Three"
    "color" : "blue",
    "valu" : "#00f"
    },
    {
    "product" : "Four"
    "color" : "cyan",
    "valu" : "#0ff"
    }
    ]

    RightHandside or expected:

    [
    {
    "product" : "Four"
    "color" : "cyan",
    "valu" : "#0ff"
    },
    {
    "product" : "Two"
    "color" : "green",
    "valu" : "#0f0"
    },
    {
    "product" : "Three"
    "color" : "blue",
    "valu" : "#00f"
    },
    {
    "product" : "One"
    "color" : "red",
    "valu" : "#f00"
    }
    ]

    There are only four products, But the order has changed.

  • jakubiak
    jakubiak Posts: 795 admin

    JSON diffs require array elements to be in the same order, which is why you are seeing this behavior. One way to do this is to convert your JSON to XML using the XML Converter tool, and then use the XML Sort tool to sort the XML before performing an XML diff using the Diff tool. This will require you to use XML within your regression control, which you will need to get by identifying what the sorted XML will look like and then pasting that into your Diff tool.

  • Parasofttoudaya
    Parasofttoudaya Posts: 232 ✭✭

    Gotcha. Thanks..

  • Parasofttoudaya
    Parasofttoudaya Posts: 232 ✭✭

    Suggestion is not working.
    I am able to use the XML convertor on my JSON response. On top of that i used the XML SORT, where as XML sort is not sorting my parsed response.

  • benken_parasoft
    benken_parasoft Posts: 1,228 ✭✭✭
    edited February 2018

    The XML Sort tool is somewhat deprecated. It was originally used to sort XML prior to diffing as a means to ignore element order. However, this has been somewhat obsoleted by the fairly recent addition of the XMLUnit diff engine which has a "Ignore element order" option. So, consider not using the XML Sort tool to simplify your test setup.

  • Parasofttoudaya
    Parasofttoudaya Posts: 232 ✭✭

    Thanks Ben its working!!