It seems that generally, Mathematica users (or at least the Q&A I've read here) prefer associations to represent data in ways that many other languages would use structures.
However, when Importing JSON files, Mathematica generates a list of rules.
Is there any clear advantage one way or the other? Is it just syntactical? Is there a reason Mathematica uses lists of rules for JSON instead of associations?
key0 -> value
for the samekey0
, e.g.{a -> x, b -> y, b -> z}
. But if you convert this into an association, only the last key-value pair for a given key will be kept:Association[{a -> x, b -> y, b -> z}]
gives<|a -> x, b -> z|>
. I don't know, but I imagine this might be one reason to maintain a difference. $\endgroup$Association
is newer and has several advantages, which are discussed elsewhere on this site. Since it’s newer, old functionality that returned lists may still do so, for the sake of compatibility. They work differently, sincepattern -> replacement
has a different intention thankey -> value
. ThusReverse /@ {x -> a, y -> b}
inverts the mapping represented by the list; mapping any function (likeReverse
) on an association applies the function to each value. Etc. The different semantics make each useful for different applications.Association
is intended to represent key/value data. $\endgroup$