Last active
March 9, 2022 20:23
-
-
Save franccesco/395a02b22868e184f661c990190d3a11 to your computer and use it in GitHub Desktop.
JSON example for PSQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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