Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent Json ordering while inserting into Mysql table

Tags:

json

mysql

The reordering of Json-Content is already an known issue while sending AJAX Request, but I wasn't aware that the same happens while inserting Json-content into an Mysql Table with JSON type.

In this case the mysql server also orders its content before saving.

before:

{"c":3, "b":2, "a":1}

after:

{"a":1, "b":2, "c":3}

Suppose I would like to prevent this ordering for some purpose... Is there any possibility to disable the ordering without adapting the Json data itself?

like image 984
Christian Felix Avatar asked Sep 08 '25 15:09

Christian Felix


1 Answers

The key to this is to serialize this > then store it > retrieve > then parse

For example: First store object as a string

var jsonDataStringed = JSON.stringify(myJsonData);
INSERT INTO MY_TABLE(id,jsonDataStringed);

then to retrieve the string and change it back to object

db.sequelize.query(`SELECT * FROM MY_TABLE`);
var myJsonData = JSON.parse(db[0])
like image 171
Michael Nelles Avatar answered Sep 10 '25 08:09

Michael Nelles