Answers for "How to ignore fields of json to compare in JSONAssert JSONCompare"

0

How to ignore fields of json to compare in JSONAssert JSONCompare

package IgnoringFieldsDuringComparision;
 
import org.json.JSONException;
import org.skyscreamer.jsonassert.Customization;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.skyscreamer.jsonassert.comparator.CustomComparator;
import org.skyscreamer.jsonassert.comparator.JSONComparator;
 
public class IgnoringFieldsFromNestedJsonArray {
 
 public static void main(String[] args) throws JSONException {
 
 String s1 = "[\r\n" + 
 "  {\r\n" + 
 "    \"id\": 1,\r\n" + 
 "    \"first_name\": \"Amod\",\r\n" + 
 "    \"last_name\": \"Mahajan\",\r\n" + 
 "    \"married\": false,\r\n" + 
 "    \"salary\": 123.45,\r\n" + 
 "    \"mob\": [\r\n" + 
 "      {\r\n" + 
 "        \"type\": \"personal\",\r\n" + 
 "        \"number\": \"1234566\"\r\n" + 
 "      },\r\n" + 
 "      {\r\n" + 
 "        \"type\": \"business\",\r\n" + 
 "        \"number\": \"987654321\"\r\n" + 
 "      }\r\n" + 
 "    ]\r\n" + 
 "  },\r\n" + 
 "  {\r\n" + 
 "    \"id\": 2,\r\n" + 
 "    \"first_name\": \"Animesh\",\r\n" + 
 "    \"last_name\": \"Prashant\",\r\n" + 
 "    \"married\": true,\r\n" + 
 "    \"salary\": 223.45,\r\n" + 
 "    \"mob\": [\r\n" + 
 "      {\r\n" + 
 "        \"type\": \"personal\",\r\n" + 
 "        \"number\": \"1234566\"\r\n" + 
 "      },\r\n" + 
 "      {\r\n" + 
 "        \"type\": \"business\",\r\n" + 
 "        \"number\": \"987654321\"\r\n" + 
 "      }\r\n" + 
 "    ]\r\n" + 
 "  }\r\n" + 
 "]";
 
 String s2 = "[\r\n" + 
 "  {\r\n" + 
 "    \"id\": 1,\r\n" + 
 "    \"first_name\": \"Amod\",\r\n" + 
 "    \"last_name\": \"Mahajan\",\r\n" + 
 "    \"married\": false,\r\n" + 
 "    \"salary\": 123.45,\r\n" + 
 "    \"mob\": [\r\n" + 
 "      {\r\n" + 
 "        \"type\": \"personal\",\r\n" + 
 "        \"number\": \"1234566\"\r\n" + 
 "      },\r\n" + 
 "      {\r\n" + 
 "        \"type\": \"business\",\r\n" + 
 "        \"number\": \"34545646\"\r\n" + 
 "      }\r\n" + 
 "    ]\r\n" + 
 "  },\r\n" + 
 "  {\r\n" + 
 "    \"id\": 2,\r\n" + 
 "    \"first_name\": \"Animesh\",\r\n" + 
 "    \"last_name\": \"Prashant\",\r\n" + 
 "    \"married\": true,\r\n" + 
 "    \"salary\": 223.45,\r\n" + 
 "    \"mob\": [\r\n" + 
 "      {\r\n" + 
 "        \"type\": \"personal\",\r\n" + 
 "        \"number\": \"1234566\"\r\n" + 
 "      },\r\n" + 
 "      {\r\n" + 
 "        \"type\": \"business\",\r\n" + 
 "        \"number\": \"987654321\"\r\n" + 
 "      }\r\n" + 
 "    ]\r\n" + 
 "  }\r\n" + 
 "]";
 
 System.out.println(s1);
 System.out.println(s2);
 JSONComparator com = new CustomComparator(JSONCompareMode.STRICT, 
 new Customization("[0].mob[1].number", (o1, o2) -> true));
 
 JSONAssert.assertEquals(s1, s2, com);
 
 }
 
}
Posted by: Guest on October-08-2021
0

