Search

7/20/2012

Micha’s Golden Rule

Micha’s Golden Rule

Micha Gorelick, a data scientist in NYC, coined the following rule: Do not store data in the keys of a JSON blob. This is Micha’s Golden Rule; it should always be followed when forming JSON for use in D3, and will save you many confusing hours. This means that one should never form JSON like the following:
{
"bob": 20,
"alice": 23,
"drew": 30
}
Here we are storing data in both the key (name) and the value (age). This is perfectly valid JSON, but breaks Micha’s Golden Rule, which is bad. Instead, we would form the following as a list:
[{
    "name": "bob",
    "age": 20
}, {
    "name": "alice",
    "age": 23
}, {
    "name": "drew",
    "age": 30
}]
Here, we store data only in the values, and the keys indicate only what the data repre- sents. If you are in the unfortunate position of having JSON that breaks Micha’s Golden Rule, consider using d3.entries() to make the conversion.
via: O'Reilly Getting Start with D3

沒有留言: