import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?m)^(?:(?:(?&V_Obj)|(?&V_Ary))|(?<Invalid>(?&Er_Obj)|(?&Er_Ary))(*SKIP)(*FAIL))(?(DEFINE)(?<Sep_Ary>(?:,(?!\\s*[\\}\\]])|(?=\\s*[\\]])))(?<Sep_Obj>(?:,(?!\\s*[\\}\\]])|(?=\\s*[\\}])))(?<Er_Obj>(?>\\{(?:\\s*(?&Str)(?:\\s*:(?:\\s*(?:(?&Er_Value)|(?<Er_Ary>\\[(?:\\s*(?:(?&Er_Value)|(?&Er_Ary)|(?&Er_Obj))(?:\\s*(?&Sep_Ary)|(*ACCEPT)))*(?:\\s*\\]|(*ACCEPT)))|(?&Er_Obj))(?:\\s*(?&Sep_Obj)|(*ACCEPT))|(*ACCEPT))|(*ACCEPT)))*(?:\\s*\\}|(*ACCEPT))))(?<Er_Value>(?>(?&Numb)|(?>true|false|null)|(?&Str)))(?<Str>(?>\"[^\\\\\"]*(?:\\\\[\\s\\S][^\\\\\"]*)*\"))(?<Numb>(?>[+-]?(?:\\d+(?:\\.\\d*)?|\\.\\d+)(?:[eE][+-]?\\d+)?|(?:[eE][+-]?\\d+)))(?<V_KeyVal>(?>\\s*(?&Str)\\s*:\\s*(?&V_Value)\\s*))(?<V_Value>(?>(?&Numb)|(?>true|false|null)|(?&Str)|(?&V_Obj)|(?&V_Ary)))(?<V_Ary>\\[(?>\\s*(?&V_Value)\\s*(?&Sep_Ary))*\\s*\\])(?<V_Obj>\\{(?>\\s*(?&V_KeyVal)\\s*(?&Sep_Obj))*\\s*\\}))";
final String string = "\n"
+ "================================\n"
+ "Valid JSON :\n"
+ "=================================\n\n"
+ "1. Nested Objects and Arrays (Company Structure)\n"
+ "This example demonstrates a company structure with departments, each containing a list of employees.\n\n"
+ "{\n"
+ " \"company\": \"Global Solutions Inc.\",\n"
+ " \"headquarters\": {\n"
+ " \"city\": \"New York\",\n"
+ " \"state\": \"NY\",\n"
+ " \"zip\": \"10001\"\n"
+ " },\n"
+ " \"departments\": [\n"
+ " {\n"
+ " \"name\": \"Engineering\",\n"
+ " \"manager\": {\n"
+ " \"id\": \"ENG001\",\n"
+ " \"firstName\": \"Alice\",\n"
+ " \"lastName\": \"Johnson\"\n"
+ " },\n"
+ " \"employees\": [\n"
+ " {\n"
+ " \"id\": \"EMP001\",\n"
+ " \"firstName\": \"Bob\",\n"
+ " \"lastName\": \"Williams\",\n"
+ " \"position\": \"Software Engineer\",\n"
+ " \"skills\": [\"Python\", \"Java\", \"SQL\"]\n"
+ " },\n"
+ " {\n"
+ " \"id\": \"EMP002\",\n"
+ " \"firstName\": \"Charlie\",\n"
+ " \"lastName\": \"Davis\",\n"
+ " \"position\": \"DevOps Engineer\",\n"
+ " \"skills\": [\"AWS\", \"Docker\", \"Kubernetes\"]\n"
+ " }\n"
+ " ]\n"
+ " },\n"
+ " {\n"
+ " \"name\": \"Marketing\",\n"
+ " \"manager\": {\n"
+ " \"id\": \"MKT001\",\n"
+ " \"firstName\": \"David\",\n"
+ " \"lastName\": \"Miller\"\n"
+ " },\n"
+ " \"employees\": [\n"
+ " {\n"
+ " \"id\": \"EMP003\",\n"
+ " \"firstName\": \"Eve\",\n"
+ " \"lastName\": \"Taylor\",\n"
+ " \"position\": \"Marketing Specialist\",\n"
+ " \"skills\": [\"SEO\", \"Content Creation\"]\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ " ],\n"
+ " \"projects\": [\n"
+ " {\n"
+ " \"projectId\": \"PROJ101\",\n"
+ " \"projectName\": \"New Product Launch\",\n"
+ " \"status\": \"In Progress\",\n"
+ " \"teamMembers\": [\"EMP001\", \"EMP003\"]\n"
+ " }\n"
+ " ]\n"
+ "}\n\n\n"
+ "2. GeoJSON Feature Collection (Geospatial Data)\n"
+ "This example uses the GeoJSON standard to represent a collection of geographic features.\n\n"
+ "{\n"
+ " \"type\": \"FeatureCollection\",\n"
+ " \"features\": [\n"
+ " {\n"
+ " \"type\": \"Feature\",\n"
+ " \"properties\": {\n"
+ " \"name\": \"Eiffel Tower\",\n"
+ " \"country\": \"France\",\n"
+ " \"city\": \"Paris\"\n"
+ " },\n"
+ " \"geometry\": {\n"
+ " \"type\": \"Point\",\n"
+ " \"coordinates\": [2.2945, 48.8584]\n"
+ " }\n"
+ " },\n"
+ " {\n"
+ " \"type\": \"Feature\",\n"
+ " \"properties\": {\n"
+ " \"name\": \"Central Park\",\n"
+ " \"country\": \"USA\",\n"
+ " \"city\": \"New York\"\n"
+ " },\n"
+ " \"geometry\": {\n"
+ " \"type\": \"Polygon\",\n"
+ " \"coordinates\": [\n"
+ " [\n"
+ " [-73.981, 40.768],\n"
+ " [-73.958, 40.796],\n"
+ " [-73.949, 40.793],\n"
+ " [-73.981, 40.768]\n"
+ " ]\n"
+ " ]\n"
+ " }\n"
+ " }\n"
+ " ]\n"
+ "}\n\n\n"
+ "3. Product Catalog with Variations (E-commerce)\n"
+ "This JSON structure represents a product catalog with various product details and variations.\n\n"
+ "{\n"
+ " \"catalogId\": \"CAT001\",\n"
+ " \"products\": [\n"
+ " {\n"
+ " \"productId\": \"PROD001\",\n"
+ " \"name\": \"T-Shirt\",\n"
+ " \"description\": \"Comfortable cotton t-shirt.\",\n"
+ " \"price\": 19.99,\n"
+ " \"availableSizes\": [\"S\", \"M\", \"L\", \"XL\"],\n"
+ " \"colors\": [\n"
+ " {\n"
+ " \"name\": \"Red\",\n"
+ " \"hexCode\": \"#FF0000\",\n"
+ " \"stock\": { \"S\": 10, \"M\": 15, \"L\": 8, \"XL\": 5 }\n"
+ " },\n"
+ " {\n"
+ " \"name\": \"Blue\",\n"
+ " \"hexCode\": \"#0000FF\",\n"
+ " \"stock\": { \"S\": 12, \"M\": 20, \"L\": 10, \"XL\": 7 }\n"
+ " }\n"
+ " ],\n"
+ " \"reviews\": [\n"
+ " {\n"
+ " \"userId\": \"USER001\",\n"
+ " \"rating\": 5,\n"
+ " \"comment\": \"Great quality and fit!\"\n"
+ " },\n"
+ " {\n"
+ " \"userId\": \"USER002\",\n"
+ " \"rating\": 4,\n"
+ " \"comment\": \"Good t-shirt, a bit pricey.\"\n"
+ " }\n"
+ " ]\n"
+ " },\n"
+ " {\n"
+ " \"productId\": \"PROD002\",\n"
+ " \"name\": \"Jeans\",\n"
+ " \"description\": \"Classic denim jeans.\",\n"
+ " \"price\": 49.99,\n"
+ " \"availableSizes\": [\"28\", \"30\", \"32\", \"34\"],\n"
+ " \"colors\": [\n"
+ " {\n"
+ " \"name\": \"Indigo\",\n"
+ " \"hexCode\": \"#4B0082\",\n"
+ " \"stock\": { \"28\": 7, \"30\": 10, \"32\": 12, \"34\": 8 }\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ " ]\n"
+ "}\n\n\n"
+ "=========================\n"
+ "Invalid JSON :\n"
+ "=========================\n\n"
+ "These examples illustrate various ways in which a JSON file can be considered invalid according to the JSON specification. Developers often encounter these types of errors during data serialization or parsing.\n\n"
+ " -----------------\n"
+ "1. Missing Double Quotes around Keys:\n"
+ "Code\n\n"
+ "{\n"
+ " name: \"Alice\",\n"
+ " \"age\": 30,\n"
+ " \"city\": \"New York\"\n"
+ "}\n\n"
+ "Invalidity: The key name is not enclosed in double quotes, which is a requirement in JSON. \n\n"
+ " -----------------\n"
+ "2. Trailing Commas in Objects or Arrays:\n"
+ "Code\n\n"
+ "{\n"
+ " \"item1\": \"value1\",\n"
+ " \"item2\": \"value2\",\n"
+ "}\n\n"
+ " Invalidity: There is a trailing comma after the last key-value pair in the object. \n\n"
+ "Code\n\n"
+ "[\n"
+ " \"element1\",\n"
+ " \"element2\",\n"
+ "]\n\n"
+ "Invalidity: There is a trailing comma after the last element in the array. \n\n"
+ " -----------------\n"
+ "3. Incorrect Data Types or Values:\n"
+ "Code\n\n"
+ "{\n"
+ " \"product\": \"Laptop\",\n"
+ " \"price\": $1200.00,\n"
+ " \"available\": True\n"
+ "}\n\n"
+ "Invalidity: The price value $1200.00 is not a valid JSON number (currency symbols are not allowed). The available value True is not a valid JSON boolean (must be lowercase true or false). \n\n"
+ " -----------------\n"
+ "4. Mismatched Braces or Brackets:\n"
+ "Code\n\n"
+ "{\n"
+ " \"user\": {\n"
+ " \"id\": 123,\n"
+ " \"name\": \"Bob\",\n"
+ " \"details\": [\n"
+ " \"email\": \"bob@example.com\"\n"
+ " }\n"
+ " ]\n"
+ "}\n\n"
+ "Invalidity: The details array contains an object that is closed with a } instead of a ], and the array itself is closed with a ] after the object, leading to a structural mismatch. \n\n"
+ " -----------------\n"
+ "5. Unescaped Characters within Strings:\n"
+ "Code\n\n"
+ "{\n"
+ " \"message\": \"He said, \"Hello!\"\"\n"
+ "}\n\n"
+ " Invalidity: The double quotes within the string \"Hello!\" are not escaped, causing a syntax error. They should be \\\"Hello!\\\". \n\n"
+ " -----------------\n"
+ "6. Comments within JSON:\n"
+ "Code\n\n"
+ "{\n"
+ " // This is a comment\n"
+ " \"data\": \"some value\"\n"
+ "}\n\n"
+ " Invalidity: JSON does not support comments. \n\n"
+ " -----------------\n"
+ "7. Missing Colons or Values in Key-Value Pairs:\n"
+ "Code\n\n"
+ "{\n"
+ " \"key1\": \"value1\",\n"
+ " \"key2\"\n"
+ " \"key3\": \"value3\"\n"
+ "}\n\n"
+ " Invalidity: The key2 entry is missing a colon and a value. \n\n"
+ " -----------------\n"
+ "8. Invalid JSON Structure (e.g., array with object syntax):\n"
+ "Code\n\n"
+ "[\n"
+ " \"item1\": \"value1\",\n"
+ " \"item2\": \"value2\"\n"
+ "]\n\n"
+ "Invalidity: An array cannot contain key-value pairs; it should contain elements directly. \n\n\n"
+ "{\n"
+ " \"key1\" : \"value1\",\n"
+ " \"key2\" \"lk\"\n"
+ "}\n"
+ " -----------------\n\n"
+ "{\n"
+ " \"key1\" : \"value1\"\n"
+ " \"key2\" \"lk\"\n"
+ "}\n"
+ " -----------------\n\n"
+ "{\n"
+ " \"key1\" : \"value1\",\n"
+ " \"key2\" \"lk\"\n"
+ "}\n"
+ " -----------------\n\n"
+ "{\n"
+ " \"key1\" : \"value1\",\n"
+ " \"key2\" : mj\n"
+ "}\n"
+ " -----------------\n\n"
+ "{\n"
+ " \"key1\" : \"value1\",\n"
+ " \"key2\" : \"mj\n"
+ "}\n"
+ " -----------------\n\n"
+ "{\n"
+ " \"key1\" : \"value1\",\n"
+ " \"hhh\" : [ \n"
+ " { \n"
+ " \"key0\" : [{\"MMM\" : \"a\"}, \"b\",\"c\"],\n"
+ " \"key2\" : \"mj\",\n"
+ " \"key3\" :\n"
+ " [ \n"
+ " -----------------\n\n"
+ "[\n"
+ " \"mn\" \n"
+ " {\"MMM\" : a\"} ,\n"
+ " \"b\" , \"c\"\n"
+ "]\n\n";
final String subst = "$1 ERROR_here>> ";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);
System.out.println("Substitution result: " + result);
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html