承接国内外服务器租用托管、定制开发、网站代运营、网站seo优化托管接单、网站代更新,新老站点皆可!!咨询QQ:3787320601

如何获得PostgreSQL数据库中的JSON值

管理员 2023-07-19 08:02:05 互联网圈 0 ℃ 0 评论 5129字 收藏

如何获得PostgreSQL数据库中的JSON值

在PostgreSQL数据库中有一列为JSON,要获得JSON中得数据可以用下面sql:

select orderno as OrderNo
,amount as Amount
,ordertime as OrderTime
, recordtype as RecordType
from jsonb_to_recordset(( –特定方法
select array_to_json(array_agg(data)) –转换成一个数组
from wallet_details
where id = @id
)::jsonb) as x(orderno text, amount numeric(16, 6), ordertime text, recordtype varchar(32));

如果你获得得数据是当前行,但是JSON中也要取出来几个值可以用下面的方式获得:

select pay_params::json->>’Key’ as Md5Key ,
pay_params::json->>’AppId’ as Appid ,
pay_params::json->>’MchId’ as Mchid ,
pay_params::json->>’SubMchId’ as Submchid ,
tenant_id as Tenant_Id
from spm_wallet_settings where id=’12’

补充:PostgreSql数据库sql语句取Json值

1:json字段实例:

{
“boxNum”: 0,
“orderNum”: 0,
“commentNum”: 0
}

A.取boxNum的值

1.1)select 字段名->‘boxNum’ from 表名;

1.2)select jsonb_extract_path_text字段名, ‘boxNum’) from 表名;

2:json字段实例:

{
“boxNum”: “0”,
“orderNum”: “0”,
“commentNum”: “0”
}

A.取boxNum的值,不带双引号。

2.1)select 字段名->>‘boxNum’ from 表名;

2.2)select jsonb_extract_path_text字段名, ‘boxNum’) from 表名;

3:json字段实例:

{
“unitPrices”: [{
“price”: 10.0,
“unitId”: “8”,
“unitName”: “500克”,
“unitAmount”: “0”,
“isPMDefault”: true,
“isHomeDefault”: true,
“originalPrice”: 10.0
}],
“productName”: “远洋 加拿大 螯龙虾 野生捕捞”,
“productType”: 1,
“skuPortRate”: {
“id”: “a6b83048⑶878⑷698⑻8c2⑵a9de288ac56”,
“cityId”: “2bf8c60c⑺89d⑷33a⑼1ae⑻e4ae3e587a4”,
“dynamicProperties”: [{
“name”: “死亡率”,
“propertiesId”: “f05bda8c-f27c⑷cc6-b97e-d4bd07272c81”,
“propertieValue”: {
“value”: “2.0”
}
}, {
“name”: “失水率”,
“propertiesId”: “ee9d95d7⑺e28⑷d54-b572⑷8ae64146c46”,
“propertieValue”: {
“value”: “3.0”
}
}]
},
“quotePriceAttribute”: {
“currencyName”: “人民币”
}
}

A.取quotePriceAttribute中的currencyName币制名称

select (字段名>>‘quotePriceAttribute’)::json->>‘currencyName’ from 表名;

B.取unitPrices中的price单价

select jsonb_array_elements((字段名->>‘unitPrices’)::jsonb)->>‘price’ from 表名;

C.取skuPortRate中的dynamicProperties的name为死亡率的propertieValue里面的value;

select bb->‘propertieValue’->>‘value’ as value from (
select jsonb_array_elements(((字段名->>‘skuPortRate’)::json->>‘dynamicProperties’)::jsonb) as bb from 表名) as dd where dd.bb @> ‘{“name”: “死亡率”}’;

4.json字段实例:

[{“name”: “捕捞方式”, “showType”: 4, “propertiesId”: “9a14e435⑼688⑷e9b-b254-0e8e7cee5a65”,
“propertieValue”: {“value”: “野生捕捞”, “enValue”: “Wild”}},
{“name”: “加工方式”, “showType”: 4, “propertiesId”: “7dc101df-d262⑷a75-bdca⑼ef3155b7507”,
“propertieValue”: {“value”: “单冻”, “enValue”: “Individual Quick Freezing”}},
{“name”: “原产地”, “showType”: 4, “propertiesId”: “dc2b506e⑹620⑷e83⑻ca1-a49fa5c5077a”,
“propertieValue”: {“value”: “爱尔兰”, “remark”: “”, “enValue”: “Ireland”}}]

–获得原产地

select
(SELECT ss->‘propertieValue’ as mm FROM
(SELECT jsonb_array_elements (dynamic_properties) AS ss FROM product
where id=a.id) as dd where dd.ss @> ‘{“name”: “原产地”}’)->>‘value’ as cuntry,
a.*
from product as a where a.id=‘633dd80f⑺250⑷65f⑻982⑺a7f01aaeeec’;

5:json例子:huren:[“aaa”,“bbb”,“ccc”…]

需求:取值aaa去““双引号”

select replace(cast(jsonb_array_elements(huren) as text), ‘”‘,”) from XXX limit 1

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有毛病或未斟酌完全的地方,望不吝赐教。

文章来源:丸子建站

文章标题:如何获得PostgreSQL数据库中的JSON值

https://www.wanzijz.com/view/65421.html

相关文章

Related articles

X

截屏,微信识别二维码

微信号:weimawl

(点击微信号复制,添加好友)

打开微信