I have a model with a jsonb field where responses from a third-party service are stored. The response changes periodically; some fields are added, some disappear. I want to generate a CSV from a sample of records. To do this, I need to gather all possible fields from the sample and turn them into columns in the CSV file.
Question posted in PostgreSQL
The official documentation can be found here.
The official documentation can be found here.
2
Answers
To achieve this, you’ll need to iterate over your sample records, extract all unique keys from the JSONB field, and then use those keys to create columns in your CSV file. Here’s a step-by-step approach you can follow in Python:
This script iterates over each record in the sample, extracts the keys from the JSONB field, and updates a set containing all unique keys. Then, it creates a CSV file and writes the header with the ‘id’ column and all unique keys as column names. Finally, it iterates over the sample records again, writes each record to the CSV file with corresponding values for each column.
You can replace sample_records with your actual sample data retrieved from the database. Additionally, ensure that you handle any potential errors or edge cases based on your specific requirements and the nature of your data.
Pivoting rows to columns is painful because SQL needs to know the schema of
SELECT
before it starts. Since a CSV file is just data, I have done something along these lines before:Working fiddle