Parse to Lua Tips¶
JSON "objects" and Lua tables have a similar looking structure, but differ in the following ways:
General¶
Lua uses an equal sign =
as its setter. JSON uses a colon :
as its setter. Both use curly braces as "objects":
--Lua local tbl_obj = { username = "Jim", age = 23 }
//JSON (javascript) var json_obj = { "username": "Jim", "age": 23 }
JSON uses quoted strings as key names. This is optional for Lua in most cases, except in the cases of spaces, etc. For example, to send a key name with a space or special character (like $) then you must wrap it in a set of brackets as a quoted string.
For example:
local t = { username = "Ed", ["key with spaces"] = true }
There are more examples of this in the "Unique Keys" section below.
Arrays¶
Arrays in JSON are enclosed in brackets:
//JSON (javascript) var json_arr = [ 'green', 'blue', 'red' ]
In Lua, use a table array instead:
--Lua local tbl_arr = { 'green', 'blue', 'red' }
Unique Keys¶
If you have a key with uncommon chars, spaces, etc., you must wrap it with brackets and quotes so that it will be converted to JSON properly from Lua.
--== Nope, even though $gt is a valid Parse key. local data_tbl = { $gt = 200 } --== Yes! Wrap the value in brackets and quotes. local data_tbl = { ["$gt"] = 200 }
--== Nope. local data_tbl = { my awesome color = "Green" } --== Yes! local data_tbl = { ["my awesome color"] = "Green" }
This is not needed for common keys:
--== Using common keys local data_tbl = { color = "Green", dogs = 3 } --== Mixing common and unique local data_tbl = { score = { ["$gt"] = 200 } }
Conversions¶
In the Parse REST API guide you will see lots of usage examples. You need to do table conversions on the data you will send from Lua. In the examples, the data
property is the -d flag, followed by some JSON data.
# Parse.com example code curl -X POST \ ... -d '{"score":1337,"playerName":"Sean Plott","cheatMode":false}' \ https://api.parse.com/1/classes/GameScore
This particular data structure (-d) is a simple conversion to Lua:
local data = { score = 1337, playerName = "Sean Plott", cheatMode = false }
The following ACL structure is a bit more work:
# Parse.com example code curl -X POST \ ... -d '{ "name": "Moderators", "ACL": { "*": { "read": true } } }' \ https://api.parse.com/1/roles
Again, from the -d key above, a conversion to Lua land:
local data = { name = "Moderators", ACL = { ["*"] = { read = true } } }
Query data require the same conversion:
curl -X GET \ ... --data-urlencode 'where={"score":{"$gte":1000,"$lte":3000}}' \ https://api.parse.com/1/classes/GameScore
From -data-urlencode flag to Lua:
local query = { score = { ["$gte"] = 1000, ["$lte"] = 3000 } }
Note
You might have noticed that the where= is not part of the query. This parameter is added during request processing.