How to convert Java class to Python T.T

Can someone tell me how to convert this java classes to python:
Java: Help T.T

File file = new File("/Users/gim-yuwan/Documents/project/RealResearch-API-BackEnd-Beta/src/main/resources/static/test.json");

            BufferedReader reader = new BufferedReader(new FileReader(file));

            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

            String s = "";

            Stack<Character> stack = new Stack<>();

            boolean flag = false;

            List<Jsoncolumn> result = new ArrayList<>();

            Answer answer = null;

            Questions questions = null;

            Jsoncolumn o = null;

            String lang="";

            boolean kycs=false;

            while ((s = reader.readLine()) != null) {

                for (char c : s.toCharArray()) {

                    if (c == '{' || c == '[') stack.push(c);

                    else if (c == ']' && stack.peek() == '[') stack.pop();

                    else if (c == '}' && stack.peek() == '{') stack.pop();

                }

                if (stack.size()==1&&s.contains("level") && s.contains("NumberInt")) {

                    flag = true;

                    o = new Jsoncolumn();

                    o.level = Integer.parseInt(s.replaceAll("[^0-9]", ""));

                    System.out.println(o.level);

                }

                if(stack.size()==2){

                    if (s.contains("kyc")&&!s.contains("s")) {

                        kycs=false;

                    }

                    if (s.contains("kycs")) {

                        kycs=true;

                    }

                }

                if (flag&&!kycs) {

                    if (stack.size() == 3) {

                        if ((s.contains("en") || s.contains("ko") || s.contains("ru"))&&!s.contains("languageType")) {

                            if(questions==null)questions=new Questions();

                            lang=s.split(":")[0].replaceAll("[\"']","").trim();

                            questions.lang = lang;

                        }

                    }

                    if (stack.size() == 5) {

                        if (s.contains("q_id")) {

                            if (questions == null) questions = new Questions();

                            questions.lang = lang;

                            questions.qId = Integer.parseInt(s.replaceAll("[^0-9]", ""));

                        }

                        if (s.contains("type")) {

                            if (questions == null) questions = new Questions();

                            questions.lang = lang;

                            String s2 = s.split(":")[1].replaceAll("[\",']", "").trim();

                            if(s2.toCharArray()[s2.length()-1]==',')s2 =s2.substring(0,s2.length()-1);

                            questions.type = s2;

                        }

                        if (s.contains("question")) {

                            if (questions == null) questions = new Questions();

                            String s2 = s.split(":")[1].replaceAll("[\"']", "").trim();

                            if(s2.toCharArray()[s2.length()-1]==',')s2=s2.substring(0,s2.length()-1);

                            questions.content = s2;

                        }

                    }

                    if (stack.size() == 7) {

                        if (answer == null) answer = new Answer();

                        if (s.contains("a_id")) answer.a_id = Integer.parseInt(s.replaceAll("[^0-9]", ""));

                        if (s.contains("answer")){

                            String s2 = s.split(":")[1].replaceAll("[\"']", "").trim();

                            if(s2.indexOf(s2.length())==',')s2 =s2.substring(0,s2.length()-2);

                            answer.answer = s2;

                        }

                    }

                    if (stack.size() == 6) {

                        if (answer != null) {

                            if (questions != null)

                                questions.answerList.add(answer);

                            answer = null;

                        }

                    }

                    if (stack.size() == 4) {

                        if (questions != null) o.questions.add(questions);

                        questions = null;

                    }

                }

                if(stack.isEmpty()){

                    if(o!=null)result.add(o);

                    o=null;

                }

            }

You didn’t include any of the imports or the class signature. It’s going to be difficult to accurately convert this without that information.

Generally to convert from Java to Jython you would drop type information, curly braces, semi-colons, add colons where appropriate, and add qualified imports ( from package import Class) at the top.

Also, the majority of that code snippet looks like an (extremely naive) JSON parser, which is something Python (and Ignition) can both do ‘natively’: https://docs.inductiveautomation.com/display/DOC81/system.util.jsonDecode; you can also use https://docs.inductiveautomation.com/display/DOC81/system.file.readFileAsString to read the file’s contents in one step, instead of Java’s verbose stream handling pattern.

3 Likes