
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How can we write JSON objects to a file in Java?
The JSON is one of the widely used data-interchange formats and is a lightweight and language independent. The json.simple is a lightweight JSON processing library that can be used to write JSON files and it can be used to encode or decode JSON text and fully compliant with JSON specification(RFC4627). In order to read a JSON file, we need to download the json-simple.jar file and set the path to execute it.
Example
import java.io.*; import java.util.*; import org.json.simple.*; import org.json.simple.parser.*; public class JSONObjectWriterToFileTest { public static void main(String[] args) throws IOException { JSONObject obj = new JSONObject(); obj.put("Name", "Adithya"); obj.put("Course", "MCA"); JSONArray subjects = new JSONArray(); subjects.add("Subject1: DBMS"); subjects.add("Subject2: JAVA"); subjects.add("Subject3: PYTHON"); obj.put("Subjects:", subjects); try (FileWriter file = new FileWriter("/Users/User/Desktop/course1.json")) { file.write(obj.toJSONString()); System.out.println("JSON Object write to a File successfully"); System.out.println("JSON Object: " + obj); } } }
course1.json file
Output
JSON Object write to a File successfully JSON Object: {"Subjects:":["Subject1: DBMS","Subject2: JAVA","Subject3: PYTHON"],"Course":"MCA","Name":"Adithya"}
- Related Articles
- How can we read a JSON file in Java?
- How can we merge two JSON objects in Java?
- How we can convert Python objects into JSON objects?
- How to Write/create a JSON file using Java?
- Check whether we can write to a file in Java
- How can we convert a JSON array to a list using Jackson in Java?\n
- How can we convert a JSON string to a JSON object in Java?
- How to create and write JSON array to a file in java?
- How can we encode a JSON object in Java?
- How can we decode a JSON object in Java?
- How can we read & write a file using Gson streaming API in Java?
- How to write a JSON string to file using the Gson library in Java?
- How can we check if a JSON object is empty or not in Java?\n
- How can we create a JSON using JsonGenerator in Java?
- How can we parse a nested JSON object in Java?

Advertisements