我希望能够访问存储在postgresql数据库中的字段json中存储在json中的更深层元素.例如,我希望能够从下面提供的json访问遍历路径states-> events-> time的元素.这是我正在使用的postgreSQL查询:
SELECT
data#>> '{userId}' as user,
data#>> '{region}' as region,
data#>>'{priorTimeSpentInApp}' as priotTimeSpentInApp,
data#>>'{userAttributes, "Total Friends"}' as totalFriends
from game_json
WHERE game_name LIKE 'myNewGame'
LIMIT 1000
Run Code Online (Sandbox Code Playgroud)
这是来自json字段的示例记录
{
"region": "oh",
"deviceModel": "inHouseDevice",
"states": [
{
"events": [
{
"time": 1430247045.176,
"name": "Session Start",
"value": 0,
"parameters": {
"Balance": "40"
},
"info": ""
},
{
"time": 1430247293.501,
"name": "Mission1",
"value": 1,
"parameters": {
"Result": "Win ",
"Replay": "no",
"Attempt Number": "1"
},
"info": ""
}
]
}
],
"priorTimeSpentInApp": 28989.41467999999,
"country": "CA",
"city": "vancouver",
"isDeveloper": true,
"time": 1430247044.414,
"duration": 411.53,
"timezone": "America/Cleveland",
"priorSessions": 47,
"experiments": [],
"systemVersion": "3.8.1",
"appVersion": "14312",
"userId": "ef617d7ad4c6982e2cb7f6902801eb8a",
"isSession": true,
"firstRun": 1429572011.15,
"priorEvents": 69,
"userAttributes": {
"Total Friends": "0",
"Device Type": "Tablet",
"Social Connection": "None",
"Item Slots Owned": "12",
"Total Levels Played": "0",
"Retention Cohort": "Day 0",
"Player Progression": "0",
"Characters Owned": "1"
},
"deviceId": "ef617d7ad4c6982e2cb7f6902801eb8a"
}
Run Code Online (Sandbox Code Playgroud)
该SQL查询有效,除了它没有给我任何totalFriends的返回值(例如data#>>'{userAttributes,"Total Friends"}'作为totalFriends).我假设问题的一部分是事件落在一个方括号内(我不知道json格式中的含义)而不是大括号,但我也无法从userAttributes键中提取值.
如果有人能帮助我,我将不胜感激.
如果在别处问过这个问题,我很抱歉.我对postgresql甚至json都很陌生,以至于我无法找到适当的术语来找到这个(和相关的)问题的答案.
kli*_*lin 11
你应该熟悉Postgres 中json 和json函数和运算符的基础知识.
在第二个来源注意运营商->和->>.一般规则:用于->获取json对象,->>以获取json值作为文本.使用这些运算符,您可以以返回正确值的方式重写查询'Total Friends':
select
data->>'userId' as user,
data->>'region' as region,
data->>'priorTimeSpentInApp' as priotTimeSpentInApp,
data->'userAttributes'->>'Total Friends' as totalFriends
from game_json
where game_name like 'myNewGame';
Run Code Online (Sandbox Code Playgroud)
方括号中的Json对象是json数组的元素.Json数组可能有许多元素.元素由索引访问.Json数组从0开始索引(数组的第一个元素的索引为0).例:
select
data->'states'->0->'events'->1->>'name'
from game_json
where game_name like 'myNewGame';
-- returns "Mission1"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1517 次 |
| 最近记录: |