0
 public void process(FeedExchange exchange) throws Exception {
        List<BasicDBObject> collectionAttributes = (List<BasicDBObject>) exchange.getInput();
        for (BasicDBObject collectionAttribute : collectionAttributes) {
          if (collectionAttribute.get("name") != null) {
            String attributeName = collectionAttribute.getString("name");
            if (attributeName.equals(JobParamConstants.DEFAULT_LOCALE) || attributeName
              .equals(JobParamConstants.APPLICABLE_LOCALES)) {
              exchange.setProperty(attributeName, collectionAttribute.getString("_id"));
            }
          }

hi i need to write junit testcase for above program..so i want to pass input to collectionAttributes.my input json is GetCatalogCollectionResponse.json

{
"properties":[{
"attributeId":"123",
"value":"345"
},
{
"attributeId":"2345",
"value":"567"
}]
}

i want to parse this json to collectionAttributes in mongodb.i tried the following code

BasicDBObject DBObject = new BasicDBObject();
DBObject.parse(GetCatalogCollectionResponse.json);

but i got an error.could you help me i am beginner to java,any help would be appreciated..

1
  • The method DBObject.parse is used to parse a JSON string. Commented Oct 7, 2020 at 6:34

2 Answers 2

1

You can use the Google's Gson library to parse the JSON from a file as shown below. The parsed object can be mapped to a java.util.Map, from which you can build the BasicDBObject.

BufferedReader reader = new BufferedReader(new FileReader("test_file.json"));
Gson gson = new Gson();
Map<String, Object> map = gson.fromJson(reader, Map.class);
System.out.println(map);
// {properties=[{attributeId=123, value=345}, {attributeId=2345, value=567}]}

BasicDBObject obj = new BasicDBObject(map);
System.out.println(obj.toJson());
// {"properties": [{"attributeId": "123", "value": "345"}, {"attributeId": "2345", "value": "567"}]}
Sign up to request clarification or add additional context in comments.

4 Comments

hi thank you..the json file was stored under src/test/resources in ecllipse and i tried this line in src/java/test folder .BufferedReader reader = new BufferedReader(new FileReader("src/test/resources/test_file.json")).it shows FileNotFoundException exception.how can i pass this test_file.json ..correct me i m beginner in java.. –
I Googled with this search string: "how to access file in resources folder in java", and found quite a few answers. I am sure you will find something useful for your need.
thank you i found an answer for how to access file in resource folder.
0

finally i found answer for my question.

public class BasicDBObjectInput {

  public static String getInput(String resourceName) throws IOException {
    ClassLoader classLoader = BasicDBObjectInput.class.getClassLoader();
    File file = new File(classLoader.getResource(resourceName).getFile());
    String json = FileUtils.readFileToString(file, Charset.defaultCharset());

    return json;
  }

}

here we can pass our json file in string format like this

String json = `BasicDBObjectInput.getInput("GetCatalogCollectionAttributeResponse.json"); BasicDBObject collectionAttributes = BasicDBObject.parse(json);`

hope it will helps somebody.

Comments

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.