# How to convert Java class to Python T.T

**URL:** <https://forum.inductiveautomation.com/t/how-to-convert-java-class-to-python-t-t/42800>\
**Category:** Ignition\
**Tags:** python\
**Created:** [February 1, 2021, 3:26am UTC](https://forum.inductiveautomation.com/t/how-to-convert-java-class-to-python-t-t/42800 "2021-02-01T03:26:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kjy0124kr](https://avatars.discourse-cdn.com/v4/letter/k/f07891/32.png) [@kjy0124kr](https://forum.inductiveautomation.com/u/kjy0124kr)\
**Post date:** [February 1, 2021, 3:26am UTC](https://forum.inductiveautomation.com/t/how-to-convert-java-class-to-python-t-t/42800/1 "2021-02-01T03:26:28Z")

</div>

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

```auto
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;

                }

            }

```

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [February 1, 2021, 2:21pm UTC](https://forum.inductiveautomation.com/t/how-to-convert-java-class-to-python-t-t/42800/2 "2021-02-01T14:21:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [February 1, 2021, 6:54pm UTC](https://forum.inductiveautomation.com/t/how-to-convert-java-class-to-python-t-t/42800/3 "2021-02-01T18:54:20Z")

</div>

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;](https://docs.inductiveautomation.com/display/DOC81/system.util.jsonDecode;) you can also use [https://docs.inductiveautomation.com/display/DOC81/system.file.readFileAsString](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.
