When consuming the API using JSON, it is important to always use the field id_str instead of id. This is due to the way Javascript and other languages that consume JSON evaluate large integers. If you come across a scenario where it doesn't appear that id and id_str match, it's due to your environment having already parsed the id integer, munging the number in the process. Read below for more information on how Twitter generates its ids.
In addition to Tweets being given snowflake-based IDs, direct messages will also be snowflake 64-bit integers beginning September 30th, 2011.
So what is Snowflake?
Snowflake is a service we will be using to generate unique Tweet IDs. These Tweet IDs are unique 64bit unsigned integers, which, instead of being sequential like the current IDs, are based on time. The full ID is composed of a timestamp, a worker number, and a sequence number.
The problem
Before launch it came to our attention that some programming languages such as Javascript cannot support numbers with >53bits. This can be easily examined by running a command similar to: (90071992547409921).toString() in your browsers console or by running the following JSON snippet through your JSON parser.
{"id": 10765432100123456789, "id_str": "10765432100123456789"}
In affected JSON parsers the ID will not be converted successfully and will lose accuracy. In some parsers there may even be an exception.
The solution
To allow javascript and JSON parsers to read the IDs we need to include a string version of any ID when responding in the JSON format. What this means is Status, User, Direct Message and Saved Search IDs in the Twitter API will now be returned as an integer and a string in JSON responses. This will apply to the main Twitter API, the Streaming API and the Search API.
For example, a status object will now contain an id and an id_str. The following JSON representation of a status object shows the two versions of the ID fields for each data point.
[
{
"coordinates": null,
"truncated": false,
"created_at": "Thu Oct 14 22:20:15 +0000 2010",
"favorited": false,
"entities": {
"urls": [
],
"hashtags": [
],
"user_mentions": [
{
"name": "Matt Harris",
"id": 777925,
"id_str": "777925",
"indices": [
0,
14
],
"screen_name": "themattharris"
}
]
},
"text": "@themattharris hey how are things?",
"annotations": null,
"contributors": [
{
"id": 819797,
"id_str": "819797",
"screen_name": "episod"
}
],
"id": 12738165059,
"id_str": "12738165059",
"retweet_count": 0,
"geo": null,
"retweeted": false,
"in_reply_to_user_id": 777925,
"in_reply_to_user_id_str": "777925",
"in_reply_to_screen_name": "themattharris",
"user": {
"id": 6253282,
"id_str": "6253282"
},
"source": "web",
"place": null,
"in_reply_to_status_id": 12738040524,
"in_reply_to_status_id_str": "12738040524"
}
]
What should you do - RIGHT NOW
The first thing you should do is attempt to decode the JSON snippet above using your production code parser. Observe the output to confirm the ID has not lost accuracy.
What you do next depends on what happens:
- If your code converts the ID successfully without losing accuracy you are OK but should consider converting to the _str versions of IDs as soon as possible.
- If your code has lost accuracy, convert your code to using the _str version immediately. If you do not do this your code will be unable to interact with the Twitter API reliably.
- In some language parsers, the JSON may throw an exception when reading the ID value. If this happens in your parser you will need to ‘pre-parse’ the data, removing or replacing ID parameters with their _str versions.
Summary
- If you develop in Javascript, know that you will have to update your code to read the string version instead of the integer version.
- If you use a JSON decoder, validate that the example JSON, above, decodes without throwing exceptions. If exceptions are thrown, you will need to pre-parse the data. Please let us know the name, version, and language of the parser which throws the exception so we can investigate.