0

I want to compare 2 json and return error message for which object or value is not match

For example:

json1 = {
    "id": 1,
    "product": {
        "productId": "456",
        "product_detail": [
            {
                "name": {
                    "value": 1
                }
            },
            {
                "name": {
                    "value": 2
                }
            }
        ]
    }
}

json2 = {
    "id": 1,
    "product": {
        "productId": "123",
        "product_info": [
            {
                "name": [{
                    "value": 3,
                }
            },
            {
                "name": {
                    "value": 4
                }
            }
        ]
    }
}

I was using try / catch on each object and value, however, it is complicated process to const each propriety. Therefore, i am looking for another way to do the checking

My expected error message result should return all object / value is not matching in test result:

Failed - json2 > product_info propriety not matching with json

Failed - json2 > product.product_detail.name[0].value : 3 not equal with json1 > product.product_detail.name[0].value :1

Failed - json2 > product.product_detail.name[1].value : 4 not equal with json1 > product.product_detail.name[1].value :2

If there are no error found > return message pass

1 Answers1

0

are you sure you need recursion for that?

Checkout lodash isEqual function: https://lodash.com/docs/#isEqual

cucumber
  • 57
  • 5