Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between JSONField and ArrayField

The following case

[{type: x, val: y}, {...}, {...}, ...]

can be represented by JSONField alone.

Also, the following case

[1,2,3,4,5]

can be represented by JSONField.

Then what is the point of using ArrayField?

Am I correct to assume JSONField covers all the cases of ArrayField?

like image 816
innhyu Avatar asked Mar 13 '20 00:03

innhyu


1 Answers

No, you're not correct in that assumption. You can make PostgreSQL arrays of any type that PostgreSQL supports but JSON only supports strings, integers, floating point numbers, booleans, JavaScript-style objects, and arrays.

For example, you can create an array of timestamps or dates but to store those types in JSON you end converting them to strings (or sometimes numbers) and store those strings. Then, if you need to use the time functions or operators, you'd have to dig the strings out of the JSON arrays, convert them into timestamps or dates, and finally apply the functions or operators.

Similarly for all the other data types that PostgreSQL understands that JSON doesn't.

The array functions and operators and JSON (array) functions and operators are comparable but the data types are not.

like image 78
mu is too short Avatar answered Sep 21 '22 19:09

mu is too short