Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails jsonb - Prevent JSON keys from reordering when jsonb is saved to Postgresql database

I have a column amount_splits that I need to save my JSON to in the key order I've specified.

How do I prevent Rails / Postgres jsonb from auto sorting my JSON keys when I save it to the database? (for creating or updating)

It looks like it's trying to sort alphabetically, but does a poor job at it.

Here's what I'm saving:

{
    "str_fee": 3.17,       # key 1
    "eva_fee": 14.37,      # key 2
    "fran_royalty": 14.37, # key 3
    "fran_amount": 67.09   # key 4
}

This is how it actually saves:

{
    "eva_fee": 14.37,     # key 2
    "str_fee": 3.17,      # key 1
    "fran_amount": 67.09, # key 4
    "fran_royalty": 14.37 # key 3
}

Purpose:

Before you answer "sorting doesn't matter when the JSON is consumed on the receiving end", stop and think first please... and please read on

I need the keys to be sorted in the way I need them sorted because the client interface that consumes this JSON is displaying the JSON to developers that need the keys to be in the order that the documentation tells them its in. And the reason it needs to be in that order is to display the process of what calculations happened in which order first:

The correct order tells the developer:

The str_fee was applied first, then the eva_fee, then the fran_royalty... making fran_amount the ending amount.

But based on how jsonb sorts this, it incorrectly tells our developers that:

The eva_fee was applied first, then the str_fee, then the fran_amount... making fran_royalty the ending amount.

like image 203
skplunkerin Avatar asked Aug 31 '18 22:08

skplunkerin


People also ask

Is Jsonb better than JSON?

The json data type stores an exact copy of the input text, which processing functions must reparse on each execution; while jsonb data is stored in a decomposed binary format that makes it slightly slower to input due to added conversion overhead, but significantly faster to process, since no reparsing is needed.

Is Jsonb efficient?

Most applications should use JSONB for schemaless data. It stores parsed JSON in a binary format, so queries are efficient.

Can you index Jsonb Postgres?

JSONB and IndexesPostgreSQL can use indexes for the text results as compare operands. GIN index can be used by the GIN JSONB operator class.

How do I query Jsonb data in PostgreSQL?

Querying the JSON documentPostgreSQL has two native operators -> and ->> to query JSON documents. The first operator -> returns a JSON object, while the operator ->> returns text. These operators work on both JSON as well as JSONB columns. There are additional operators available for JSONB columns.


1 Answers

Actually they're not sorted alphabetically but rather by key length then alphabetically, that explains the order you get. The jsonb type has been create as a better version of the json type to write and access data and it's probably for indexation and search purpose that they change the keys order. 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.

like image 83
Abderrahmane Amalou Avatar answered Sep 21 '22 08:09

Abderrahmane Amalou