semmitmondo

semmitmondo

How to decode a JSON object into a keyword list while preserving the same key order?

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.

Most Liked

aseigo

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.

thiagoramos

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})

LostKobrakai

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

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!

michalmuskala

michalmuskala

Exactly the objects: :ordered_objects is the way to solve this, no need for hacky solutions. It decodes into a Jason.OrderedObject struct that implements Access and Enumerable and can be used similarly to a keyword list. You can also access the .values field for the actual raw keyword. The ordered object struct also encodes into JSON with the specified order.

And yes, while JSON is explicitly not ordered and relying on order is not conforming, there’s not much you can do usually, when interacting with APIs where the order does matter

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement