5 ms·
Since you're planning on doing some conceptual work with JSONB, just a heads upon one gotcha - duplicate properties are not allowed. I.E.: { task: "do stuff
by qooleot 12y ago
Since you're planning on doing some conceptual work with JSONB, just a heads upon one gotcha -
duplicate properties are not allowed. I.E.:
{
task: "do stuff",
task: "do other stuff"
}
which sometimes is useful when you have front-end data with an N-number of entries but its a form that serializes to an object instead of an array. There are other use cases too.
- masklinn 12y agoRFC4627 §2.2 ¶1 > The names within an object SHOULD be unique. RFC2119 §3 > [SHOULD], or the adjective "RECOMMENDED", mean that there may exist valid reasons in particular circumstances to ignore a particular item, but the full implications must be understood and carefully weighed before choosing a different course. Not allowing duplicate keys in JSON objects is very close the exact opposite of a gotcha. Allowing and round-tripping them would be a gotcha.
- tracker1 12y agoMost implementations that allow them, from those I've seen only take the last (in actual order) value...
- qooleot 12y agoNo, it can be a gotcha because the json column type (which still exists and many may mistakenly use instead of the jsonb type) allows it: select '{"foo": 1, "foo": 2}'::json; json ---------------------- {"foo": 1, "foo": 2} (1 row) select '{"foo": 1, "foo": 2}'::jsonb; jsonb ------------ {"foo": 2} (1 row) So technically, the gotcha is you have to remember to use jsonb instead of json, as well as json having a true "gotcha" and jsonb not having one.
- ak4g 12y agothe json type just stores the string value passed to it, but only the final value is available to the JSON-aware operators. mydb=# select ('{"foo": 1, "foo": 2}'::jsonb)->'foo' jsonb_foo, ('{"foo": 1, "foo": 2}'::json)->'foo' json_foo; jsonb_foo|2 json_foo|2
- harel 12y agoI would have been worried if it was allowed. This is exactly what arrays are for.
- jackmaney 12y agoOkay, I'll bite. What, exactly, do you think should happen if you have an object of the form { task: "do stuff", task: "do other stuff" } ? Objects in JSON are key-value pairs. A single key goes to a single value. Instead, you should map task to an array of values.
- mjevans 12y agoNo, you should have written an array of values to begin with. Coercing data types is surely more wrong than taking the most recently supplied value for a given key in a map.
- mc808 12y agoUm, I think you're agreeing. "Instead, you [i.e., not the deserializer] should map task to an array of values."