marcus
marcus

Reputation: 179

Error with Jongo parsing JSON

I'm using a combination of Java Play Framework, MongoDB and Jongo as my go between for a basic web CRUD app. I keep receiving a JSON parse exception even though my string doesn't contain any illegal characters. It's actually failing on closing curly bracket at the end of the statement. Below is my error and code. The query string is just a string builder, searching if an object is empty or has a value, if it has a value it's appended to a string.

Jongo method:

public static Iterable<OneDomain> findWithQueryString(String queryString){
    return domains().find("{#}", queryString).as(OneDomain.class);
}

Controller Methods: String builder example:

        if(queryStringBuilder.toString().equalsIgnoreCase("")){
            queryStringBuilder.append("date: {$gte : " + searchObj.dateFrom + ", $lt: " + searchObj.dateTo + "}");
        }else{
            queryStringBuilder.append(" , ");
            queryStringBuilder.append("date: {$gte : " + searchObj.dateFrom + ", $lt: " + searchObj.dateTo + "}");
        }

        String queryString = queryStringBuilder.toString();

        Iterable<OneDomain> filteredIterable = OneDomain.findWithQueryString(queryString);

Gives me this error:

   Caused by: com.mongodb.util.JSONParseException:
   {"source : Online Lists , blacklist : true , vetted : true , is : false , isNot : true"}
                                                                                          ^

with the error on the "}" ending it.

In addition to that, if I try to escape the characters by putting in a \" so it becomes \"date\" it will parse and error out like so:

   Caused by: com.mongodb.util.JSONParseException:
   {"\"source\" : \"Online Lists\" , \"blacklist\" : true , \"vetted\" : true , \"is\" : false , \"isNot\" : true"}

Upvotes: 0

Views: 495

Answers (1)

Marc B
Marc B

Reputation: 360872

You're building JSON by hand, and doing it wrong. You need to learn the basic JSON syntax requirements

A basic JSON-encoded object is

{"key1":"value1","key2":"value with \" escaped internal quote"}

Note all of the quotes. Your json string is a single very long object key with no associated value, which is not permitted. All keys must have values.

Upvotes: 1

Related Questions