when I try to select some record from a table

    SELECT * FROM movie_test WHERE tags = ('["dramatic","women", "political"]'::json)

sql代码抛出了一个错误

LINE 1: SELECT * FROM movie_test WHERE tags = ('["dramatic","women",...
                                        ^
HINT:  No operator matches the given name and argument type(s). You might      need to add explicit type casts.

********** 错误 **********

ERROR: operator does not exist: json = json
SQL 状态: 42883
指导建议:No operator matches the given name and argument type(s). You might need to add explicit type casts.
字符:37

我是不是错过了什么,或者我在哪里可以了解到关于这个错误的一些信息.

推荐答案

简而言之,使用JSONB而不是JSON,或者将JSON转换为JSONB.

You cannot compare json values. You can compare text values instead:

SELECT * 
FROM movie_test 
WHERE tags::text = '["dramatic","women","political"]'

Note however that values of type JSON are stored as text in a format in which they are given. Thus the result of comparison depends on whether you consistently apply the same format:

SELECT 
    '["dramatic" ,"women", "political"]'::json::text =  
    '["dramatic","women","political"]'::json::text      -- yields false!
    

In Postgres 9.4+ you can solve this problem using type JSONB, which is stored in a decomposed binary format. Values of this type can be compared:

SELECT 
    '["dramatic" ,"women", "political"]'::jsonb =  
    '["dramatic","women","political"]'::jsonb           -- yields true

so this query is much more reliable:

SELECT * 
FROM movie_test 
WHERE tags::jsonb = '["dramatic","women","political"]'::jsonb

阅读更多关于JSON Types的内容.

Json相关问答推荐

我如何知道TJSONNumber是double还是double?

使用json_query更新事实

使用快速json库编写json可以消除所有缩进

如何在Android中解析带有动态键和可变对象名称的改装JSON响应?

JOLT分裂和数组数据

如何使用 JOLT 将带有列表的 JSON 项目取消列出为多个项目?

正向闪烁后的微调值

如何编写 jolt 规范以将不同的对象转换为数组

从 Inno Setup 中的 JSON 文件的每个对象中读取特定字符串

jq - 将父键值提取为子元素旁边的逗号分隔值

将环境变量值从 yaml 传递到 json

使用 Javascript 判断 JSON 对象是否包含值

如何使用 C# 将 JSON 文本转换为对象

使用 Spring 和 JsonTypeInfo 注释将 JSON 反序列化为多态对象模型

Select 什么数据类型json或者jsonb或者text

我应该如何处理 JSON 中的 HATEOAS 链接和引用?

在 HTML 数据属性上添加 JSON 是不是很糟糕?

Gson 将一组数据对象转换为 json - Android

如何将单引号转义成双引号转成单引号

如何在本地存储中存储对象数组?