How to run JSON queries with arguments?

Hello : )

I want to keep my queries in JSON format and work for example in Java.
(instead of using query builders)
I can do this, but i cant make dynamic queries with that,like passing arguments to a query.

String json = ReadMyQueryFromDisk(query_name);
Document myquery = Document.parse(json);

Does someone knows any library that support this functionality?Allowing us to use JSON queries with arguments in JAVA? If you know any library that does this even if its not java, if you can send the link.

Thank you

I do that too.

I have a very simple solution. I replace dynamic values with place holder. Once I read my query as a Document from a file, like ReadMyQueryFromDisk, I process the query into a very simple template like processor where all place holders are replaced with real values. For example:

// I want to query the sales between 2 dates.
// My stored JSON query document will be

query_read_from_file = { "$and" : [ { "date" : { "$gte" : "_from_date_place_holder" } } ,
             { "date" : { "$lte" : "_to_date_place_holder" } } ] }

// The next step is to create a Document that maps the place holder to real values

values = new Document( "_from_date_place_holder" , TheRealFromDate ).
    append( "_to_date_place_holder" , TheRealToDate ) ;

// A simple for loop replaces all place holders in the query with the real values from values.
2 Likes

Thank you steevej.

You do this?

  1. query from disk(special symbolism for the parameters like _ in front) => string query;
  2. string query => Document (with Document.parse(query);) = queryDoc
  3. create a hash-map with your parameters (strings to Java Objects)
  4. search on that queryDoc (like all keys and all members of the arrays)
    and replace the parameters from the hash-map?

If you have the code ready(i guess its the same in all queries) if you can send the complete code, including the loop if possible, if you don’t have it ready its ok i will try it sometime.

This is a solution many people use? Are there any reasons to not do it?
(assuming we prefer to write our queries in MQL from a Query builder)

Yes that is the general approach.

I wrote and have the code but I cannot share it because I do not own it. But I am allowed to share my ideas.

Yes it is the same.

I have tried thing like https://www.stringtemplate.org/ and https://freemarker.apache.org/ but the difficulties with them was to have the value with the correct class. Doing the replacement my self allow to have the correct class for the parameters because I am creating the hash-map of parameters.

2 Likes

ok thank you, you helped me anyways : ) I will write that code sometime.

I leave the question open

  • if someone has implementation in any language to post it, so we dont all make our custom implementations.
  • Also are there any reasons to not use this approach?
    (assuming that we prefer to write MQL instead of using the various query builders)

This worked brilliantly. Thank you so much!