1

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]

2 Answers 2

1

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()

Sign up to request clarification or add additional context in comments.

6 Comments

I still didn't get the concept of buffer.toString(). I have my data in file. Can you please walk me through it?
You're reading the whole website content (JSON) into a StringBuffer. So you can just parse the JSON-String into a Document or JsonObject or whatever. In case you're having the data in a file, you just read the file fully into a StringBuffer and parse it into e.g. a Document. (You should use a StringBuilder instead of a StringBuffer tho)
Didn't manage to do it. I am getting an error "JSON reader was expecting a value but found 'java' ".
Can you tell me how can avoid this error: java.lang.IllegalArgumentException: Invalid BSON field name @odata.context In my json file, first character is odata.context. and i can't able to do anything.
See here. BSON (MongoDB JSON Format) doesn't allow '.' or '$' in JSON keys. You can simply remove all '@odata.'. These are just some unnecessary prefixes imo.
|
0

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 );

1 Comment

JSON string means: JSON file path?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.