Take the 2-minute tour ×
Stack Apps is a question and answer site for apps, scripts, and development with the Stack Exchange API. It's 100% free, no registration required.

in order to uniquely identify a associated_users on must traverse the object graph into the onsite object and construct an artificial key.

it makes more sense to either flatten the structure or, more appropriately in my opinion, simply use a site key instead of duplicating data that, if desired, can be pulled from the /sites route.

this is an example of where a decent site key would come in handy, but if, as insisted, a mixed case string with, currently, spaces and who knows what else in the future, is what we have for a key then please surface that on the result.

the current structure is unnatural and the nested object graph complicates consumption unnecessarily.

Current:

{
  "associated_users": [
    {
      "user_id": 242897,
      "user_type": "registered",
      "display_name": "code poet",
      "reputation": 9272,
      "on_site": {
        "name": "Stack Overflow",
        "logo_url": "http://sstatic.net/so/img/logo.png",
        "api_endpoint": "http://api.stackoverflow.com",
        "site_url": "http://stackoverflow.com",
        "description": "Q&A for programmers",
        "icon_url": "http://sstatic.net/so/apple-touch-icon.png"
      },
      "email_hash": "df4a7fbd8a054fd6193ca0ee62952f1f"
    },
    {
      "user_id": 38306,
      "user_type": "registered",
      "display_name": "code poet",
      "reputation": 111,
      "on_site": {
        "name": "Server Fault",
        "logo_url": "http://sstatic.net/sf/img/logo.png",
        "api_endpoint": "http://api.serverfault.com",
        "site_url": "http://serverfault.com",
        "description": "Q&A for system administrators and IT professionals",
        "icon_url": "http://sstatic.net/sf/apple-touch-icon.png"
      },
      "email_hash": "df4a7fbd8a054fd6193ca0ee62952f1f"
    },
    {
      "user_id": 31747,
      "user_type": "registered",
      "display_name": "code poet",
      "reputation": 100,
      "on_site": {
        "name": "Super User",
        "logo_url": "http://sstatic.net/su/img/logo.png",
        "api_endpoint": "http://api.superuser.com",
        "site_url": "http://superuser.com",
        "description": "Q&A for computer enthusiasts and power users",
        "icon_url": "http://sstatic.net/su/apple-touch-icon.png"
      },
      "email_hash": "df4a7fbd8a054fd6193ca0ee62952f1f"
    },
    {
      "user_id": 144906,
      "user_type": "registered",
      "display_name": "code poet",
      "reputation": 1252,
      "on_site": {
        "name": "Stack Overflow Meta",
        "logo_url": "http://sstatic.net/mso/img/logo.png",
        "api_endpoint": "http://api.meta.stackoverflow.com",
        "site_url": "http://meta.stackoverflow.com",
        "description": "Q&A about Stack Overflow, Server Fault, and Super User",
        "icon_url": "http://sstatic.net/mso/apple-touch-icon.png"
      },
      "email_hash": "df4a7fbd8a054fd6193ca0ee62952f1f"
    },
    // stackapss  missing
    {
      "user_id": 14,
      "user_type": "registered",
      "display_name": "code poet",
      "reputation": 3123,
      "email_hash": "df4a7fbd8a054fd6193ca0ee62952f1f"
    }
  ]
}

Proposed: (i would be happy to meet you half way and tolerate the nested on_site node if only we can get the site id on the root as shown)

{
  "associated_users": [
    {
      "user_id": 242897,
      "user_type": "registered",
      "display_name": "code poet",
      "reputation": 9272,
      "site_name": "Stack Overflow",
      "email_hash": "df4a7fbd8a054fd6193ca0ee62952f1f"
    },
    {
      "user_id": 38306,
      "user_type": "registered",
      "display_name": "code poet",
      "reputation": 111,
      "site_name": "Server Fault",
      "email_hash": "df4a7fbd8a054fd6193ca0ee62952f1f"
    },
    {
      "user_id": 31747,
      "user_type": "registered",
      "display_name": "code poet",
      "reputation": 100,
      "site_name": "Super User",
      "email_hash": "df4a7fbd8a054fd6193ca0ee62952f1f"
    },
    {
      "user_id": 144906,
      "user_type": "registered",
      "display_name": "code poet",
      "reputation": 1252,
      "site_name": "Stack Overflow Meta",
      "email_hash": "df4a7fbd8a054fd6193ca0ee62952f1f"
    },
    {
      "user_id": 14,
      "user_type": "registered",
      "display_name": "code poet",
      "reputation": 3123,
      "email_hash": "df4a7fbd8a054fd6193ca0ee62952f1f"
    }
  ]
}
share|improve this question

1 Answer 1

You're de-serializing on_site, just define an equality operator - however that is done on your platform of choice - and be done with it.

Bam, unique key.


We're returning the site data to save a query.

Consider, it will probably be a very common use case for /users/{id}/associated will be:

  • query associated accounts given 1 site/id pair
  • get some subset of user activity across all sites the user is active on

If we didn't return the full site data, you'd either have to make a /sites call (wasted query, and extra wait time for the user) or implement a caching scheme*. Since we already heavily cache the sites data, it costs us nothing to return the relevant bits of it.

*Not that there's anything wrong with caching, but if you have to implement it for a trivial use case something's a bit dicey.

share|improve this answer
1  
as i said, i can see how the nested site structure could be an illustration of acceptable denormalization. But what you seem reluctant to acknowledge in this and other cases is that your denormalization does not simplify the task of consuming the data. You keep suggesting that we should make efforts to manipulate the data and the structures we create to arrive at what would be seen as a usable result when the inclusion of one simple field of data, that you already have in hand, in the root of the structure in question would allow direct deserialization into a usable object. –  Sky Sanders Jun 18 '10 at 14:46
1  
I don't want to have to construct artificial structures and hook the deserialization process with extra measures. I want the data being returned from the api be shaped in a way that is usable as-is. And if that can be done with the inclusion of a field here or there, why not do it? It is not like I am suggesting some outrageous smelly kludge, I am begging for a simple small degree of normalization. –  Sky Sanders Jun 18 '10 at 14:50
1  
@code poet - You're keying data, it isn't a horrible kludge to have to construct the keys yourself. Asking for 100% duplicated data in a return to simply your - and, probably, only your - code is a bit much. Just reach into the on_site object if you have to. I still think defining an equality operator is the most elegant solution. –  Kevin Montrose Jun 18 '10 at 14:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .