import java.util.ArrayList;
import java.util.List;

public class Main {

    private static String json =
            "{ \n" +
            "  \"vinForRepair\" : \"ABCDE123455432199\",\n" +
            "  \"dateOfRepair\" : \"17/08/2021\",\n" +
            "  \"mileage\" : 100000,\n" +
            "  \"items\" : [ {\n" +
            "    \"description\" : \"Water pump\",\n" +
            "    \"quantity\" : 1,\n" +
            "    \"price\" : 120.0,\n" +
            "    \"metric\" : \"UNIT\n" +
            "}, {         <---------This should be ignored\n" +
            "    \"description\" : \"Motor oil\",\n" +
            "    \"quantity\" : 1,\n" +
            "    \"price\" : 30.0,\n" +
            "    \"metric\" : \"LITER\"\n" +
            "  } ]\n" +
            "}, {         <---------This is where i want to split\n" +
            "  \"vinForRepair\" : \"ABCDE123455432100\",\n" +
            "  \"dateOfRepair\" : \"15/08/2021\",\n" +
            "  \"mileage\" : 250000,\n" +
            "  \"items\" : [ {\n" +
            "    \"description\" : \"Break fluid\",\n" +
            "    \"quantity\" : 1,\n" +
            "    \"price\" : 20.0,\n" +
            "    \"metric\" : \"LITER\"\n" +
            "  }, {       <---------This should be ignored\n" +
            "    \"description\" : \"Tyre\",\n" +
            "    \"quantity\" : 2,\n" +
            "    \"price\" : 80.0,\n" +
            "    \"metric\" : \"UNIT\"\n" +
            " } ]\n" +
            "}";

    public static void main(String[] args) {
        List<String> jsons = split(json);
        System.out.println(jsons.size());
        jsons.forEach(it -> {
            System.out.println("-------");
            System.out.println(it);
        });
    }

    private static List<String> split(String json) {
        List<String> result = new ArrayList<>();
        int depth = 0;
        int start = 0;
        for (int i = 0; i < json.length(); i++) {
            if (json.charAt(i) == '{') {
                if (depth == 0) {
                    start = i;
                }
                depth++;
            }
            if (json.charAt(i) == '}') {
                depth--;
                if (depth == 0) {
                    result.add(json.substring(start, i + 1));
                }
            }
        }
        return result;
    }

}
