Facebook user ids too large for number

I’m executing a Facebook fql query using the Corona Facebook module to search for friends with names containing some value typed in by the user.  Even though I’m including “format=json-strings” in the url, the ids of the Facebook friends returned by the Corona Facebook module are being converted to a numbers.  For the most part this is fine, but some of the newer Facebook ids are too large for a 32 bit number and are being returned like the following, 1.0000130523746e+14.

Is there anyway to get the id value as a string so that it’s not converted to scientific notation?

Any ideas on how to work around this?

I had this problem when working with facebook and parse.com - in the end I stored the facebook id as a string by concatenating it with “F”.

Hi Jeremy, Nick,

Thanks for raising and looking at this.  Would you mind clarifying a bit more what you’ve been experiencing?  I understand the risk that a large number could be cast as a string in an unusable scientific notation.  But from what I can tell, the JSON responses from Facebook always contain user IDs as strings (within quotes), not as numbers.  So it’s not clear to me how/why this issue is arising.

Here’s a simple test case.  It doesn’t actually use the Facebook API, just works with a JSON in the format that the Facebook API would return.

[lua]

local json = require(“json”)

– Simulated response from a request to the Facebook API

local response = [[

{

    “data”: [

        {

          “name”: “User Name”, 

          “id”: “123456789123456789”

        }

    ]

}

]]

local results = json.decode(response)

print(results.data[1].id)     – Prints the full string

print(json.encode(results))    – Re-encoded as JSON (e.g., if saving to a file), the id is the full string

[/lua]

Are you seeing situations where the JSON from Facebook is not as above, but instead is providing the ID as a number?

  • Andrew

That is a very clever idea Nick.  Thanks for the help!

Andrew,

I can’t tell exactly where the id is being interpreted as a number.  Concatenating a character on the front of the id, in the FQL query, seems to fix things.

Interesting.  It does look like it’s an FQL issue.  Using the Graph API Explorer (https://developers.facebook.com/tools/explorer), I ran some simple FQL queries.  The query “SELECT name FROM user WHERE uid = me()” returns the id as a number, but the query “SELECT uid2 FROM friend WHERE uid1 = me();” returns the ids as strings (consistent with the documentation at https://developers.facebook.com/docs/reference/fql/friend which says uid1 and uid2 are numeric strings).

  • Andrew

Andrew,

When issuing a FQL query, you can use the parameter format=json-strings, to force the results to all be string values.  I’ve tested this in the browser and it behaves as expected.  When I use that with the Corona facebook framework, it either gets dropped or over written.

Jeremy,

Just thowing this out there… Does:

tostring( id )

Help at all?

Thanks Danny but tostring just turns the number 1.0000130523746e+14, into the string “1.0000130523746e+14”.  This seems to be a problem with representing a number that is larger than what a 32 bit field can hold.

I had this problem when working with facebook and parse.com - in the end I stored the facebook id as a string by concatenating it with “F”.

Hi Jeremy, Nick,

Thanks for raising and looking at this.  Would you mind clarifying a bit more what you’ve been experiencing?  I understand the risk that a large number could be cast as a string in an unusable scientific notation.  But from what I can tell, the JSON responses from Facebook always contain user IDs as strings (within quotes), not as numbers.  So it’s not clear to me how/why this issue is arising.

Here’s a simple test case.  It doesn’t actually use the Facebook API, just works with a JSON in the format that the Facebook API would return.

[lua]

local json = require(“json”)

– Simulated response from a request to the Facebook API

local response = [[

{

    “data”: [

        {

          “name”: “User Name”, 

          “id”: “123456789123456789”

        }

    ]

}

]]

local results = json.decode(response)

print(results.data[1].id)     – Prints the full string

print(json.encode(results))    – Re-encoded as JSON (e.g., if saving to a file), the id is the full string

[/lua]

Are you seeing situations where the JSON from Facebook is not as above, but instead is providing the ID as a number?

  • Andrew

That is a very clever idea Nick.  Thanks for the help!

Andrew,

I can’t tell exactly where the id is being interpreted as a number.  Concatenating a character on the front of the id, in the FQL query, seems to fix things.

Interesting.  It does look like it’s an FQL issue.  Using the Graph API Explorer (https://developers.facebook.com/tools/explorer), I ran some simple FQL queries.  The query “SELECT name FROM user WHERE uid = me()” returns the id as a number, but the query “SELECT uid2 FROM friend WHERE uid1 = me();” returns the ids as strings (consistent with the documentation at https://developers.facebook.com/docs/reference/fql/friend which says uid1 and uid2 are numeric strings).

  • Andrew

Andrew,

When issuing a FQL query, you can use the parameter format=json-strings, to force the results to all be string values.  I’ve tested this in the browser and it behaves as expected.  When I use that with the Corona facebook framework, it either gets dropped or over written.

Jeremy,

Just thowing this out there… Does:

tostring( id )

Help at all?

Thanks Danny but tostring just turns the number 1.0000130523746e+14, into the string “1.0000130523746e+14”.  This seems to be a problem with representing a number that is larger than what a 32 bit field can hold.