简体   繁体   English

从PostGreSQL jsonb列中提取键/值对?

[英]Extracting key-value pair from PostGreSQL jsonb column?

I have a postgresql database in which I have a column that contains a jsonb object. 我有一个postgresql数据库,其中有一个包含jsonb对象的列。

I've come accross a situation where I need to extract a single key-value pair from my json object. 我遇到了需要从json对象提取单个键值对的情况。 I have currently no idea and have been unable to find any examples of how this is done. 我目前不知道,也无法找到有关此操作的任何示例。

I have the following table: 我有下表:

----------------------------------------------
| id | did | timestamp | data | db_timestamp |
----------------------------------------------

The data column is my json object and one examples of what it contains is: 数据列是我的json对象,其中包含的一个示例是:

{"n": 336372148490, "ac": 22.0, "al": 119.0, "be": 346.3, "la": 55.69707492, "lo": 12.58713834, "sa": 5, "sp": 2.6100767, "provider": "gps"}

So what I want to do is to write a query in which I look for a single key-value pair. 所以我想做的是编写一个查询,在其中查找单个键值对。 I Currently have the following query, 我目前有以下查询,

SELECT data WHERE did = '357139052424715' and timestamp < 1466642640000

I want to extract a single key-value pair. 我想提取一个键值对。 How do I change the above query to extract "la": 55.69707492 only? 如何更改以上查询以提取"la": 55.69707492"la": 55.69707492

you can do this as: 您可以这样做:

SELECT data->>'la' WHERE did = '357139052424715' and timestamp < 1466642640000

For more examples see: 有关更多示例,请参见:

http://clarkdave.net/2013/06/what-can-you-do-with-postgresql-and-json/ http://clarkdave.net/2013/06/what-c​​an-you-do-with-postgresql-and-json/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM