Skip to content

How to control column order when using 'dynamic' component #780

Answered by lovasoa
lozdown asked this question in Q&A
Discussion options

You must be logged in to vote

To control the display order of columns in the resulting JSON object, we will need to get rid of jsonb object merging with ||.
The issue lies in the fact that jsonb does not preserve key order, whereas json_object_agg can be used to explicitly define the order of keys.

Here's the solution, using an intermediate request to create the final ordered keys and values list before calling json_object_agg :

    
 WITH src AS (
   -- This CTE extracts the necessary fields and maintains the correct order.
   -- It includes both the "Last Name" and "at" fields to satisfy the GROUP BY requirement.
   SELECT
      f."Last Name" AS name,
      e."at" AS cnt,
      f.eventdate,
      CASE WHEN f.partici…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by lozdown
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants