Mysql 5.7 native json support - control keys order in json_insert function

后端 未结 2 536
名媛妹妹
名媛妹妹 2021-01-12 10:39

I am facing issues with json key ordering while inserting using json_insert function.

I have the sorted records as below

\"AIR\", \"AIR Express\"

\"

相关标签:
2条回答
  • 2021-01-12 10:57

    That's sad, but at now is no way to keep original keys order.

    Normalization, Merging, and Autowrapping of JSON Values

    The normalization performed by MySQL also sorts the keys of a JSON object (for the purpose of making lookups more efficient). The result of this ordering is subject to change and not guaranteed to be consistent across releases. In addition, extra whitespace between keys, values, or elements in the original document is discarded.

    0 讨论(0)
  • 2021-01-12 11:08

    i had the same problem, the right way to solve this problem i found by talking with the community,is to adding an extra value for each key on the object,it is the most right way. The shorter example based on your code, you can do

    {"1:FH":"Free house", "2:UN": "United Nations", "3:AIR": "AIR Express", "4:CFR": "Cost and Freight", "5:UPS": "United Parcel Service"}

    Then by these extra values, you sort the positions of the keys, you can do this in many other ways, if you or someone else are interested in this way of key sortering, let me know, and i will show how, on JS, or PHP. Cheers!

    0 讨论(0)
提交回复
热议问题