2

Is there a function in mysql to sort a json field?

SELECT new FROM mytable
["orange", "apple"]

==> Sorting the json array

SELECT SORTED(new) FROM mytable
["apple", "orange"]
David542
  • 104,438
  • 178
  • 489
  • 842
  • I don't think so. All the JSON functions are listed [here](https://dev.mysql.com/doc/refman/8.0/en/json-functions.html) – Barmar Dec 08 '18 at 00:14
  • I see lots of questions from you about MySQL JSON. Just because this is available, I don't think you should overuse it. It would be better if you normalized your schema using traditional SQL design. – Barmar Dec 08 '18 at 00:16
  • @Barmar right, yea this is a denormalized field of another table that we use on a `ListAll()` call, so we're storing a denormalized json field to improve read performance, etc... – David542 Dec 08 '18 at 00:19
  • @Barmar do you think there would a way to do this by perhaps unnesting the array and then nesting it again? – David542 Dec 08 '18 at 00:56
  • 1
    I don't know a way of unnesting the array in MySQL. Don't use JSON for this kind of thing. If it were in a normalized table you could use `ORDER BY`. – Barmar Dec 08 '18 at 01:00
  • It's not MySQL, but Couchbase N1QL has such a function, ARRAY_SORT(); https://docs.couchbase.com/server/6.0/n1ql/n1ql-language-reference/arrayfun.html#fn-array-sort – Johan Larson Dec 08 '18 at 01:40

0 Answers0