
- 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 to add a JSON string to an existing JSON file in Java?
A Gson is a json library for Java and it can be used to generate a JSON. In the initial step, we can read a JSON file and parsing to a Java object then need to typecast the Java object to a JSonObject and parsing to a JsonArray. Then iterating this JSON array to print the JsonElement. We can create a JsonWriter class to write a JSON encoded value to a stream, one token at a time. Finally, a new JSON string can be written to an existing json file.
Example
import java.io.*; import java.util.*; import com.google.gson.*; import com.google.gson.stream.*; import com.google.gson.annotations.*; public class JSONFilewriteTest { public static String nameRead; public static void main(String[] args) { try { JsonParser parser = new JsonParser(); Object obj = parser.parse(new FileReader("employee1.json")); JsonObject jsonObject = (JsonObject) obj; System.out.println("The values of employee1.json file:\n" + jsonObject); JsonArray msg = (JsonArray)jsonObject.get("emps"); Iterator<JsonElement> iterator = msg.iterator(); while(iterator.hasNext()) { nameRead = iterator.next().toString(); System.out.println(nameRead); } Name name = new Name(); name.setName("Vamsi"); Gson gson = new Gson(); String json = gson.toJson(name); FileWriter file = new FileWriter("employee1.json", false); JsonWriter jw = new JsonWriter(file); iterator = msg.iterator(); Employees emps = new Employees(); while(iterator.hasNext()) { emps.addEmployee(gson.fromJson(iterator.next().toString(), Name.class)); } emps.addEmployee(name); gson.toJson(emps, Employees.class, jw); file.close(); System.out.println("data added to an existing employee1.json file successfully"); } catch(Exception e) { e.printStackTrace(); } } } // Name class class Name { @Expose private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } // Employees class class Employees { @Expose List<Name> emps = new ArrayList<>(); public List<Name> getEmployees() { return emps; } public void addEmployee(Name name) { this.emps.add(name); } }
Output
The values of employee1.json file: {"emps":[{"name":"Adithya"},{"name":"Jai"}]} {"name":"Adithya"} {"name":"Jai"} data added to an existing employee1.json file successfully
employee1.json file
- Related Articles
- How to deserialize a JSON into an existing object in Java?
- How can we convert a JSON string to a JSON object in Java?
- How to add elements to JSON Object using JSON-lib API in Java?
- How to write a JSON string to file using the Gson library in Java?
- How to convert a JSON string to a bean using JSON-lib API in Java?
- How to serialize a JSON string to an Output Handler in Java?
- How to Write/create a JSON file using Java?
- How to add/insert additional property to JSON string using Gson in Java?\n
- How to add/append content to an existing file using Java?
- How to read an external JSON file in JavaScript
- How to create and write JSON array to a file in java?
- Convert a JSON String to Java Object using the json-simple library in Java?\n
- How can we read a JSON file in Java?
- How to add an element to a JSON object using JavaScript?
- How to read JSON file in Python

Advertisements