directus / directus

The flexible backend for all your projects 🐰 Turn your DB into a headless CMS, admin panels, or apps with a custom UI, instant APIs, auth & more.
https://directus.io
Other
28.28k stars 3.94k forks source link

JSON field type sort the content of the field #23003

Open rcarcasson opened 4 months ago

rcarcasson commented 4 months ago

Describe the Bug

I have a collection with a JSON field type on it with a simple structure:

image

The issue: I want a specific order to each element, but it doesn't matter where i put the element, every time I save the record Directus sort the elements by each text length:

I want: image

But Directus sort the element: image

How I can avoid this behaviour?

To Reproduce

Create a JSON field type on any collection as JSON type

Directus Version

v10.6.3

Hosting Strategy

Self-Hosted (Docker Image)

Database

No response

rijkvanzanten commented 4 months ago

Any reason not to use an Array instead of an Object for ordered values? Property order in javascript objects isn't generally guaranteed

rcarcasson commented 4 months ago

I don't know why it was implemented using an object, but it has worked like this for years. What we want is for it not to sort the object when any modification is saved. Changing the object to an array would mean modifying the front that uses this information and modifying the entire structure of the collections that use this current format

rijkvanzanten commented 4 months ago

I'm assuming it's caused by the fact that the JSON interface uses JSON.stringify() to format the values when the form is rendered, which combined with JS' non-guaranteed order causes it to be resorted.

rcarcasson commented 4 months ago

I tried to figure out why this happen without results so finally I change the format of the data to an array with the object on it and it keeps the elements without sort them

image

These help me to keep the order of the elements