innhyu
innhyu

Reputation: 360

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?

Upvotes: 8

Views: 2144

Answers (1)

mu is too short
mu is too short

Reputation: 434606

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.

Upvotes: 14

Related Questions