title: Lua in Redis from JSON
body: <p>I have a list of JSON strings stored in Redis that looks something like this:</p>
  
  <pre><code>[
     { "ID": 25, "DomainID": 23455, "Name": "Stuff", "Value": 23 }, 
     { "ID": 35, "DomainID": 23455, "Name": "Stuff", "Value": 10 }
  ]
  </code></pre>
  
  <p>The key would be something like "Event:23455".</p>
  
  <p>Using a Lua script and ServiceStack.Redis how would I pull out an anonymous object containing only values where the value is less than 20?</p>
  
  <p>So what I want to return would look like this:</p>
  
  <pre><code>[{ "ID": 35, "Value": 10}]
  </code></pre>
  
  <p>Thanks.</p>
  
  <p><strong>UPDATE 03/31/2013:</strong></p>
  
  <p>After trying what has been suggested I now have a new problem. A Lua script syntax error.</p>
  
  <p>I'm getting a Lua syntax error about "expecting '=' near cjson". Here is the Lua script string (in C#) I am feeding to Redis: </p>
  
  <pre><code>string luaScript = "local tDecoded = cjson.decode(redis.call('GET', KEYS[1]));"
  + "local tFinal = {};"
  + "for iIndex, tValue in ipairs(tDecoded) do"
  + "     if tonumber( tValue.Value ) &lt; 20 then"
  + "        table.insert(tFinal, { ID = tValue.ID, Value = tValue.Value});"
  + "     end"
  + "end"
  + "return cjson.encode(tFinal);";
  </code></pre>
  
  <p>Are there any Lua or Redis Lua experts out there that can see what might be the problem? i.e. Does the Lua syntax look correct?</p>
  
  <p><strong>UPDATE 04/02/2013</strong></p>
  
  <p>The parsing error was resolved by adding \n newline characters to end of each line like so.</p>
  
  <pre><code>string luaScript = "local tDecoded = redis.call('GET', KEYS[1]);\n"
  + "local tFinal = {};\n"
  + "for iIndex, tValue in ipairs(tDecoded) do\n"
  + "     if tonumber( tValue.Value ) &lt; 20 then\n"
  + "        table.insert(tFinal, { ID = tValue.ID, Value = tValue.Value});\n"
  + "     else\n"
  + "         table.insert(tFinal, { ID = 999, Value = 0});\n"
  + "     end\n"
  + "end\n"
  + "return cjson.encode(tFinal);";
  </code></pre>
  
  <p>Unfortunately this works without errors but for some reason only returns "{}" or an empty list in the ServiceStack RedisClient. So I'm not there yet but I am one step closer.</p>