surajjd's avatar

Json column is auto sorted (which I dont want)

I am saving JSON values in the database column.

{
    "24-08-2023": 1,
    "25-08-2023": 1,
    "26-08-2023": 0,
    "27-08-2023": 0,
    "28-08-2023": 1,
    "29-08-2023": 1,
    "30-08-2023": 1,
    "31-08-2023": 1,
    "01-09-2023": 2,
    "02-09-2023": 0,
    "03-09-2023": 0,
    "04-09-2023": 2,
    "05-09-2023": 2,
    "06-09-2023": 2,
    "07-09-2023": 2,
    "08-09-2023": 2,
    "09-09-2023": 0,
    "10-09-2023": 0,
    "11-09-2023": 3,
    "12-09-2023": 4,
    "13-09-2023": 4,
    "14-09-2023": 4,
    "15-09-2023": 6,
    "16-09-2023": 0,
    "17-09-2023": 0,
    "18-09-2023": 5,
    "19-09-2023": 6,
    "20-09-2023": 6,
    "21-09-2023": 6,
    "22-09-2023": 9,
    "23-09-2023": 0,
    "24-09-2023": 0,
    "25-09-2023": 8,
    "26-09-2023": 8,
    "27-09-2023": 9,
    "28-09-2023": 9,
    "29-09-2023": 9,
    "30-09-2023": 0,
    "01-10-2023": 0,
    "02-10-2023": 9,
    "03-10-2023": 10,
    "04-10-2023": 10
}

But Whenever it gets saved it automatically sorts which first section(day) is ascending order here is what gets saved

{
    "01-09-2023": 2,
    "01-10-2023": 0,
    "02-09-2023": 0,
    "02-10-2023": 9,
    "03-09-2023": 0,
    "03-10-2023": 10,
    "04-09-2023": 2,
    "04-10-2023": 10,
    "05-09-2023": 2,
    "06-09-2023": 2,
    "07-09-2023": 3,
    "08-09-2023": 3,
    "09-09-2023": 0,
    "10-09-2023": 0,
    "11-09-2023": 3,
    "12-09-2023": 4,
    "13-09-2023": 4,
    "14-09-2023": 4,
    "15-09-2023": 4,
    "16-09-2023": 0,
    "17-09-2023": 0,
    "18-09-2023": 5,
    "19-09-2023": 7,
    "20-09-2023": 7,
    "21-09-2023": 5,
    "22-09-2023": 9,
    "23-09-2023": 0,
    "24-08-2023": 1,
    "24-09-2023": 0,
    "25-08-2023": 1,
    "25-09-2023": 9,
    "26-08-2023": 0,
    "26-09-2023": 9,
    "27-08-2023": 0,
    "27-09-2023": 8,
    "28-08-2023": 1,
    "28-09-2023": 10,
    "29-08-2023": 1,
    "29-09-2023": 9,
    "30-08-2023": 1,
    "30-09-2023": 0,
    "31-08-2023": 1
}

If this would help I am using Mysql and json column and array as a cast for column on the model

0 likes
5 replies
JussiMannisto's avatar

Properties in JSON are unordered, so you shouldn't expect software to preserve the order. If you must preserve order, you should use some other data structure.

The order of elements in an array is preserved. You might, for example, use an array of items that have this structure:

{
"date": "24-08-2023",
"count": 1
}
surajjd's avatar

@JussiMannisto Ok thanks But I am little confused here. if JSON are unordered and If Sure values which I am generating are correct. then how Laravel or mysql ordering it while saving in db.

JussiMannisto's avatar

@surajjd The standard defines JSON object as an unordered collection of keys an values. Any software that conforms to the standard doesn't have to preserve the original order. The final order might depend on how the data is stored in memory, or it might be explicitly sorted.

The takeaway is: you shouldn't rely on JSON properties having a specific order. The order may change anywhere along the way where it's serialized or deserialized.

Tray2's avatar

First off, never ever store data as json, Second whatever MySQL does to it when it writes or reads it to disk is something MySQL does, you can't really do anything about it. You are obviously storing a date and a value, that is perfect for storing in two columns in your table. Storing them like real columns, you can index them as you please.

I suggest that you give this post a read.

https://tray2.se/posts/database-design

1 like
surajjd's avatar

@Tray2 Thanks for Reply. Yes, I changed the JSON column to text. and Precalculated is just an attribute value in the Model. so, can't change that to the two different columns.

Please or to participate in this conversation.