<p>In this tutorial we will see that JSON format convert to the DBObject of MongoDB. Using &#8220;com.mongodb.util.JSON&#8221; class to convert JSON data directly to a DBObject. <br />
For example, data represent in JSON format :</p>
<pre class="java" name="code">{
 "empName" : "Dinesh Rajput",
 "empAge" : 26,
 "salary" : 70000
}
</pre>
<p>To convert it to DBObject, you can code like this :</p>
<pre class="java" name="code">DBObject dbObject = (DBObject) JSON.parse("{"empName" : "Dinesh Rajput",
 "empAge" : 26,
 "salary" : 70000}");
</pre>
<p>
Create a Java class to convert JSON Data To DBObject.</p>
<pre class="java" name="code">package com.dineshonjava.mongo.test;

import java.net.UnknownHostException;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.util.JSON;

/**
 * @author Dinesh Rajput
 *
 */
public class JSONDocumentDemo {

 public static void main(String[] args) {
 
 try {
 
 Mongo mongo = new Mongo("localhost", 27017);
 DB db = mongo.getDB("dineshonjavaDB");
 
 // get a single collection
 DBCollection collection = db.getCollection("empCollection");

 // convert JSON to DBObject directly
 DBObject dbObject = (DBObject) JSON.parse("{'empName':'Dinesh Rajput', 'empAge': 26,'salary':70000}");
 
 collection.insert(dbObject);
 
 DBCursor cursorDoc = collection.find();
 while (cursorDoc.hasNext()) {
 System.out.println(cursorDoc.next());
 }
 
 System.out.println("Done");
 
 } catch (UnknownHostException e) {
 e.printStackTrace();
 } catch (MongoException e) {
 e.printStackTrace();
 }
 
 }

}
</pre>
<div align='center' id="ads-id"></div>
<p>If everything is fine then we will get the following output on the console.</p>
<div style="background-color: #ff99cc;"><b>output:</b></p>
<p>{ &#8220;_id&#8221; : { &#8220;$oid&#8221; : &#8220;5106c6eb581d9921d6f5ccee&#8221;} , &#8220;empName&#8221; : &#8220;Dinesh Rajput&#8221; , &#8220;empAge&#8221; : 26 , &#8220;salary&#8221; : 70000}<br />
Done</div>
<p><b>Download Source Code + Libs</b><br />
<a href="https://sites.google.com/site/dinesh9582486434/my-forms/MongoDBJSONDemo.zip?attredirects=0&;d=1" target="_blank"><b>MongoDBJSONDemo.Zip</b></a></p>
<p></p>
<div style="background-color: #ff99cc;"> ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;<b> ; ; ; ; <;<;<a href="https://dineshonjava.com/2013/01/java-mongodb-query-document-to-database.html">previous</a><;<; ; ; ; ; ; ; ; ; ; ; ; ; || <a href="https://dineshonjava.com/2013/01/introduction-to-mongodb.html">index ; </a>||  ;  ;  ;  ; >;>;<a href="https://dineshonjava.com/2013/01/authentication-to-mongodb-with-java.html">next</a>>;>;</b></div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/java-mongodb-query-document-to-database/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/spring-data-mongodb-examples/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '587'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…