-1

We need to read JSON from STDIN. Input gives one line of ugliefied JSON. Output should be formatted JSON. Check the standard output link. Use 2 white spaces (not‘\t’) for one indentation.

SAMPLE INPUT:

{“group” : {list : [1,2,3]}, “list” : [“a”,”b”,”c”]}

SAMPLE OUTPUT:

{

“group” : {

List : [1,2,3]

},

“list” : [“a”,”b”,”c”]

}

Here is the code I am using:

public class JSONPetty {

    public static void main(String[] args) {
         String myJsObj = "{“group” : {list : [1,2,3]}, “list” : [“a”,”b”,”c”]}";
         System.out.println(isMatched(myJsObj));    
    }

    public static StringBuffer isMatched(String expression) {
         final String opening = "{"; // opening delimiters
         final String closing = "}"; // respective closing delimiters
         StringBuffer temp = null;
         Stack < Character > buffer = new Stack <Character > ();
         for (char c: expression.toCharArray()) {
             if (opening.indexOf(c)!= -1) // this is a left delimiter
             {
                try {
                     //Here temp is not appending its returning null value
                     temp.append("{" + "\n");   
                } catch (Exception e) {
                    System.out.println(e.toString());
                }  
             }
             if (closing.indexOf(c)!= -1) // this is a left delimiter
             {
                 temp.append("}" + "\n");
             }  
         }
         return temp;
     }
}

Also, need to know if anyone has any other better solutions.

user656802
  • 13
  • 1
  • 6
  • 3
    Possible duplicate of [Pretty-Print JSON in Java](https://stackoverflow.com/questions/4105795/pretty-print-json-in-java) – Aniket Sahrawat Jan 15 '18 at 18:49
  • Its not duplicate. Before posting I have crosscheck these things. – user656802 Jan 15 '18 at 18:53
  • 1
    Mind explaining why it can not be considered as duplicate? Both the questions have same intention! – Aniket Sahrawat Jan 15 '18 at 18:55
  • What is wrong with the code? – takendarkk Jan 15 '18 at 18:57
  • My intension here to use stack as whenever I traversing the string and whenever I am getting "{" or "}" I need to add "\n". I am not looking for inbuilt function to do work here. So not a possible duplicate. – user656802 Jan 15 '18 at 18:58
  • @csmckelvey "temp.append("{" + "\n");" is retuning null, was expecting { then new line followed by other things. But getting null value, not clicking why? – user656802 Jan 15 '18 at 19:00
  • Please update your question to include the error and details since that is what you are actually asking about. – takendarkk Jan 15 '18 at 19:00
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – takendarkk Jan 15 '18 at 19:01
  • @csmckelvey, Correct. This is the second same mistake I did in my last program also. Thanks for correcting it. Also do you have any better suggestion for this program without using inbuilt functions. – user656802 Jan 15 '18 at 19:04
  • 1
    Make sure you remove those "Microsoft Word quote" characters from the input string. Those are unicode characters and won't match any legal char value the way you expect. use escapes and regular char quotes instead, like `"{\"group\" : {list..."` – geneSummons Jan 15 '18 at 19:05
  • @geneSummons Thanks. I have noted down your points. – user656802 Jan 15 '18 at 19:07

2 Answers2

0

The reason you're getting the Exception is because your StringBuffer variable temp is never set to anything. You declare it as null and then try to call the append method on it. You should instead make it a new StringBuffer like this:

StringBuffer temp = new StringBuffer();
Francis Bartkowiak
  • 1,374
  • 2
  • 11
  • 28
0

You might be getting only empty braces in the output, the reason could be that - you are not appending anything to the string buffer in case the character does not happen to be '{' or '}'. Also temp.append("}" + "\n"); would put } on the same line.

import java.util.*;
public class MyClass {
    public static StringBuffer isMatched(String expression) {
         final String opening = "{"; // opening delimiters
         final String closing = "}"; // respective closing delimiters
         StringBuffer temp = new StringBuffer();
         Stack < Character > buffer = new Stack <Character > ();
         char[] ch = expression.toCharArray();
         for (int i = 0 ; i < ch.length ;i++) {
             char c = ch[i];
             if (opening.indexOf(c)!= -1) // this is a left delimiter
             {
            
                     //Here temp is not appending its returning null value
                     temp.append("{" + "\n");   
                
             }
             else if (closing.indexOf(c)!= -1) // this is a left delimiter
             {
                 char extraComma = (i+1) < ch.length ? ',' : ' ';
                 temp.append("\n" + "}"+ extraComma + "\n");
                 i++;
             } 
             else{
                 temp.append(c);
             }
             
         }
         return temp;
     }
    public static void main(String args[]) {
        String myJsObj = "{\"group\" : {list : [1,2,3]}, \"list\" : [\"a\",\"b\",\"c\"]}";
        System.out.println(isMatched(myJsObj));  
    }
}

You can refer to this code.