semmitmondo
How to decode a JSON object into a, for instance, keyword list while preserving the same key order that the JSON string uses?
Elixir maps lose this information because of the underlaying Erlang map implementation.
Trending in Questions
One of my LiveView view’s has a very heavy javascript dependency so I want to avoid loading it for every page on the site, while still us...
New
Hi all!
I know that quite a few people use Phoenix LiveView but it is hard to get a more up-to-date list of folks/companies using it. I ...
New
Hello everyone!
I am new to coding with Elixir/Erlang. And i am looking for some utilities for fuzzing testing like AFL++.
Does anyone ...
New
I’m very new to Elixir, so sorry if this is a silly question:
I am wondering what happens to the mailbox of a process when the machine i...
New
I load tested a plain Phoenix chat app backed by Postgres with 100 million messages in it, everything on default settings, to see what br...
New
Hey, I know this has been discussed ad nauseum, but I couldn’t find a definitive answer or at least some consensus around this, so let me...
New
I don’t suppose you can lazy load a library depending on runtime data? For example if it’s a certain user they can lazy load ‘library A’ ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hi everyone :waving_hand:
I’ve been working on PhiaUI, an open-source UI component library for Phoenix LiveView, and I wanted to share i...
New
On November 23, 2026, an unauthorized third party exploited a vulnerability in hex.pm (CVE-2026-129514, CVSS 10.0) that let them run comm...
New
@hugobarauna, Dr. Dimitrios Koutmos (my brother) and I (Alex Koutmos) have been hard at work on writing a book on how you can use Elixir ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #code-sync
- #exunit
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #ai
- #authentication
- #elixirconf-us
- #umbrella
- #full-time-contract
- #blog-post
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-eu
- #distillery
- #security
- #processes
- #api
- #forms
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
aseigo
According to the JSON specification, JSON objects are unordered. So if you get the same ordering, it is entirely coincidental or an implementation detail of the underlying library. But you can not rely on ordering of the entries there since they are, by specification, not ordered.
You may want to considering using a list instead if the ordering is absolutely required.
OvermindDL1
Precisely this. JSON specifies that objects are unordered but array’s are ordered, so if order is important then it should be an array, not an object.
albertored
I know specification says JSON objects are unordered but sometimes preserving the order can be useful.
Take for instances a situation where you want to read an external JSON file, apply some small changes and write it back preserving the order so to not create large diffs (the file can be under version control).
For instance in python you can do this with this code:
In Elixir I am only able to write JSON objects in a custom order by using
Keywordinstead ofMapbut I haven’t find a way of reading a JSON object into aKeyword.LostKobrakai
In
jasonit seems like objects are actually decoded into a list until the whole object is read and only at that moment the list is converted to a map:https://github.com/michalmuskala/jason/blob/v1.2.2/lib/decoder.ex#L284
But given jason seems to want to be spec compliant I’m not sure it would accept changing that.
albertored
Thanks, I am able to use this
:jasonimplementation detail to get what I want.It is a very far from optimal solution, surely not performant and relies on the order in which keys are decoded by Jason but it seems to work for my needs (it’s more like a script than something that should be done a lot of times per second).
I put it here just for people that may have my same requirements but I wrote it without optimizing it so be kind with your comments
Edited as per @LostKobrakai suggestion
JeyHey
How do you write the loaded json back? Wouldn’t the order of the keys be not deterministic in this step?
albertored
By using
Jason.Encode.keyword/2. This function preserves the order of theKeywordin encoding it.LostKobrakai
Why combine the data for the key into a string just to need a regex to separate it again. You can have a key like
{count, key}in elixir maps.albertored
This is exactly what I mean, I wrote this “without thinking” to much, just to see if it can work. Thanks for your suggestion!
thiagoramos
By the way I had a problem where I had to get the json ordered.
I found out we can use
Jason.decode(json_string, %{objects: :ordered_objects})