【问题标题】:How to access json values when keys vary from one row to another in SQLite?当 SQLite 中的键从一行到另一行不同时,如何访问 json 值?
【发布时间】:2020-09-22 07:59:55
【问题描述】:

试图理解以 json 格式编写的数据库。我设法将主表转换为 csv 格式,但是表本身中有以 json 格式编写的列。

有问题的表格描述了足球比赛,我遇到问题的属性提供了两支参赛球队的详细信息(例如阵容、进球等)。该属性提供了两个主要的键/值对,键值是参加比赛的球队的 id,因此键会逐行更改。

我已经设法通过在如下查询中手动写入 id 来访问一些信息(使用 team_id 1609):

SELECT json_extract(teams_data, "$.1609") AS trial
FROM Matches_England

(有问题的列称为teams_data,有问题的表称为Matches_England)

然而,这显然不是每场比赛都可行的。

我正在寻找一种方法来访问 SQLite 中每一行的这两个值。

这是相关行的示例:

{**"1609"**: {"scoreET": 0, "coachId": 7845, "side": "home", "teamId": 1609, "score": 4, "scoreP": 0, "hasFormation": 1, "formation": {"bench": [{"playerId": 20612, "ownGoals": "0", "redCards": "0", "goals": "0", "yellowCards": "0"}, {"playerId": 25662, "ownGoals": "0", "redCards": "0", "goals": "0", "yellowCards": "0"}, {"playerId": 7864, "ownGoals": "0", "redCards": "0", "goals": "0", "yellowCards": "0"}, {"playerId": 230020, "ownGoals": "0", "redCards": "0", "goals": "0", "yellowCards": "0"}, {"playerId": 26010, "ownGoals": "0", "redCards": "0", "goals": "1", "yellowCards": "0"}, {"playerId": 7879, "ownGoals": "0", "redCards": "0", "goals": "0", "yellowCards": "0"}, {"playerId": 7870, "ownGoals": "0", "redCards": "0", "goals": "1", "yellowCards": "0"}], "lineup": [{"playerId": 370224, "ownGoals": "0", "redCards": "0", "goals": "0", "yellowCards": "0"}, {"playerId": 120339, "ownGoals": "0", "redCards": "0", "goals": "0", "yellowCards": "0"}, {"playerId": 7945, "ownGoals": "0", "redCards": "0", "goals": "1", "yellowCards": "0"}, {"playerId": 14869, "ownGoals": "0", "redCards": "0", "goals": "0", "yellowCards": "0"}, {"playerId": 25413, "ownGoals": "0", "redCards": "0", "goals": "1", "yellowCards": "0"}, {"playerId": 7868, "ownGoals": "0", "redCards": "0", "goals": "0", "yellowCards": "0"}, {"playerId": 3560, "ownGoals": "0", "redCards": "0", "goals": "0", "yellowCards": "0"}, {"playerId": 167145, "ownGoals": "0", "redCards": "0", "goals": "0", "yellowCards": "0"}, {"playerId": 3319, "ownGoals": "0", "redCards": "0", "goals": "0", "yellowCards": "0"}, {"playerId": 7882, "ownGoals": "0", "redCards": "0", "goals": "0", "yellowCards": "0"}, {"playerId": 49876, "ownGoals": "0", "redCards": "0", "goals": "0", "yellowCards": "0"}], "substitutions": [{"playerIn": 26010, "playerOut": 370224, "minute": 67}, {"playerIn": 7870, "playerOut": 120339, "minute": 67}, {"playerIn": 7879, "playerOut": 7945, "minute": 75}]}, "scoreHT": 2}, **"1631"**: {"scoreET": 0, "coachId": 333782, "side": "away", "teamId": 1631, "score": 3, "scoreP": 0, "hasFormation": 1, "formation": {"bench": [{"playerId": 119630, "ownGoals": "0", "redCards": "0", "goals": "0", "yellowCards": "0"}, {"playerId": 8498, "ownGoals": "0", "redCards": "0", "goals": "0", "yellowCards": "0"}, {"playerId": 350976, "ownGoals": "0", "redCards": "0", "goals": "0", "yellowCards": "0"}, {"playerId": 8066, "ownGoals": "0", "redCards": "0", "goals": "0", "yellowCards": "0"}, {"playerId": 285508, "ownGoals": "0", "redCards": "0", "goals": "0", "yellowCards": "0"}, {"playerId": 217078, "ownGoals": "0", "redCards": "0", "goals": "0", "yellowCards": "0"}, {"playerId": 283142, "ownGoals": "0", "redCards": "0", "goals": "0", "yellowCards": "0"}], "lineup": [{"playerId": 14763, "ownGoals": "0", "redCards": "0", "goals": "1", "yellowCards": "0"}, {"playerId": 192748, "ownGoals": "0", "redCards": "0", "goals": "0", "yellowCards": "0"}, {"playerId": 8013, "ownGoals": "0", "redCards": "0", "goals": "0", "yellowCards": "0"}, {"playerId": 8480, "ownGoals": "0", "redCards": "0", "goals": "0", "yellowCards": "0"}, {"playerId": 8653, "ownGoals": "0", "redCards": "0", "goals": "0", "yellowCards": "0"}, {"playerId": 149019, "ownGoals": "0", "redCards": "0", "goals": "0", "yellowCards": "0"}, {"playerId": 8488, "ownGoals": "0", "redCards": "0", "goals": "0", "yellowCards": "94"}, {"playerId": 14853, "ownGoals": "0", "redCards": "0", "goals": "0", "yellowCards": "0"}, {"playerId": 265366, "ownGoals": "0", "redCards": "0", "goals": "0", "yellowCards": "0"}, {"playerId": 12829, "ownGoals": "0", "redCards": "0", "goals": "2", "yellowCards": "0"}, {"playerId": 26150, "ownGoals": "0", "redCards": "0", "goals": "0", "yellowCards": "0"}], "substitutions": [{"playerIn": 217078, "playerOut": 14763, "minute": 72}, {"playerIn": 285508, "playerOut": 192748, "minute": 82}, {"playerIn": 283142, "playerOut": 8013, "minute": 88}]}, "scoreHT": 2}}

在这种情况下,参加比赛的两个团队 id 是 1631 和 1609。如果我能够找到一种方法将这两个键/值对分成两个单独的列,仅包括两个单独的列,那就足够了值(因为团队 ID 再次嵌套在原始值中,所以我仍然可以从那里获取它)。

【问题讨论】:

  • 我不会存储大量文本,而是将其分解为规范化所有关系和数据所需的许多表。更容易使用
  • 这确实有效。我用python规范了一切。谢谢!

标签: json sqlite key-value sqlite-json1


【解决方案1】:

使用json_each 将每个键/值对变成一行。

select *
from json_each('{"123": {"a": 23, "b": 42}, "456": {"a": 5, "b": 7}}');

key  value            type    atom  id  parent  fullkey  path
---  ---------------  ------  ----  --  ------  -------  ----
123  {"a":23,"b":42}  object        2           $.123    $   
456  {"a":5,"b":7}    object        8           $.456    $   

然后使用值列。

select json_extract(things.value, '$.a') as a
from (
  select * from json_each('{"123": {"a": 23, "b": 42}, "456": {"a": 5, "b": 7}}')
) things;

a 
--
23
5 

还有json_tree 可以更直接地处理嵌套数据。

这显然很尴尬,而且会很慢,都是字符串处理。如果可能,请执行此操作一次并将数据规范化为表格和行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-12
    • 2014-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    • 2023-03-15
    相关资源
    最近更新 更多