John Vaughan
John Vaughan

Reputation: 39

Using BlueMix trying to insert JSON data into a Cloudant database without having to Hard Code the format of the file inside Java

I downloaded the "Favorites" app for Bluemix Cloudant. It was nice, until I realized that the "fields" were hard coded in the java files on the server. For example the ResourseServlet Object has this hard coded create function.

create(Database db, String id, String name, String value, Part part,
        String fileName) throws IOException {

Accompanied by hard coded MAP builds

Map<String, Object> data = new HashMap<String, Object>();
data.put("name", name);
data.put("_id", id);
data.put("value", value);
data.put("creation_date", new Date().toString());

leading to a database save..

db.save(data);

This type of example is ok if you want all your fields from all your json files hard coded in the java code.

But that doesn't work for and I imagine most All the applications I work with return text files pre-formatted in a JSON format.

So I need to simply be able to have a function that can receive like a "FileInputStream" object or if I have to, I can use a BufferedReader and read the thing in to a String and try to save the object.

I really like the cloudant database, because it stores and retrieves JSON but now I have 1000's of JSON files I want to store there of different fields types and structures and cannot really hard code all those value pairs for every type.

Is there a cool library or function that if I already have a file, it just puts it in the database like

db.save(JSONurl);   

where the JSONurl is just a url from the web that send back JSON?

thanks for your help. Been up till 3am for a week trying to find that method. (it took me days to figure out that the fields were hard coded in the server code.... whoa)

I realize I could write a loop and try to parse the JSON data myself and re-encode it, but it seems like there should be something simpler and available.

Upvotes: 1

Views: 1551

Answers (2)

John Vaughan
John Vaughan

Reputation: 39

Thank you for the response on the JsonParser object. While I was doing more research, I figured out how to do something similar with a Gson Object from google where I build a HashMap from a Json formatted string or URL that returns JSON. I used a loop to build a string from an input file, but perhaps that step could be avoided, but it worked.

import com.google.gson.Gson;
import java.util.HashMap;
import java.io.BufferedReader;  
import java.io.InputStreamReader;   
import java.lang.StringBuilder;    
import java.net.URL;

...

URL myURL = new URL("http://... some url that returns JSON");
BufferedReader reader = new BufferedReader(new InputStreamReader(myURL.openStream()));

while (true) {
            iLine=reader.readLine();
            if (iLine == null) {
            break;
            }  // end if
            stringBuilder.append(iLine);
        } // end while
        reader.close();

HashMap<String, Object> myMap = gson.fromJson(stringBuilder.toString(), HashMap.class);

        myMap.put("_id", id + "");
        myMap.put("value", "Writing From URL Object String V2");
        db.save(myMap);

...   

Upvotes: 0

Alex da Silva
Alex da Silva

Reputation: 4590

You can use the db.save(JsonObject) to accomplish what you need.

See the sample code below. I am passing a hardcoded JSON string as a sample test, but you can read the JSON string from a file or URL if you need. Also I used JsonObject and JsonParser from Google, they are available in the Cloudant "Favorites" sample.

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

String JSONString = new String ("{ \"hot\": { \"season\": \"summer\", \"weather\": \"usually warm and sunny\" }, \"cold\": { \"season\": \"winter\", \"weather\": \"usually cold and snowy\" }, \"sneezy\": { \"season\": \"spring\", \"weather\": \"cool with rain and sun\" },\"colorful\": {\"season\": \"autumn\",\"weather\": \"breezes\"} }");              
JsonParser parser = new JsonParser();
JsonObject data = (JsonObject)parser.parse(JSONString);              
db.save(data);

This will create the following document in Cloudant:

{
  "_id": "9ec91813106c4aa69ad38e42b268e1f8",
  "_rev": "1-1d8ab47557aaeb1ac342cd4f5f153f16",
  "hot": {
    "season": "summer",
    "weather": "usually warm and sunny"
  },
  "cold": {
    "season": "winter",
    "weather": "usually cold and snowy"
  },
  "sneezy": {
    "season": "spring",
    "weather": "cool with rain and sun"
  },
  "colorful": {
    "season": "autumn",
    "weather": "breezes"
  }
}

Upvotes: 2

Related Questions