Question

I am writing a test code to compare json value. Currently, it am using JSONAssert to compare my json value with api response and work fine.

But I think response json might be changed to support more feature. Then JSONAssert could report failed because there is extra field that isn't expected.

So I want to make a logic to remove some added elements before using JSONAssert.

Is there any simple way to remove desired fields from json ?

Example ) Current : { "field1": "AAAA", "field2": "BBBB" } Next : { "field1": "AAAA", "field3": "CCC" , "field2": "BBBB" }

I want to remove "field3" from json before using it.

No correct solution

OTHER TIPS

First the answer on your question

I assume you have parsed the JSON source string into JavaScript object structure. So then you can do the following:

// option 1
delete Next["field3"];

// option 2
delete Next.field3;

The first option should be the better one in most cases, because it allows you to generate property names dynamically.

On the other hand

If you now that your JSON data may contain other values then the expected, then your test may be altered or you need another test. In case of unit-testing (what I assume you are want to do here) you should test only a single particular thing in one test. For anything else you should write additional tests.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top