title: Parsing a JSON query string into a JSON object
body: <p>The query on the API is structured like this:</p>
  
  <pre><code>../myapi/products?_q=genre,retail_price&amp;_a=g,p&amp;p.gt=10&amp;p.lt=20&amp;g.eq=POP
  </code></pre>
  
  <p>There are two arrays: _q which lists the query parameters and _a listing the corresponding aliases. So p -> retail_price and g -> genres</p>
  
  <p>I can parse this into:</p>
  
  <pre><code>{$and : [ genre: { '$eq': 'POP' }, retail_price: { '$gt': '10' }, retail_price: { '$lt': '20' } ]}
  </code></pre>
  
  <p>Almost happy. But there are a couple of problems with this approach:
   1. the '$eq' etc instead of $eq etc
   2. the numeric value is now a string '10'</p>
  
  <p>I consider (2) to be a nasty one. Since the server cannot know the type (maybe it should be '10' instead of 10).</p>
  
  <p>So, I want to try another approach. And that is parsing it all into a queryString and then convert that with JSON.parse()</p>
  
  <p>First I put up some query string and try it in the shell:</p>
  
  <pre><code>db.products.find({$and: [{genre: { $eq: 'ROC'}}, {retail_price: {$gt:7}}, {retail_price: {$lt:10}}]})
  </code></pre>
  
  <p>Works like a charm.</p>
  
  <p>Then I tried this:</p>
  
  <pre><code>var queryStr = "{$and: [{genre: { $eq: 'ROC'}}, {retail_price: {$gt:7}}, {retail_price: {$lt:10}}]}";
  </code></pre>
  
  <p>and: (Product is a mongoose model)</p>
  
  <pre><code>Product.find(JSON.parse(queryStr), function(err, product) {
      if (err)
          res.send(err);
  
          res.json(product);
      });
  </code></pre>
  
  <p>To my surprise it did not work at all.</p>
  
  <p>Also doing</p>
  
  <pre><code>console.log(JSON.stringify(JSON.parse(queryStr)));
  </code></pre>
  
  <p>Does not write output to the console.       </p>
  
  <p>What is happening here?</p>