John Sick
John Sick

Reputation: 436

Parse JSON Data and Save it to MongoDB

I have a JSON Data, which is save in a file and look like this.

{"@odata.context":"/redfish/v1/$metadata#ServiceRoot(Oem,Id,Name,RedfishVersion,UUID,Links,Systems,Chassis,Managers,Tasks,SessionService,AccountService,EventService,Registries,JsonSchemas)","@odata.id":"/redfish/v1/","@odata.type":"#ServiceRoot.v1_0_5.ServiceRoot","Oem":{"ts_fujitsu":{"@odata.type":"http://ts.fujitsu.com/redfish-schemas/v1/FTSSchema.v1_0_0#FTSServiceRoot.v1_0_0.FTSServiceRoot","FileDownload":{"@odata.id":"/redfish/v1/Oem/ts_fujitsu/FileDownload"},"CASConfiguration":{"Enabled":false,"DisplayLoginPage":false,"ServerLoginUrl":null,"ServerLogoutUrl":null}}},"Id":"RootService","Name":"Root Service","RedfishVersion":"1.0.5","UUID":"74fce745-28ad-410a-bbf8-9291d486b457","Links":{"Oem":{},"Sessions":{"@odata.id":"/redfish/v1/SessionService/Sessions"}},"Systems":{"@odata.id":"/redfish/v1/Systems"},"Chassis":{"@odata.id":"/redfish/v1/Chassis"},"Managers":{"@odata.id":"/redfish/v1/Managers"},"Tasks":{"@odata.id":"/redfish/v1/TaskService"},"SessionService":{"@odata.id":"/redfish/v1/SessionService"},"AccountService":{"@odata.id":"/redfish/v1/AccountService"},"EventService":{"@odata.id":"/redfish/v1/EventService"},"Registries":{"@odata.id":"/redfish/v1/Registries"},"JsonSchemas":{"@odata.id":"/redfish/v1/JsonSchemas"},"@Redfish.Copyright":"Copyright 2014-2017 Distributed Management Task Force, Inc. (DMTF). For the full DMTF copyright policy, see http://www.dmtf.org/about/policies/copyright.","@odata.etag":"1511366122"}

Now I am using Java to save this data in JSONBject. I need to parse this data and save it to MongoDB. This data I am getting from URL. The code to retrieve data and save it to file are below.

public JsonObject authen() {
    JsonObject myRestData = new JsonObject();
    try{
          URL myUrl = new URL("http://{physical-system}/redfish/v1");
          URLConnection urlCon = myUrl.openConnection();
          urlCon.setRequestProperty("Method", "GET");
          urlCon.setRequestProperty("Accept", "application/json");
          urlCon.setConnectTimeout(5000);
          //set the basic auth of the hashed value of the user to connect
          urlCon.addRequestProperty("Authorization", GetMyCredentials() );
          InputStream is = urlCon.getInputStream();
          InputStreamReader isR = new InputStreamReader(is);
          BufferedReader reader = new BufferedReader(isR);
          StringBuffer buffer = new StringBuffer();
          String line = "";
          while( (line = reader.readLine()) != null ){
            buffer.append(line);
          }
          reader.close();
          JsonParser parser = new JsonParser();
          myRestData = (JsonObject) parser.parse(buffer.toString());

          return myRestData;

        }catch( MalformedURLException e ){
          e.printStackTrace();
          myRestData.addProperty("error", e.toString());
          return myRestData;
        }catch( IOException e ){
          e.printStackTrace();
          myRestData.addProperty("error", e.toString());
          return myRestData;
        }
}

public void writer(JsonObject o) throws JSONException, IOException {
    try (FileWriter file = new FileWriter("file1.json")) {
        file.write(o.toString());
        System.out.println("Successfully Copied JSON Object to File...");
        System.out.println("\nJSON Object: " + o);
    }
public void parser(JsonObject o) throws JSONException {
    JsonElement n = o.get("UUID");
    System.out.println(n);

Now, I want to save this JSON Data in mongoDB. I was thinking to save it by parsing it and then save it. But my data is too much to parse. I want to save all the data to MongoDB. And if i want anything from database I can query it easily. I am thinking of a smart way to do that. [I am newbie to MongoDB & JSON]

Upvotes: 1

Views: 7600

Answers (2)

Cedric
Cedric

Reputation: 552

You should use the latest version of the mongo-java-driver where you can simply write

MongoClient client = new MongoClient("<your connection string (optional)>");
MongoCollection<Document> collection = client.getDatabase("<Database>").getCollection("<Collection>");
Document doc = Document.parse(<Your JSON string>);
collection.insertOne(doc);

Your JSON string would be buffer.toString()

Upvotes: 1

Guillaume Barr&#233;
Guillaume Barr&#233;

Reputation: 4228

You can save your JSON directly into MongoDB using the method save of DBCollection.

Here is a list example :

DB db = mongo.getDB("your DB");
DBCollection collection = db.getCollection("your Collection");
final Object myRestData  = JSON.parse(buffer.toString());
collection.save((DBObject) myRestData );

Upvotes: 0

Related Questions