

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Pretty print JSON using org.json library in Java?
The JSON is a lightweight, text-based and language-independent data exchange format. A.JSONObject can parse text from a string to produce a map-like object. The object provides methods for manipulating its contents, and for producing a JSON compliant object serialization. The files in the org.json package implement JSON encoders/decoders in Java. It also includes the capability to convert between JSON, XML, HTTP headers, Cookies, and CDL.
We can pretty-print a JSON using the toString(int indentFactor) method of org.json.JSONObject class, where indentFactor is the number of spaces to add to each level of indentation.
Syntax
public java.lang.String toString(int indentFactor) throws JSONException
Example
import org.json.*; public class JSONPrettyPrintTest { public static void main(String args[]) throws JSONException { String json = "{" + "Name : Jai," + "Age : 25, " + "Salary: 25000.00 " + "}"; JSONObject jsonObj = new JSONObject(json); System.out.println("Pretty Print of JSON:"); System.out.println(jsonObj.toString(4)); // pretty print json } }
Output
Pretty Print of JSON: { "Salary": 25000, "Age": 25, "Name": "Jai" }
- Related Questions & Answers
- Pretty print JSON using Jackson library in Java?
- Pretty print JSON using the flexjson library in Java?
- How to pretty print JSON using the Gson library in Java?
- Pretty print JSON using javax.json API in Java?
- How to pretty print json using javascript?
- Convert a JSON String to Java Object using the json-simple library in Java?
- Convert CSV to JSON using the Jackson library in Java?
- Convert JSON to/from Map using Jackson library in Java?
- Convert JSON object to Java object using Gson library in Java?
- Convert Java object to JSON using the Gson library in Java?
- How to convert Java object to JSON using GSON library?
- How to convert Java object to JSON using Jackson library?
- MongoDB print JSON without whitespace i.e. unpretty JSON?
- Convert a Map to JSON using the Gson library in Java?
- How to convert a JSON to Java Object using the Jackson library in Java?
Advertisements