【发布时间】:2015-07-14 20:42:04
【问题描述】:
我希望能够在 json 字段中访问存储在 json 中的更深层次的元素,存储在 postgresql 数据库中。例如,我希望能够从下面提供的 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
这是来自 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"
}
该 SQL 查询有效,只是它没有给我任何 totalFriends 的返回值(例如 data#>>'{userAttributes, "Total Friends"}' as totalFriends)。我认为问题的一部分是事件落在方括号内(我不知道在 json 格式中表示什么)而不是花括号,但我也无法从 userAttributes 键中提取值。
如果有人可以帮助我,我将不胜感激。
很抱歉,如果其他地方有人问过这个问题。我对 postgresql 甚至 json 都很陌生,以至于我无法想出正确的术语来找到这个(和相关)问题的答案。
【问题讨论】:
-
Your query works。这可能是您的实际数据存在问题。
-
是的,好像是这样。一旦你说我能够找出问题所在。对于我试图抓住的每个元素,我都需要一个 group by 语句。然后它起作用了。
标签: json postgresql