MaxHalford / maxhalford.github.io

:house_with_garden: Personal website
https://maxhalford.github.io
MIT License
12 stars 5 forks source link

blog/no-dynamic-keys-in-json/ #34

Open utterances-bot opened 5 months ago

utterances-bot commented 5 months ago

For analytics, don't use dynamic JSON keys • Max Halford

I love the JSON format. It’s the kind of love that grows on you with time. Like others, I’ve been using JSON everywhere for so many years, to the point where I just take it for granted. I suppose the main thing I like about JSON is its flexibility. You can structure your JSONs without too much care. There will always be a way to consume and manipulate it. But I have discovered a bit of anti-pattern, which I believe is worth raising awareness about.

https://maxhalford.github.io/blog/no-dynamic-keys-in-json/

rchampourlier commented 5 months ago

Hey Max!

Thanks for sharing, you're making it super clear why it's not ideal to use such JSON structure when working in BigQuery and doing analytics.

What about dictionary structures with non-dynamic keys.

For example in Typescript:

type RecordWithStaticKeys = Record<'Key1'|'Key2', value: any>

instead of:

type RecordWithDynamicKeys = Record<string, value: any>

?

Does it cause the same problem? Maybe it does if you want to simplify your SQL to iterate over the different static keys?

Which means the problem is not only with dynamic keys then 🤔

MaxHalford commented 5 months ago

Hey @rchampourlier! Yes, that works. The only downside is that the database issuing the queries needs to have the list of static keys. That's not difficult to setup, but it means you might end up with two sources of truth.