Created
August 20, 2015 11:38
-
-
Save gtors/8a57253504543c4b80b2 to your computer and use it in GitHub Desktop.
Postgresql function which converts json underscored keys to camel case keys.
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
CREATE FUNCTION key_underscore_to_camel_case(s text) | |
RETURNS json | |
IMMUTABLE | |
LANGUAGE sql | |
AS $$ | |
SELECT to_json(substring(s, 1, 1) || substring(replace(initcap(replace(s, '_', ' ')), ' ', ''), 2)); | |
$$; | |
-- TODO: add recursive processing | |
CREATE FUNCTION json_underscore_to_camel_case(data json) | |
RETURNS json | |
IMMUTABLE | |
LANGUAGE sql | |
AS $$ | |
SELECT ('{'||string_agg(key_underscore_to_camel_case(key)||':'||value, ',')||'}')::json | |
FROM json_each(data) | |
$$; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this! Can you help me modify this for it to work with JSON array of objects?