How do you store key-value pair data with NO ORDER?

How to store key-value pair data with NO ORDER ??

Hello, everyone.
I am a student who are creating an app using by APP INVENTOR.
Here is my question.

After the database saves key-value pair data, it will arrange them in A~Z order, but I don't want to do this.

Just like:
{"zip":"abc","bob":"def"}
After saving, it turn to be:
{"bob":"def","zip":"abc"}

But I DON't want to do this.
What I want just to keep the original order
What should I do?

THANK YOU !
(Please forgive my poor English)

Why does the sort order matter ?

What are you doing with your json once it is returned ?

Actually, it’s the AI API (Which I use in my project) that requires the JSON in a specific format—like {"role":"user","content":"text"}—or it’ll throw an error saying the JSON is malformed.

Here is a workaround you could do with blocks

If it has some same key name, how could i do ?
​Like
{"role":"user","content":"text1","role":"system","content":"text2","role":"user","content":"text3"}........

This is a two column table, with optional header row.

Use [["role","content"],["user","text1"],["system","text2"],...]
as your JSON text.

That is not valid json

sorry,i may get wrong last time.
The ture json is
[{"role":"user","content":"text1"},{"role":"system","content":"text2"},{"role":"user","content":"text3"}]
but it will turn to be
[{"content":"text1","role":"user"},{"content":"text2","role":"system"},{"content":"text3","role":"user"}]
which order from A to Z

So you are keeping a list of objects.
Each object is a dictionary with keys "content" and "role'.

The order of contents of a dictionary should not matter, since you access it by key, not position.

How are you saving and restoring the data in the database?

How was the table data defined as a table to the data base?


The JSON text I mentioned above corresponds to the variable "message" in the image.
I use it to save the chat history

I know what you mean.But the question is i dont konw how the ai api work...if the order change, it will return error code(mean json wrong) to me

Which tag value data base are you using?

  • cloudDB
  • Firebase
  • TinyWebDB

?

CloudDB has a block to add an entry to a list,, perfect for chat history.

As a procedure:

actually, i dont know how difference between these tag....but i am using the "TinyDB"(not web) to do it.
and i want to save the history into my phone local but not online.
could you offer me another plan? or explain the difference between them?

If possible, I would be most grateful!!!!

TinyDB is the easiest data base.

To preserve the list order of a chat history locally, pick a tag for when the chat started, like 2025-12-21-14:40 (My local time for when this board thread started in date time sort order).

Start the value under that tag as an empty list.

As each message arrives as a three key dictionary,

  • retrieve the chat history for the current chat start datetime as a list into a local variable named history (empty list if not found)
  • add the message dictionary object to the list history
  • save the updated chat history list back into its TinyDB tag.

Read the articles on How to Work With Lists.