Created
March 7, 2016 19:43
-
-
Save robbles/a12e57254e73b50b9048 to your computer and use it in GitHub Desktop.
Extract attribute from a JSON column with SQL
This file contains hidden or 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
-- | |
-- NOTE: this will just give you the original data if the attribute is not present. | |
-- You might need to wrap it in an IF somehow if it's not always there. | |
-- | |
SELECT | |
substring_index(substring_index(TABLE.JSON_COLUMN, '"ATTRIBUTE_TO_EXTRACT": "', -1), '",', 1) | |
from TABLE.JSON_COLUMN | |
-- If the JSON data looks like this: {"key":"value"} instead of this: {"key": "value"} (no space after colon), | |
-- you'll need to use this slightly modified version: | |
SELECT | |
substring_index(substring_index(TABLE.JSON_COLUMN, '"ATTRIBUTE_TO_EXTRACT":"', -1), '",', 1) | |
from TABLE.JSON_COLUMN |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment