I have data in table which is of type text so i am converting it to json. Based on id of data i want to update a value of column.
SELECT
CASE
WHEN is_json(data)
THEN data::json #> '{id}'
ELSE
NULL
END
FROM table;
UPDATE table SET hash = md5(
result from above query??
);
Some kind of:
How can i write the update query?
SELECT
CASE
WHEN is_json(data)
THEN data::json #> md5('{id}')
ELSE
NULL
END
FROM table;
I hava json stored in a text column. data:
{'id': 'e807b554-fd15-4f7c-bbc5-d9531c8174a9','array': ['a'], 'rank': 151}
My stored procedure is:
CREATE OR REPLACE FUNCTION is_json(input_text varchar) RETURNS boolean AS $$
DECLARE
maybe_json json;
BEGIN
BEGIN
maybe_json := input_text;
EXCEPTION WHEN others THEN
RETURN FALSE;
END;
RETURN TRUE;
END;
$$ LANGUAGE plpgsql IMMUTABLE;
When i select the query:
select * from table where is_json(data);
I get empty row. Whereas data has json in form of text.
Is this what you want?
update mytable
set hash = md5(case when is_json(data) then data::json #>> '{id}' end)
Or if you want to only update rows that contain valid json:
update mytable
set hash = md5(data::json #>> '{id}')
where is_json(data)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.