Skip to content

Instantly share code, notes, and snippets.

@franccesco
Last active March 9, 2022 20:23
Show Gist options
  • Save franccesco/395a02b22868e184f661c990190d3a11 to your computer and use it in GitHub Desktop.
Save franccesco/395a02b22868e184f661c990190d3a11 to your computer and use it in GitHub Desktop.
JSON example for PSQL
-- JSON Table
WITH json_table AS (
SELECT '{"action": "remove_newsletter_cta_session", "controller": "pages"}'::json AS json_field
)
-- Return values from JSON column
SELECT
json_field->>'action' AS action,
json_field->>'controller' AS controller
FROM json_table
WITH json_cte AS (
SELECT '{"isbn":"123-456-222","author":{"lastname":"Doe","firstname":"Jane"},"editor":{"lastname":"Smith","firstname":"Markus"},"title":"The Ultimate Database Study Guide","category":["Non-Fiction","Technology"]}'::json as json_field
)
SELECT
json_field->>'isbn' AS isbn,
json_field->>'title' AS title,
json_field->>'category' AS categories,
json_field->'author'->>'firstname' AS firstname,
json_field->'author'->>'lastname' AS lastname,
CONCAT(json_field->'editor'->>'firstname', ' ', json_field->'editor'->>'firstname') AS editor_full_name
FROM json_cte
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment