[英]Query Json data saved in Postgres
我正在嘗試查詢保存在postgres中的JSON數據。 這就是創建表的方式
CREATE TABLE ALARMDATA2(ALARM CHAR(1300))
這是JSON對象:
{"delay_max": 0.0, "ts_errors": [{"count": 0, "state": 0, "is_priority1": true, "name": "SYNC", "is_priority2": false}, {"count": 0, "state": 0, "is_priority1": true, "name": "BYTE", "is_priority2": false}, {"count": 0, "state": 0, "is_priority1": true, "name": "PAT", "is_priority2": false}, {"count": 0, "state": 0, "is_priority1": true, "name": "CC", "is_priority2": false}, {"count": 0, "state": 0, "is_priority1": true, "name": "PMT", "is_priority2": false}, {"count": 0, "state": 0, "is_priority1": true, "name": "PID", "is_priority2": false}, {"count": 0, "state": 0, "is_priority1": false, "name": "TS", "is_priority2": true}, {"count": 0, "state": 0, "is_priority1": false, "name": "CRC", "is_priority2": true}, {"count": 0, "state": 0, "is_priority1": false, "name": "PCR", "is_priority2": true}, {"count": 0, "state": 0, "is_priority1": false, "name": "ACC", "is_priority2": true}, {"count": 0, "state": 0, "is_priority1": false, "name": "PTS", "is_priority2": true}, {"count": 0, "state": 0, "is_priority1": false, "name": "CAT", "is_priority2": true}], "is_stream_paused": false, "delay_min": 0.0, "ac_err": 0.0, "oj_err": 0.0}
我想基於“ delay_max”(JSON中的第一個條目)進行查詢。 我正在使用此查詢
SELECT ALARM->>'delay_max' AS delay_max FROM alarmdata2;
我認為根據這些鏈接( 這里和這里 ),查詢語法是可以的,但是我收到此錯誤
提示:沒有運算符匹配給定的名稱和參數類型。 您可能需要添加顯式類型轉換。
我已經搜索了一段時間,但沒有頭緒。 有什么建議嗎?
您必須將ALARM列定義為JSON或JSONB類型:
CREATE TABLE ALARMDATA2(ALARM JSONB)
然后它將起作用。
ALTER TABLE alarmdata2
ALTER COLUMN alarm
TYPE jsonb
USING alarm::jsonb;
SELECT ALARM::jsonb->>'delay_max' AS delay_max
FROM alarmdata2;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.