Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON fields are ordered differently when exceeding 8 fields per object #10699

Closed
kamal-choudhary opened this issue Aug 6, 2021 · 18 comments
Closed
Labels
issue: bug Issue reporting a bug severity: low If the issue only affects a very niche base of users and an easily implemented workaround can solve source: core:content-manager Source is core/content-manager package status: confirmed Confirmed by a Strapi Team member or multiple community members

Comments

@kamal-choudhary
Copy link

Bug report

Describe the bug

I have a JSON data field in one of my content types. It works perfectly fine until the number of fields inside the JSON object is either 8 or less than 8. As soon as I introduce field number 9, the order of JSON fields gets changed.

Steps to reproduce the behavior

  1. Add a JSON data field in your content type
  2. Go to your content type and create an entry
  3. Add some valid JSON for the JSON field, it works fine until the number of fields inside the JSON object is either 8 or less than 8
  4. Added field number 9 and the ordering of fields will get changed.

Expected behavior

The ordering of fields should not change. Items should be ordered as entered.

Code snippets

Let’s say I have a Person type JSON-LD object and I add only 8 fields in this format:

{
  "@context": "https://schema.org",
  "@type": "Person",
  "name": "Jane Doe",
  "url": "http://www.janedoe.com",
  "sameAs": "sdfsdfsdf",
  "jobTitle": "CEO",
  "image": "janedoe.jpg",
  "description": "jane doe description"
}

Until this moment, it is perfectly fine.

But if I add a new field like “memberOf”, then the ordering of the fields gets changed.

{
  "name": "Jane Doe",
  "url": "http://www.janedoe.com",
  "@context": "https://schema.org",
  "memberOf": "janedoewebsite.com",
  "sameAs": "sdfsdfsdf",
  "@type": "Person",
  "jobTitle": "CEO",
  "image": "janedoe.jpg",
  "description": "jane doe description"
}

The whole ordering of fields has been changed by introducing field 9.

System

  • Node.js version: 14.15.3
  • NPM version: 6.14.9
  • Strapi version: 3.6.3
  • Database: SQLite locally, postgreSQL on Production
  • Operating system: macOS Big Sur
@derrickmehaffy derrickmehaffy added the status: can not reproduce Not enough information to reproduce label Aug 12, 2021
@derrickmehaffy
Copy link
Member

derrickmehaffy commented Aug 12, 2021

Tested on v3.6.6 using Linux Mint 20.2 and SQLite (Node v14) and it works fine:

Nevermind, I tried to test with the 2nd block structure posted and it didn't change but if I use the first structure and add the extra entry to the bottom I see that it changes the order now.

@derrickmehaffy derrickmehaffy added severity: low If the issue only affects a very niche base of users and an easily implemented workaround can solve status: confirmed Confirmed by a Strapi Team member or multiple community members issue: bug Issue reporting a bug and removed status: can not reproduce Not enough information to reproduce labels Aug 12, 2021
@derrickmehaffy derrickmehaffy added this to To Review in [Experiment] Issue Board via automation Aug 12, 2021
@derrickmehaffy
Copy link
Member

Unsure of source, it could be something the Content-Manager is doing or it could have something to do with the backend. 🤔

@rowild
Copy link

rowild commented Jan 25, 2022

I observe a similar problem. This is what the json data looks like in the JSON field:

{
  "pilot": {
    "route": "/pilot",
    "btn_txt": "To Pilot Experiment",
    "btn_type": "success"
  },
  "experiment": {
    ...
  },
  "playground": {
    ...
  },
  "introduction": {
    ...
  }
}

If I want to change to order and put introduction as the first object:

{
  "introduction": {
    "route": "/Introduction",
    "btn_txt": "Introduction",
    "btn_type": "success"
  }
  "pilot": {
     ...
  },
  "experiment": {
    ...
  },
  "playground": {
    ...
  }
}

strapi does not accept this and instead saves the old version again.

This only happens in production mode, not in development. (And currently I only tested Strapi v3.)
I would recommend to NOT set severity to low, because this does influence frontend output.

@derrickmehaffy
Copy link
Member

I observe a similar problem. This is what the json data looks like in the JSON field:

{
  "pilot": {
    "route": "/pilot",
    "btn_txt": "To Pilot Experiment",
    "btn_type": "success"
  },
  "experiment": {
    ...
  },
  "playground": {
    ...
  },
  "introduction": {
    ...
  }
}

If I want to change to order and put introduction as the first object:

{
  "introduction": {
    "route": "/Introduction",
    "btn_txt": "Introduction",
    "btn_type": "success"
  }
  "pilot": {
     ...
  },
  "experiment": {
    ...
  },
  "playground": {
    ...
  }
}

