Posted In: JSON

Convert json string to pretty print json output using JSON library

 

 

1. Example will use following Test data

private String testData() {
	String json = "{" + "	\"portfolio\": [" 
			+ "		{"
	        + "			\"stock\": \"Infosys\","
	        + "			\"quantity\": \"100\","
	        + "			\"buy\": \"1011\","
	        + "			\"profit\": \"10000\"" 
	        + "		},"
	        + "		{"
	        + "			\"stock\": \"State Bank of India\","
	        + "			\"quantity\": \"1000\","
	        + "			\"buy\": \"204\","
	        + "			\"profit\": \"20000\"" 
	        + "		},"
	        + "		{"
	        + "			\"stock\": \"Reliance Industries\","
	        + "			\"quantity\": \"100\","
	        + "			\"buy\": \"1200\","
	        + "			\"profit\": \"12000\"" 
	        + "		}"
	        + "	]" + "}";
	return json;
}

 

2. Using GSON.toJson

@Test
public void testGsonPrint() {
	try {
		Gson gson = new GsonBuilder().setPrettyPrinting().create();
		Reader reader = new StringReader(testData());
		Object object = gson.fromJson(reader,
		        Object.class);		
		System.out.println("\ntestGsonPrint()");
		System.out.println(gson.toJson(object));
	} catch (Exception e) {
		e.printStackTrace();
	}
}

 

3. Using Jackson 2X writerWithDefaultPrettyPrinter

@Test
public void testJackson2Print() {
	try {
		ObjectMapper mapper = new ObjectMapper();
		JsonNode expectedNode = mapper.readTree(testData());
		System.out.println("\ntestJackson2Print()");
		System.out.println(
		        mapper.writerWithDefaultPrettyPrinter()
		                .writeValueAsString(expectedNode));
	} catch (Exception e) {
		e.printStackTrace();
	}
}

 

4. Using Jackson 1X writerWithDefaultPrettyPrinter

@Test
public void testJackson1Print() {
	try {
		org.codehaus.jackson.map.ObjectMapper mapper = new org.codehaus.jackson.map.ObjectMapper();
		org.codehaus.jackson.JsonNode expectedNode = mapper
		        .readTree(testData());
		System.out.println("\ntestJackson1Print()");
		System.out.println(
		        mapper.writerWithDefaultPrettyPrinter()
		                .writeValueAsString(expectedNode));
	} catch (Exception e) {
		e.printStackTrace();
	}
}

 

5. Using org.json.JSONObject.toString(indent) from org.json library

@Test
public void testOrgJsonPrint() {
	try {
		org.json.JSONObject json = new org.json.JSONObject(testData());
		System.out.println("\ntestOrgJsonPrint()");
		System.out.println(json.toString(4));
	} catch (Exception e) {
		e.printStackTrace();
	}
}

 

6. Using net.sf.json.JSONObject.toString(indent) from net.sf.json-lib library

@Test
public void testNetSfJsonPrint() {
	try {
		JSONObject json = JSONObject.fromObject(testData());
		System.out.println("\ntestNetSfJsonPrint()");
		System.out.println(json.toString(4));
	} catch (Exception e) {
		e.printStackTrace();
	}
}

 

7. Output

{
  "portfolio" : [ {
    "stock" : "Infosys",
    "quantity" : "100",
    "buy" : "1011",
    "profit" : "10000"
  }, {
    "stock" : "State Bank of India",
    "quantity" : "1000",
    "buy" : "204",
    "profit" : "20000"
  }, {
    "stock" : "Reliance Industries",
    "quantity" : "100",
    "buy" : "1200",
    "profit" : "12000"
  } ]
}

 

Tags: , , , , ,

by , on February 26th, 2017

  • Categories