0

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"}
marcus
  • 141
  • 2
  • 10

1 Answers1

1

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.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Edited and updated to include if I actually escape the characters, what will happen. The error occurs somewhere along the JSON parser in Jongo – marcus Aug 20 '14 at 21:46
  • then it's STILL an invalid object. `{...}` in JSON is a javascript OBJECT. After escaping all of the internal quotes, you've STILL got the equivalent of `{"key"}`, which is not permitted. The entirely of the json-encoded "thing" is a string - that's json's whole purpose. taking a complex (or simple) javascript structure and reducing it to a single string. But you **HAVE** to obey its syntax rules. – Marc B Aug 20 '14 at 21:48
  • I'm aware of that. But when escaping and it still converts to JSON, the \ appear in the JSON string and I think the problem has to do with "{#}". Whether it's jongo or java that adds in the inner double quotes when it shouldn't, I don't know, but is there a way around it? – marcus Aug 20 '14 at 21:52
  • that'd probably be because you're double-encoding. doing your own json encoding, then the system takes that string and encodes it again. generally speaking, you never generator your own json. you build a native data structure in whatever language you're using, then you pass that structure to a json library for encoding. – Marc B Aug 20 '14 at 21:53
  • That's really my only option I see. Jongo has it's own syntax that's incredibly close and similar to Mongo, Mongo uses BSON for it's query structures. My problem is rather than having to call 8 or so different methods and combining like objects in teh return lists, I want to pass just one singular query to Mongo and have it return my list. I don't know how many fields the user will filter by (1 to 10 fields). I am trying to create a dynamic way. Another Jongo method that I know works: {return domains().find("{ failed : #}", failed).as(OneDomain.class);} – marcus Aug 20 '14 at 21:59
  • As you can see, it uses a style similar to JSON that I'm trying to emulate here to create a dynamic query string. – marcus Aug 20 '14 at 22:00