0

I am writing an API to merge two different JSON Objects. Both of these objects can have same as well different number of properties.

Object 1 :

{
    "name": "Sam",
    "lastName": "Hanks",
    "address":[
        {
            "addressLine1": "123 St.",
            "addressLine2": "xyz avenue",
            "phone": "1231-123-123"
        },
        {
            "addressLine1": "123 Building",
            "addressLine2": "xyx state Apt.",
            "phone": "1234-124-124"
        }
    ],
    "additionalInfo":{
        "age": 23,
        "height":"1.65",
        "weight": 45
    }
}

Object 2:

{
    "name": "Tom",
    "lastName": "Hanks",
    "address":[
        {
            "addressLine1": "123 St.",
            "addressLine2": "xyz avenue",
            "phone": "1231-124-124"
        }
    ],
    "additionalInfo":{
        "height":"1.65",
        "weight": 89
    }
}

These two objects when compared on the basis of properties, name, lastName, address, etc, will have some differences.

I would be getting all these differences in a data structure and will present it to the user as conflicts.

I have the code to get all the differences but I am not able to construct a data model to present it in a graceful way. A way which is readable and also at the same time can be parsed easily again when user resolve the conflict and I contruct a combined object in the next operation.

I have looked at many applications like, diffchecker, to see how they do it, but I am not able get some clue.

Is there a good way to do it using REST APIs ? All I want to know is, how can I represent differences in two json objects in json format.

1
  • Hi Code-47, welcome to softwareengineering.stackexchange.com. Your question appears to be only concerned with differences of two JSON structures and the REST API part is not important. If you, you can consider removing that part of your question.
    – Benni
    Dec 19, 2019 at 19:16

1 Answer 1

1

one option is to model it as a list of changes or differences. In your example:

[{ attribute: "name", type: "changes", left:"Sam", right: "Tom"}, { attribute: "lastName", type: "equal", left:"Hanks", right: "Hanks"},...]

This is the approach that several libraries take, like npm-deep-diff or Guava's map difference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.