Skip to content

Instantly share code, notes, and snippets.

@robbles
Created March 7, 2016 19:43
Show Gist options
  • Save robbles/a12e57254e73b50b9048 to your computer and use it in GitHub Desktop.
Save robbles/a12e57254e73b50b9048 to your computer and use it in GitHub Desktop.
Extract attribute from a JSON column with SQL
--
-- 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