Created
May 4, 2021 15:02
-
-
Save isapir/0d6dae5cb34af7b60b34ce6ddf80bf73 to your computer and use it in GitHub Desktop.
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
/** converts http cookie header to json using split */ | |
create or replace function httpcookie2json(cookie text) | |
returns text language plpgsql as | |
$body$ | |
declare | |
arr text[]; | |
el text; | |
pos int; | |
k text; | |
v text; | |
arrjs text[] := '{}'; | |
begin | |
if cookie is null then | |
return null; | |
end if; | |
arr = string_to_array(cookie, ';'); | |
foreach el in array arr loop | |
el = trim(el); | |
pos = strpos(el, '='); | |
k = substr(el, 1, pos - 1); | |
v = substr(el, pos + 1); | |
arrjs = array_append(arrjs, '"' || k || '"' || ':' || '"' || v || '"'); | |
end loop; | |
return '{' || array_to_string(arrjs, ',') || '}'; | |
end | |
$body$ | |
/** converts http cookie header to json using record set */ | |
create or replace function httpcookie2json(cookie text) | |
returns text language plpgsql as | |
$body$ | |
declare rec record; | |
k text; | |
v text; | |
arr text[] := '{}'; | |
begin | |
for rec in | |
select trim(el) as c, | |
strpos(trim(el), '=') as pos | |
from unnest(string_to_array(cookie, ';')) as el | |
loop | |
k = substr(rec.c, 1, rec.pos - 1); | |
v = substr(rec.c, rec.pos + 1); | |
arr = array_append(arr, '"' || k || '"' || ':' || '"' || v || '"'); | |
end loop; | |
return '{' || array_to_string(arr, ',') || '}'; | |
end | |
$body$ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment