在Postgres中查询JSON

时间:2017-01-17 05:13:33

标签: json postgresql groovy

我的postgres数据库中有一些JSON,它位于一个名为site_content的表中,该表有两行,idcontent,在content中是我的地方存储我的JSON。我希望能够找到一个给出他身份的玩家,我的玩家存储在密钥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语法。

2 个答案:

答案 0 :(得分:3)

你是对的,这是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可能也很有用。

答案 1 :(得分:-1)

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

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

'数据的数据类型'到map_file表是:json

相关问题