strapi does not accept this and instead saves the old version again.

This only happens in production mode, not in development. (And currently I only tested Strapi v3.) I would recommend to NOT set severity to low, because this does influence frontend output.

Can you please test this on v4, we won't be fixing issues on v3 anymore unless they are critical or security related

@nolliebigspin
Copy link
Contributor

Can you please test this on v4, we won't be fixing issues on v3 anymore unless they are critical or security related

I'm using strapi@4.0.5 right now and had the same issue as @rowild yesterday. After saving JSON file, the order of objects is completly messed up.

@derrickmehaffy derrickmehaffy added the source: core:content-manager Source is core/content-manager package label Jan 27, 2022
@derrickmehaffy derrickmehaffy added this to To be reviewed (Open) in Content - Old via automation Jan 27, 2022
@rowild
Copy link

rowild commented Jan 28, 2022

I've just tested my issue in strapi v4, too, and the problem is the same. To sum up:

  • JSON can be reorganised / restructured in development mode
  • JSON cannot be restructured in production mode; there it will always jump back to the first form it had on initial save

@rylax
Copy link

rylax commented Feb 18, 2022

Facing the same issue.

@pandeyprafull
Copy link

Facing the same as well

@derrickmehaffy
Copy link
Member

Can't reproduce on v4.1.8 using SQLite. Marking as closed until additional information is given to reproduce

Content - Old automation moved this from To be reviewed (Open) to Fixed/Shipped Apr 18, 2022
@zaychoer
Copy link

I'm using strapi@4.3.2 right now, default type for JSON field on postgres for strapi is jsonb, so i update the field json to json . After saving JSON file, the order of objects was ordered as entered.

If you want your keys order not to change, you can use the json type that does not change the order of the keys when storing the data in the database.

Hope it helps.

@chakraskun
Copy link

I'm using strapi@4.3.2 right now, default type for JSON field on postgres for strapi is jsonb, so i update the field json to json . After saving JSON file, the order of objects was ordered as entered.

If you want your keys order not to change, you can use the json type that does not change the order of the keys when storing the data in the database.

Hope it helps.

works like a charm 👍

@jmsofarelli
Copy link

I am using Strapi v4.8.2 and have tried to change the datatype directly on PostgreSQL server (from jsonb to json)
Although I can still see the JSON data in Strapi Admin UI, I am getting an error when I perform a query to the collection (I am not using this field in the query):
error: select distinct "t0"."name", "t0".*, "t0"."id" from "public"."declarations" as "t0" left join "public"."declarations_owner_links" as "t1" on "t0"."id" = "t1"."declaration_id" left join "public"."up_users" as "t2" on "t1"."user_id" = "t2"."id" left join "public"."up_users_role_links" as "t3" on "t2"."id" = "t3"."user_id" left join "public"."up_roles" as "t4" on "t3"."role_id" = "t4"."id" where ("t2"."id" = $1 and "t2"."username" = $2 and "t2"."email" = $3 and "t2"."provider" = $4 and "t2"."reset_password_token" is null and "t2"."confirmation_token" is null and "t2"."confirmed" = $5 and "t2"."blocked" = $6 and "t2"."created_at" = $7 and "t2"."updated_at" = $8 and "t4"."id" = $9 and "t4"."name" = $10 and "t4"."description" = $11 and "t4"."type" = $12 and "t4"."created_at" = $13 and "t4"."updated_at" = $14) order by "t0"."name" asc - could not identify an equality operator for type json
Any idea on how to solve / workaround this?

@marianolop
Copy link

I have strapi 3, so I can't change from jsonb to json, did anybody find a solution. This is real important, I have to show some properties in certain order

@igotDiamonds
Copy link

Facing this issue on Strapi v4.10.4

@LostAnurag
Copy link

I have strapi 3, so I can't change from jsonb to json, did anybody find a solution. This is real important, I have to show some properties in certain order

Did you resolve that issue?

@LostAnurag
Copy link

Facing this issue on Strapi v4.10.4

m facing same issue, do you have any solution to fix it?

@LostAnurag
Copy link

you can use the json type that does not change the order of the keys when storing the data in the database.

what you did to reslve it bro?
m facing that issue

@strapi-bot
Copy link

This issue has been mentioned on Strapi Community Forum. There might be relevant details there:

https://forum.strapi.io/t/json-fields-are-ordered-differently-when-exceeding-8-fields-per-object/8841/3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
issue: bug Issue reporting a bug severity: low If the issue only affects a very niche base of users and an easily implemented workaround can solve source: core:content-manager Source is core/content-manager package status: confirmed Confirmed by a Strapi Team member or multiple community members
Projects
No open projects
Development

No branches or pull requests