Skip to content

Instantly share code, notes, and snippets.

@gtors
Created August 20, 2015 11:38
Show Gist options
  • Save gtors/8a57253504543c4b80b2 to your computer and use it in GitHub Desktop.
Save gtors/8a57253504543c4b80b2 to your computer and use it in GitHub Desktop.
Postgresql function which converts json underscored keys to camel case keys.
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)
$$;
@pnikhil
Copy link

pnikhil commented May 14, 2019

Thanks for this! Can you help me modify this for it to work with JSON array of objects?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment