banner



How To Create Json File In Java

Technical Questions and Answers

  • Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Effective Resume Writing
  • HR Interview Questions
  • Computer Glossary
  • Who is Who

How to Write/create a JSON file using Java?


JSON or JavaScript Object Notation is a lightweight text-based open standard designed for human-readable data interchange. Conventions used by JSON are known to programmers, which include C, C++, Java, Python, Perl, etc. Sample JSON document

{    "book": [       {          "id": "01",          "language": "Java",          "edition": "third",          "author": "Herbert Schildt"       },       {          "id": "07",          "language": "C++",          "edition": "second",          "author": "E.Balagurusamy"       }    ] }

Json-simple library

The json-simple is a light weight library which is used to process JSON objects. Using this you can read or, write the contents of a JSON document using Java program.

JSON-Simple maven dependency

Following is the maven dependency for the JSON-simple library −

<dependencies>    <dependency>       <groupId>com.googlecode.json-simple</groupId>       <artifactId>json-simple</artifactId>       <version>1.1.1</version>    </dependency> </dependencies>

Paste this with in the <dependencies> </dependencies> tag at the end of your pom.xml file. (before </project> tag)

Example

To create a JSON document using a Java program −

  • Instantiate the JSONObject class of the json-simple library.
//Creating a JSONObject object JSONObject jsonObject = new JSONObject();
  • Insert the required key-value pairs using the put() method of the JSONObject class.
jsonObject.put("key", "value");
  • Write the created JSON object into a file using the FileWriter class as −
FileWriter file = new FileWriter("E:/output.json"); file.write(jsonObject.toJSONString()); file.close();

Following Java program creates a JSON object and writes it into a file named output.json.

Example

import java.io.FileWriter; import java.io.IOException; import org.json.simple.JSONObject; public class CreatingJSONDocument {    public static void main(String args[]) {       //Creating a JSONObject object       JSONObject jsonObject = new JSONObject();       //Inserting key-value pairs into the json object       jsonObject.put("ID", "1");       jsonObject.put("First_Name", "Shikhar");       jsonObject.put("Last_Name", "Dhawan");       jsonObject.put("Date_Of_Birth", "1981-12-05");       jsonObject.put("Place_Of_Birth", "Delhi");       jsonObject.put("Country", "India");       try {          FileWriter file = new FileWriter("E:/output.json");          file.write(jsonObject.toJSONString());          file.close();       } catch (IOException e) {          // TODO Auto-generated catch block          e.printStackTrace();       }       System.out.println("JSON file created: "+jsonObject);    } }

Output

JSON file created: { "First_Name":"Shikhar", "Place_Of_Birth":"Delhi", "Last_Name":"Dhawan", "Country":"India", "ID":"1", "Date_Of_Birth": "1981-12-05"}

If you observe the contents of the JSON file you can see the created data as −

raja

Published on 03-Jul-2019 11:48:08

  • Related Questions & Answers
  • How to Write/create a JSON array using Java?
  • How to create and write JSON array to a file in java?
  • How to write a JSON string to file using the Gson library in Java?
  • How can we write JSON objects to a file in Java?
  • How to read the contents of a JSON file using Java?
  • How to search a value inside a JSON file using Jackson in Java?
  • How to convert JSON file to CSV file using PowerShell?
  • How to add a JSON string to an existing JSON file in Java?
  • How to create a JSON using Jackson Tree Model in Java?
  • How to create a JSON Array using Object Model in Java?
  • How to create a JSON Object using Object Model in Java?
  • How to create a JSON using JsonObjectBuilder and JsonArrayBuilder in Java?
  • How can we create a JSON using JsonGenerator in Java?
  • How to read and write a file using Javascript?
  • How to write binary data to a file using Python?

How To Create Json File In Java

Source: https://www.tutorialspoint.com/how-to-write-create-a-json-file-using-java

Posted by: werneralwyet.blogspot.com

0 Response to "How To Create Json File In Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel