我有以下postgres函数,它将JSON对象中的所有键转换为驼峰格式。
CREATE FUNCTION 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));
$$;
CREATE FUNCTION json_underscore_to_camel_case(data json)
RETURNS json
IMMUTABLE
LANGUAGE sql
AS $$
SELECT ('{'||string_agg(underscore_to_camel_case(key)||':'||value, ',')||'}')::json
FROM json_each(data)
$$;
示例:
select json_underscore_to_camel_case('{"first_key": "value1", "second_key":"value2"}');
被转换为:
{"firstKey":"value1","secondKey":"value2"}
但是它不适用于对象数组。我如何对其进行修改以使其能够与JSON数组一起使用?