13

I am using JSON column type in MySQL database table. When I try to save JSON values in table column, the JSON array automatically re-order(shuffle)

I have following JSON:

{"TIMER_HEADER": [{"XX!TIMERHDR": "XXTIMERHDR", "VER": " 7", "REL": " 0", "COMPANYNAME": "XXX", "IMPORTEDBEFORE": "N", "FROMTIMER": "N", "COMPANYCREATETIME": "12423426"}, {"XX!HDR": "XXHDR", "PROD": "Qics for Wnows", "VER": "Version 6.0", "REL": "Release R", "IIFVER": "1", "DATE": "2018-01-20", "TIME": "1516520267",   "ACCNTNT": "N", "ACCNTNTSPLITTIME": "0"}], "COLUMN_HEADER": ["!TIMEACT", "DATE", "JOB", "EMP", "ITEM", "PITEM", "DURATION", "PROJ", "NOTE", "BILLINGSTATUS"]}

After saving in JSON-column of MySql table, this becomes:

{"TIMER_HEADER": [{"REL": " 0", "VER": " 7", "FROMTIMER": "N", "COMPANYNAME": "XXX", "XX!TIMERHDR": "XXTIMERHDR", "IMPORTEDBEFORE": "N", "COMPANYCREATETIME": "12423426"}, {"REL": "Release R", "VER": "Version 6.0", "DATE": "2018-01-20", "PROD": "Qics for Wnows", "TIME": "1516520267", "IIFVER": "1", "XX!HDR": "XXHDR", "ACCNTNT": "N", "ACCNTNTSPLITTIME": "0"}], "COLUMN_HEADER": ["!TIMEACT", "DATE", "JOB", "EMP", "ITEM", "PITEM", "DURATION", "PROJ", "NOTE", "BILLINGSTATUS"]}

I need the same order as I have original, because there is an validation on 3rd party.

Please help. Thanks.

Rahul Dadhich
  • 1,213
  • 19
  • 32
  • maybe this answer will help somebody [https://stackoverflow.com/a/61519255/10579735](https://stackoverflow.com/a/61519255/10579735) – Oleksandr Hrin Apr 19 '21 at 12:35

2 Answers2

15

The items of arrays don't change order in MySQL JSON columns, but object key/value pairs might.

MySQL manual says:

To make lookups more efficient, it also sorts the keys of a JSON object. You should be aware that the result of this ordering is subject to change and not guaranteed to be consistent across releases.

There is no standard that says it has to be in a certain order, so probably the best thing to do is to speak to someone on 3rd party to adjust validation.

Matt Kieran
  • 4,174
  • 1
  • 17
  • 17
kiks73
  • 3,718
  • 3
  • 25
  • 52
0

We are using blob for storage. JSON validation is handled by our application.

Using this blob column, keys are not sorted since it not optimized for JSON.

Please refer the blob docs for details. https://dev.mysql.com/doc/refman/8.0/en/blob.html

marlo
  • 6,998
  • 5
  • 28
  • 34