Post ASOKmthk2osi8d4kfA by danmayer@ruby.social
 (DIR) More posts by danmayer@ruby.social
 (DIR) Post #ASOG2WquHCmu7rzrZg by simon@fedi.simonwillison.net
       2023-02-06T03:32:33Z
       
       0 likes, 0 repeats
       
       Beginning to notice that almost every time I design a JSON format that includes a {"key1": {"...": "..."}, "key2": {"...": "..."}} structure I eventually realize it would have worked better as [{"id": "key1", "..."}, {"id": "key2", "..."}]
       
 (DIR) Post #ASOGDmn9M2WQkLbicq by stefan@gardenstate.social
       2023-02-06T03:33:20Z
       
       0 likes, 0 repeats
       
       @simon arrays are magic.
       
 (DIR) Post #ASOGevrbjX64hXdXjU by MrCheeze@wetdry.world
       2023-02-06T03:35:44Z
       
       0 likes, 0 repeats
       
       @simon {"key1": {"id": "key1", ...}} might feel wrong but is nice to use in practice.
       
 (DIR) Post #ASOGpzjrfMVpuBI2u8 by mjgardner@social.sdf.org
       2023-02-06T03:36:25Z
       
       0 likes, 0 repeats
       
       @simon You’re rediscovering the natural keys vs. surrogate keys database debate
       
 (DIR) Post #ASOH48adYREHPWiYmO by kellogh@hachyderm.io
       2023-02-06T03:39:06Z
       
       0 likes, 0 repeats
       
       @simon what tools are you using that the array works better?
       
 (DIR) Post #ASOHHf0UL9phiNG2Pw by ernesto@async.social
       2023-02-06T03:42:48Z
       
       0 likes, 0 repeats
       
       @simon isn’t it better the first format if keys are meant to be unique? And if you need to find entries by key?
       
 (DIR) Post #ASOHV76LCI3wc2PUKO by simon@fedi.simonwillison.net
       2023-02-06T03:45:34Z
       
       0 likes, 0 repeats
       
       @ernesto That's what I always think... and then when I actually access the API I inevitably find a reason to write code that turns it back into an array again
       
 (DIR) Post #ASOHhLQlkKnGWtnVPE by pearofdoom@cloudisland.nz
       2023-02-06T03:45:40Z
       
       0 likes, 0 repeats
       
       @simon is that a csv?
       
 (DIR) Post #ASOHv6PWO5dK02m3O4 by simon@fedi.simonwillison.net
       2023-02-06T03:49:12Z
       
       0 likes, 0 repeats
       
       Here's the initial issue that brought this problem to mind: https://github.com/simonw/datasette/issues/1558Then I thought back on my interactions with this kind of API and realized that I very often end up writing code to turn the key/object structures into an array of objects-with-keys instead when I actually want to render them or process them in some wayOptimizing for lookup-by-key turns out not to be as useful as I always expect it to be
       
 (DIR) Post #ASOI6dS3slVPeySg76 by somelinguist@mastodon.social
       2023-02-06T03:52:00Z
       
       0 likes, 0 repeats
       
       @simon This happened to me just the other day. I wrote the format, then went to write the code that would use the format, and after a few lines… What was I thinking? It obviously works better as an array. One of these days.
       
 (DIR) Post #ASOIPm0SIfiZh5ftNg by onelson@mastodon.social
       2023-02-06T03:59:13Z
       
       0 likes, 0 repeats
       
       @simon I suppose "it depends" on the specifics, as usual, but I agree and disagree at the same time 🤣I like "structures that make the representation of invalid states impossible" and so on, so key/value makes good sense for this. If the code in question isn't responsible for controlling and ensuring one record per id, then a plain array of records is often more useful for a variety of cases.
       
 (DIR) Post #ASOKmthk2osi8d4kfA by danmayer@ruby.social
       2023-02-06T04:25:36Z
       
       0 likes, 0 repeats
       
       @simon I do too, but it mostly feels like that is because all the built-in language transformations for array's are simpler and easier to chain than hash transforms. Not sure if that is really true, but I know I have had this gut reaction when creating API responses a few times in recent weeks.
       
 (DIR) Post #ASOOYDzIkWN6WCY2Pw by josephholsten@mstdn.social
       2023-02-06T05:07:58Z
       
       0 likes, 0 repeats
       
       @simon hey, you’ve discovered how terraform transforms all JSON input into its AST! Also, this is why we can’t round trip serialize JSON terraform config.
       
 (DIR) Post #ASOdHMRpXYEx99JDsW by ulf@mastodon.online
       2023-02-06T07:52:46Z
       
       0 likes, 0 repeats
       
       @simon i gues it depends if "id" is your main search index. if you only search for one specific keyres = arr[key]   # gimme all data ...if you search for specific properties it's better to use list of dictres = [item.get(prop) for item in arr]
       
 (DIR) Post #ASP512GJcUlecnvWUq by hynek@mastodon.social
       2023-02-06T13:03:53Z
       
       0 likes, 0 repeats
       
       @simon JSON:API has been preaching that for years :)
       
 (DIR) Post #ASPAMpEpDtVINCRIe0 by masukomi@connectified.com
       2023-02-06T14:03:34Z
       
       0 likes, 0 repeats
       
       @simon  🤔is it just that you end up wanting summarized info more often than specific data points?
       
 (DIR) Post #ASPAZm7q6i2oU4LYjw by tintvrtkovic@mastodon.social
       2023-02-06T14:03:34Z
       
       0 likes, 0 repeats
       
       @simon This irks me on account of making it technically possible to have the same key twice in the array. Although I see your point, for example Mongo has much richer operators for arrays than it has for object keys.
       
 (DIR) Post #ASPE6elkmJ1p5VO2wS by simon@fedi.simonwillison.net
       2023-02-06T14:45:32Z
       
       0 likes, 0 repeats
       
       @masukomi I inevitably want to loop through it in something that provides a stable order
       
 (DIR) Post #ASPFTl0NrJdd2ioEEq by BlindMansBinary@hachyderm.io
       2023-02-06T15:00:28Z
       
       0 likes, 0 repeats
       
       @simon I think I run into this everytime I try to do something serious with jq.
       
 (DIR) Post #ASPKBcq7yjlEHDBYbg by jonathan_s@fosstodon.org
       2023-02-06T15:52:27Z
       
       0 likes, 0 repeats
       
       @simon Yes, I've been doing that as well. As an API response, it doesn't have to transfer a "search index", but only the data. "id" is a property you want within the object, and in the client too often you want to search on other fields too, so transferring a mapping too often isn't helpful.
       
 (DIR) Post #ASPUNvRTdkmMzI9vQO by adam@ruby.social
       2023-02-06T17:47:41Z
       
       0 likes, 0 repeats
       
       @simon in the Rails world there’s a joke to never use ‘has_one’ unless you know for 100% certain there’s no possible way you’ll get ever need ‘has_many’.Even then I’ve still made the mistake. 😂
       
 (DIR) Post #ASPUmHX1AEQWRkMfhY by glyph@mastodon.social
       2023-02-06T17:52:23Z
       
       0 likes, 0 repeats
       
       @simon same. JSON *looks* like it has maps, but it actually only has structs
       
 (DIR) Post #ASQXg1yQaaFVTfr9ou by trentm@hachyderm.io
       2023-02-07T05:59:04Z
       
       0 likes, 0 repeats
       
       @simon yes. Always this.