json.decode() returns nil

I’m trying to get som data from a database via php scripts and into my app. I’ve done this successfully before, but now json.decode() acts up on me and always return nil.

This is the app code:

local json = require("json") local myNewData local decodedData local function checkItemNetworkListener( event ) if ( event.isError ) then print( "Network error!") else myNewData = event.response print ("From server: '" .. myNewData .. "'") decodedData = (json.decode(myNewData)) print ("Decoded data: " .. decodedData) local numRows = #decodedData print("Number of rows returned from database: " .. numRows) end end local php = "http://allthumbsdev.com/RegIt/test.php?it\_id=5728JHGK-NO2" print("PHP: " .. php) network.request(php, "GET", checkItemNetworkListener )

When I run this code, this is the output:

PHP: http://allthumbsdev.com/RegIt/item_request.php?it_id=5728JHGK-NO2

From server: ’

[{“Brand”:“Sony”,“Category”:“0”,“SubCategory”:“0”,“Status”:“0”}]’

Runtime error

        d:\programming\programming\corona\test cases\php\main.lua:20: attempt to concatenate upvalue ‘decodedData’ (a table value)…

The data looks good, but there seems to be something in front that shouldn’t be there. Maybe that

The simplified (removed proper error handling) PHP-script looks like this:

\<?php include 'utils.php'; $itemId = $\_GET["it\_id"]; $conn = mysqli\_connect(xxxxxxxxxxxxxxxxxxxxxxx); $retValJSON = array(); $sql = "SELECT Brand, Category, SubCategory, Status FROM regit\_item WHERE Id='$itemId' $BRAND\_S ORDER BY Category, SubCategory"; $result = mysqli\_query($conn, $sql); if ($result) { if ($result-\>num\_rows \> 0) while ($row = $result-\>fetch\_assoc()) $retValJSON[]=$row; } mysqli\_close($conn); echo json\_encode($retValJSON); ?\>

I have three questions:

  1. Where are those extra characters coming from?

  2. Are they the reason json.decode() returns nil?

  3. How do I get rid of them

Thanks!

After some research I’ve found that the first three characters are ascii 10 (LINEFEED).

So now the input to json.decode() should be:

[{“Brand”:“Sony”,“Category”:“0”,“SubCategory”:“0”,“Status”:“0”}]

But unfortunately it still returns nil, even with this “clean” string.

Are you sure it’s returning nil? The error in your output indicates the problem is that you’re trying to concatenate a string with a table (not allowed), which suggests the JSON is successfully being decoded into a table.

Arrrrgh! Where is a hole in the ground when you need it…

The print thing was a desperate attempt to debug something and instead became the problem itself… 

json.decode() actually did return nil at a certain stage, but this was also purely my own fault. In (another) attempt to get to the bottom of something I tried to hardcode a “correct” json string.

But instead of being this:

[{“Brand”:“Sony”,“Category”:“0”,“SubCategory”:“0”,“Status”:“0”}]

It was created like this:

[{‘Brand’:‘Sony’,‘Category’:‘0’,‘SubCategory’:‘0’,‘Status’:‘0’}]

And running json.decode() on the latter one indeed returns nil.

Anyway, many thanks for your help!

After some research I’ve found that the first three characters are ascii 10 (LINEFEED).

So now the input to json.decode() should be:

[{“Brand”:“Sony”,“Category”:“0”,“SubCategory”:“0”,“Status”:“0”}]

But unfortunately it still returns nil, even with this “clean” string.

Are you sure it’s returning nil? The error in your output indicates the problem is that you’re trying to concatenate a string with a table (not allowed), which suggests the JSON is successfully being decoded into a table.

Arrrrgh! Where is a hole in the ground when you need it…

The print thing was a desperate attempt to debug something and instead became the problem itself… 

json.decode() actually did return nil at a certain stage, but this was also purely my own fault. In (another) attempt to get to the bottom of something I tried to hardcode a “correct” json string.

But instead of being this:

[{“Brand”:“Sony”,“Category”:“0”,“SubCategory”:“0”,“Status”:“0”}]

It was created like this:

[{‘Brand’:‘Sony’,‘Category’:‘0’,‘SubCategory’:‘0’,‘Status’:‘0’}]

And running json.decode() on the latter one indeed returns nil.

Anyway, many thanks for your help!