如何在PostgreSQL上从JSON数据中提取特定信息

new9mtju  于 2022-12-15  发布在  PostgreSQL
关注(0)|答案(1)|浏览(201)

我想恳请您的帮助有关的JSON数据的MySQL。

[
  {"type" : "PET", "rate_period" : "MONTHLY", "amount" : 100.00, "included" : false},
  {"type" : "PARKING", "rate_period" : "MONTHLY", "amount" : 150.00, "included" : false}
]

这里是数据的形式,我想提取每个类别的信息(类型,利率_期间,金额)。请记住,我是在元数据库工作,不承认命令(SPLIT_PART,JSON_EXTRACT)。
你知道吗?
先谢了!
我实际尝试的是:

SELECT p.deal_info::json->>'{}')::json->>'type'
FROM deals

Code on SQL where I just get the Column

camsedfj

camsedfj1#

CREATE TABLE test
SELECT 1 id, CAST('
[
  {"type" : "PET", "rate_period" : "MONTHLY", "amount" : 100.00, "included" : false},
  {"type" : "PARKING", "rate_period" : "MONTHLY", "amount" : 150.00, "included" : true}
] 
' AS JSON) data;
SELECT id, CAST(data AS CHAR) FROM test;
SELECT id, jsontable.*
FROM test
CROSS JOIN JSON_TABLE(test.data,
                      '$[*]' COLUMNS ( type VARCHAR(255) PATH '$.type', 
                                       rate_period VARCHAR(255) PATH '$.rate_period', 
                                       amount DECIMAL(10,2) PATH '$.amount', 
                                       included BOOLEAN PATH '$.included'
                                       )) jsontable

| 身份证|类型|速率_周期|数量|包括在内|
| - ---------------------------------------------------|- ------|- ------|- ------|- ------|
| 1个|PET|每月|一百元|无|
| 1个|停车|每月|一百五十元|1个|
fiddle

相关问题