Corona SDK: Get JSON data from the Internet

If you want to receive a parsed JSON string from the Internet in your Corona project do as in the following example:

Include JSON library

require "json"

Create the functions

Then create the functions which get the JSON string from the Internet.

local jsonFile = function( filename, base )

-- set default base dir if none specified
if not base then base = system.ResourceDirectory; end

-- create a file path for corona i/o
local path = system.pathForFile( filename, base )

-- will hold contents of file
local contents

-- opens a file at path. returns nil if no file found
local file = path, "r" )
if file then
-- read all contents of file into a string
contents = file:read( "*a" )
io.close( file ) -- close the file after using it

return contents

local function networkListener( event )
if ( event.isError ) then
print( "Network error!")
print ( "RESPONSE: " .. event.response )
local t = json.decode( event.response )

-- Go through the array in a loop
for key in pairs(t) do
-- Here you can do whatever you like with the values



With the following lines of code you can call the above created functions to get data from the Internet.

postData = "parameter1=value1&parameter2=value2&parameter3=value3"

local params = {}
params.body = postData

local json_file_by_post = jsonFile( network.request( "", "POST", networkListener, params ) )


local json_file_by_get = jsonFile( network.request( "", "GET", networkListener ) )

About Dominik
Currently working as an Android/iOS developer for Cineman, Switzerland. Owner of and


2 Responses to “Corona SDK: Get JSON data from the Internet”
  1. Charlie sagt:

    What is the difference between your post and call? Can you give an example on each.


    • enatureadmin sagt:

      You mean the difference between the POST and GET call?
      With the POST call the variables are being sent to the server via HTTP POST. With GET the variables are being attached to the URL. If the server you are trying to get data from only reads HTTP POST calls then you have to use HTTP POST (or GET accordingly).

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

You must be logged in to post a comment.