【问题标题】:Query JSON in Postgres在 Postgres 中查询 JSON
【发布时间】:2017-06-01 01:31:45
【问题描述】:

我的 postgres 数据库中有一些 JSON,它在一个名为 site_content 的表中,该表有两行,idcontentcontent 是我存储 JSON 的地方。我希望能够根据他的 id 找到一个玩家,我的玩家存储在键 series 下,因为这是从 JSON 创建我的图表所需的键。

这是我目前使用的查询:

Blocking.get {
                sql.firstRow("""SELECT * from site_content where content -> 'playersContainer' -> 'series' -> 'id' = ${id} """)
            }.map { row ->
                log.info("row is: ${row}")
                if (row) {
                    objectMapper.readValue(row.getAt(0).toString(), Player)
                }
            }
        }

但是我得到了这个错误:

org.postgresql.util.PSQLException:错误:运算符不存在: json = 字符变化提示:没有运算符与给定名称匹配 和参数类型。您可能需要添加显式类型转换。

这是我的 JSON 示例:

"id": "${ID}",
    "team": {
        "id": "123",
        "name": "Shire Soldiers"
    },
    "playersContainer": {
        "series": [
            {
                "id": "1",
                "name": "Nick",
                "teamName": "Shire Soldiers",
                "ratings": [
                    1,
                    5,
                    6,
                    9
                ],
                "assists": 17,
                "manOfTheMatches": 20,
                "cleanSheets": 1,
                "data": [
                    3,
                    2,
                    3,
                    5,
                    6
                ],
                "totalGoals": 19
            },
            {
                "id": "2",
                "name": "Pasty",
                "teamName": "Shire Soldiers",
                "ratings": [
                    6,
                    8,
                    9,
                    10
                ],
                "assists": 25,
                "manOfTheMatches": 32,
                "cleanSheets": 2,
                "data": [
                    3,
                    5,
                    7,
                    9,
                    10
                ],
                "totalGoals": 24
            }
        ]
    }

我在这个项目中使用 Groovy,但我想这只是我遇到问题的一般 JSON postgres 语法。

【问题讨论】:

    标签: json postgresql groovy


    【解决方案1】:

    你是对的,这是 SQL 语法的问题。更正您的查询:

    select * from json_test where content->'playersContainer'->'series' @> '[{"id":"1"}]';
    

    完整示例:

    CREATE TABLE json_test (
      content jsonb
    );
    
    insert into json_test(content) VALUES ('{"id": "1",
        "team": {
          "id": "123",
          "name": "Shire Soldiers"
        },
        "playersContainer": {
          "series": [
            {
              "id": "1",
              "name": "Nick",
              "teamName": "Shire Soldiers",
              "ratings": [
                1,
                5,
                6,
                9
              ],
              "assists": 17,
              "manOfTheMatches": 20,
              "cleanSheets": 1,
              "data": [
                3,
                2,
                3,
                5,
                6
              ],
              "totalGoals": 19
            },
            {
              "id": "2",
              "name": "Pasty",
              "teamName": "Shire Soldiers",
              "ratings": [
                6,
                8,
                9,
                10
              ],
              "assists": 25,
              "manOfTheMatches": 32,
              "cleanSheets": 2,
              "data": [
                3,
                5,
                7,
                9,
                10
              ],
              "totalGoals": 24
            }
          ]
        }}');
    
    select * from json_test where content->'playersContainer'->'series' @> '[{"id":"1"}]';
    

    关于@> 运算符。这个question 可能也有用。

    【讨论】:

    • 感谢您的回复,但是我收到此错误 ERROR: operator does not exist: json @> unknown
    • 我使用的是 postgres 9.4
    • 我将我的内容转换为 jsonb 而不是 json 并且成功了,谢谢!
    • 抱歉没用,使用这个查询 select content->'playersContainer'->'series' from site_content where content->'playersContainer'->'series' @> '[{" id":"1"}]';我被 id 为 1 和 2 的玩家返回,我只想要 1
    • @NickPocock,这似乎是可能有帮助的答案。在第二列中返回寻找的系列元素。事情是这样的:select elem from json_test t, jsonb_array_elements(t.content->'playersContainer'->'series'‌​) as elem where elem->>'id'='1' ;
    【解决方案2】:

    可能会有所帮助:在 sql 语句中,我在有 json 字段的地方添加了这个“cast”:

    INSERT INTO map_file(type, data)
    VALUES (?, CAST(? AS json))
    RETURNING id
    

    'data'进入map_file表的数据类型是:json

    【讨论】:

    • 你刚刚从here复制粘贴你的答案?...
    猜你喜欢
    • 2017-01-01
    • 2013-09-20
    • 2023-03-03
    • 2014-09-23
    • 2018-04-13
    • 2015-07-17
    • 1970-01-01
    • 2023-03-16
    相关资源
    最近更新 更多