How to ignore fields of json to compare in JSONAssert JSONCompare

package IgnoringFieldsDuringComparision;
 
import org.json.JSONException;
import org.skyscreamer.jsonassert.Customization;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.skyscreamer.jsonassert.comparator.CustomComparator;
import org.skyscreamer.jsonassert.comparator.JSONComparator;
 
public class IgnoringFieldsFromNestedJsonArray {
 
 public static void main(String[] args) throws JSONException {
 
 String s1 = "[\r\n" + 
 "  {\r\n" + 
 "    \"id\": 1,\r\n" + 
 "    \"first_name\": \"Amod\",\r\n" + 
 "    \"last_name\": \"Mahajan\",\r\n" + 
 "    \"married\": false,\r\n" + 
 "    \"salary\": 123.45,\r\n" + 
 "    \"mob\": [\r\n" + 
 "      {\r\n" + 
 "        \"type\": \"personal\",\r\n" + 
 "        \"number\": \"1234566\"\r\n" + 
 "      },\r\n" + 
 "      {\r\n" + 
 "        \"type\": \"business\",\r\n" + 
 "        \"number\": \"987654321\"\r\n" + 
 "      }\r\n" + 
 "    ]\r\n" + 
 "  },\r\n" + 
 "  {\r\n" + 
 "    \"id\": 2,\r\n" + 
 "    \"first_name\": \"Animesh\",\r\n" + 
 "    \"last_name\": \"Prashant\",\r\n" + 
 "    \"married\": true,\r\n" + 
 "    \"salary\": 223.45,\r\n" + 
 "    \"mob\": [\r\n" + 
 "      {\r\n" + 
 "        \"type\": \"personal\",\r\n" + 
 "        \"number\": \"1234566\"\r\n" + 
 "      },\r\n" + 
 "      {\r\n" + 
 "        \"type\": \"business\",\r\n" + 
 "        \"number\": \"987654321\"\r\n" + 
 "      }\r\n" + 
 "    ]\r\n" + 
 "  }\r\n" + 
 "]";
 
 String s2 = "[\r\n" + 
 "  {\r\n" + 
 "    \"id\": 1,\r\n" + 
 "    \"first_name\": \"Amod\",\r\n" + 
 "    \"last_name\": \"Mahajan\",\r\n" + 
 "    \"married\": false,\r\n" + 
 "    \"salary\": 123.45,\r\n" + 
 "    \"mob\": [\r\n" + 
 "      {\r\n" + 
 "        \"type\": \"personal\",\r\n" + 
 "        \"number\": \"1234566\"\r\n" + 
 "      },\r\n" + 
 "      {\r\n" + 
 "        \"type\": \"business\",\r\n" + 
 "        \"number\": \"34545646\"\r\n" + 
 "      }\r\n" + 
 "    ]\r\n" + 
 "  },\r\n" + 
 "  {\r\n" + 
 "    \"id\": 2,\r\n" + 
 "    \"first_name\": \"Animesh\",\r\n" + 
 "    \"last_name\": \"Prashant\",\r\n" + 
 "    \"married\": true,\r\n" + 
 "    \"salary\": 223.45,\r\n" + 
 "    \"mob\": [\r\n" + 
 "      {\r\n" + 
 "        \"type\": \"personal\",\r\n" + 
 "        \"number\": \"1234566\"\r\n" + 
 "      },\r\n" + 
 "      {\r\n" + 
 "        \"type\": \"business\",\r\n" + 
 "        \"number\": \"987654321\"\r\n" + 
 "      }\r\n" + 
 "    ]\r\n" + 
 "  }\r\n" + 
 "]";
 
 System.out.println(s1);
 System.out.println(s2);
 JSONComparator com = new CustomComparator(JSONCompareMode.STRICT, 
 new Customization("[0].mob[1].number", (o1, o2) -> true));
 
 JSONAssert.assertEquals(s1, s2, com);
 
 }
 
}
Posted by: Guest on October-08-2021

Code answers related to "How to ignore fields of json to compare in JSONAssert JSONCompare"

Code answers related to "Javascript"

Browse Popular Code Answers by Language