{"id":2309,"date":"2013-03-19T18:07:53","date_gmt":"2013-03-19T09:07:53","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=2309"},"modified":"2018-11-14T16:33:55","modified_gmt":"2018-11-14T07:33:55","slug":"postgresql9-2-json%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/www.techscore.com\/blog\/2013\/03\/19\/postgresql9-2-json%e5%9e%8b\/","title":{"rendered":"PostgreSQL9.2: JSON\u578b"},"content":{"rendered":"

\u3053\u3093\u306b\u3061\u308f\u3001\u5317\u5ddd\u3067\u3059\u3002<\/p>\n

PostgreSQL 9.2 \u3067\u8ffd\u52a0\u3055\u308c\u305f JSON\u578b\u3092\u89e6\u3063\u3066\u307f\u307e\u3057\u305f\u306e\u3067\u7d39\u4ecb\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n

http:\/\/www.postgresql.org\/docs\/9.2\/static\/datatype-json.html<\/a><\/p>\n

JSON\u578b<\/h2>\n

\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u8aad\u3080\u3068JSON\u578b\u306f\u5185\u90e8\u7684\u306b\u306f\u3001RFC 4627 \u6e96\u62e0\u3059\u308b\u3088\u3046\u306b
\n\u30c1\u30a7\u30c3\u30af\u3055\u308c\u305f\u30c6\u30ad\u30b9\u30c8\u578b\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n

\u307e\u305a\u306f\u3001JSON \u578b \u3092\u30ab\u30e9\u30e0\u306b\u6301\u3064\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n

demo=# CREATE TABLE weather (country TEXT , weather_data JSON);\r\n\r\ndemo=# \\d weather\r\n     Table \"public.weather\"\r\n    Column    | Type | Modifiers \r\n--------------+------+-----------\r\n country      | text | \r\n weather_data | json |<\/pre>\n

\u6b21\u306b\u3001\u30c7\u30fc\u30bf\u3092\u5165\u308c\u3066\u307f\u307e\u3059\u3002<\/p>\n

demo=# insert into weather values  ('\u65e5\u672c','{\"\u6771\u4eac\":\"\u6674\u308c\",\"\u5927\u962a\":\"\u96e8\",\"\u540d\u53e4\u5c4b\":\"\u66c7\u308a\"}');\r\n\r\ndemo=# select * from weather;\r\n country |                weather_data                 \r\n---------+---------------------------------------------\r\n \u65e5\u672c    | {\"\u6771\u4eac\":\"\u6674\u308c\",\"\u5927\u962a\":\"\u96e8\",\"\u540d\u53e4\u5c4b\":\"\u66c7\u308a\"}\r\n(1 row)\r\n\r\ndemo=# insert into weather values  ('\u65e5\u672c','[{\"\u6771\u4eac\":\"\u6674\u308c\"},{\"\u5927\u962a\":\"\u96e8\"},{\"\u540d\u53e4\u5c4b\":\"\u66c7\u308a\"}]');\r\n\r\ndemo=# select * from weather;\r\n country |                   weather_data                    \r\n---------+---------------------------------------------------\r\n \u65e5\u672c    | {\"\u6771\u4eac\":\"\u6674\u308c\",\"\u5927\u962a\":\"\u96e8\",\"\u540d\u53e4\u5c4b\":\"\u66c7\u308a\"}\r\n \u65e5\u672c    | [{\"\u6771\u4eac\":\"\u6674\u308c\"},{\"\u5927\u962a\":\"\u96e8\"},{\"\u540d\u53e4\u5c4b\":\"\u66c7\u308a\"}]\r\n(2 rows)<\/pre>\n

\u7279\u306b\u3001\u8a70\u307e\u308b\u3053\u3068\u306a\u304f\u5229\u7528\u3067\u304d\u307e\u3057\u305f\u3002\u914d\u5217\u3082\u767b\u9332\u3067\u304d\u3001RFC 4627 \u306b\u6e96\u62e0\u3057\u3066\u3044\u308c\u3070\u554f\u984c\u306a\u3044\u3088\u3046\u3067\u3059\u3002<\/p>\n

\u307e\u305f\u3001RFC 4627 \u306b\u9055\u53cd\u3057\u3066\u3044\u308b\u3068\u3001\u30a8\u30e9\u30fc\u306b\u306a\u308a insert \u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u305b\u3093\u3067\u3057\u305f\u3002<\/p>\n

demo=# insert into weather values  ('\u65e5\u672c','{\"\u6771\u4eac\":\"\u6674\u308c\"');\r\nERROR:  invalid input syntax for type json\r\nLINE 1: insert into weather values  ('\u65e5\u672c','{\"\u6771\u4eac\":\"\u6674\u308c\"');\r\n                                            ^\r\nDETAIL:  The input string ended unexpectedly.\r\nCONTEXT:  JSON data, line 1: {\"\u6771\u4eac\":\"\u6674\u308c\"<\/pre>\n

JSON\u578b\u306e\u307e\u307e\u3001\u7d5e\u308a\u8fbc\u307f\u6761\u4ef6\u306f\u6307\u5b9a\u3067\u304d\u305a\u3001 like , order by \u306a\u3069\u3082\u30a8\u30e9\u30fc\u3068\u306a\u308a\u5931\u6557\u3057\u307e\u3057\u305f\u3002
\n\u307e\u305f\u3001JSON\u306e\u30ad\u30fc\u3084\u8981\u7d20\u3067\u306e\u691c\u7d22\u3082\u51fa\u6765\u306a\u3044\u3088\u3046\u3067\u3059\u3002<\/p>\n

demo=# select * from weather where weather_data = '{\"\u6771\u4eac\":\"\u6674\u308c\",\"\u5927\u962a\":\"\u96e8\",\"\u540d\u53e4\u5c4b\":\"\u66c7\u308a\"}';\r\nERROR:  operator does not exist: json = unknown\r\nLINE 1: select * from weather where weather_data = '{\"\u6771\u4eac\":\"\u6674\u308c\",\"...\u3000'\r\n                                                 ^\r\nHINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.\r\n\r\ndemo=# select * from weather where  weather_data like '%\u5927\u962a%';\r\nERROR:  operator does not exist: json ~~ unknown\r\nLINE 1: select * from weather where  weather_data like '%\u5927\u962a%';\r\n                                                  ^\r\nHINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.\r\n\r\ndemo=# select * from weather order by weather_data;\r\nERROR:  could not identify an ordering operator for type json\r\nLINE 1: select * from weather order by weather_data;\r\n                                       ^\r\nHINT:  Use an explicit ordering operator or modify the query.<\/pre>\n

\u4e00\u5ea6\u3001text\u578b\u306b\u30ad\u30e3\u30b9\u30c8\u3059\u308b\u3053\u3068\u3067\u3001\u7d5e\u308a\u8fbc\u307f\u6761\u4ef6\u3068\u3057\u3066\u5229\u7528\u3059\u308b\u3053\u3068\u306f\u51fa\u6765\u305d\u3046\u3067\u3059\u3002<\/p>\n

select * from weather where weather_data::text = '{\"\u6771\u4eac\":\"\u6674\u308c\",\"\u5927\u962a\":\"\u96e8\",\"\u540d\u53e4\u5c4b\":\"\u66c7\u308a\"}';\r\n country |                weather_data                 \r\n---------+---------------------------------------------\r\n \u65e5\u672c    | {\"\u6771\u4eac\":\"\u6674\u308c\",\"\u5927\u962a\":\"\u96e8\",\"\u540d\u53e4\u5c4b\":\"\u66c7\u308a\"}\r\n(1 rows)<\/pre>\n

\u95a2\u6570<\/h2>\n

JSON\u578b\u306e\u8ffd\u52a0\u306b\u5408\u308f\u305b\u3066\u3001array_to_json() row_to_json() \u306e\u95a2\u6570\u304c\u8ffd\u52a0\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n

http:\/\/www.postgresql.org\/docs\/9.2\/static\/functions-json.html<\/a><\/p>\n

\u8a66\u3057\u306b\u3001\u5229\u7528\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n

array_to_json()\u306f \u5024\u3092\u914d\u5217\u5f62\u5f0f\u306eJSON\u3068\u3057\u3066\u53d6\u5f97\u3067\u304d\u3001
\nrow_to_json()\u306f\u5024\u3092\u884c\u5f62\u5f0f\u306eJSON\u3068\u3057\u3066\u53d6\u5f97\u3067\u304d\u307e\u3059\u3002<\/p>\n

CREATE TABLE demo (col1 integer , col2 INTEGER);\r\ninsert into demo values (1,100);\r\ninsert into demo values (2,300);\r\n\r\ndemo=# select array_to_json(array_agg(demo)) from demo;\r\n                 array_to_json                 \r\n-----------------------------------------------\r\n [{\"col1\":1,\"col2\":100},{\"col1\":2,\"col2\":300}]\r\n(1 row)\r\n\r\ndemo=# select row_to_json(demo) from demo;\r\n      row_to_json      \r\n-----------------------\r\n {\"col1\":1,\"col2\":100}\r\n {\"col1\":2,\"col2\":300}\r\n(2 rows)<\/pre>\n

\u307e\u3068\u3081<\/h2>\n

\u8981\u7d20\u691c\u7d22\u304c\u3067\u304d\u306a\u3044\u306a\u3069\u3001\u6a19\u6e96\u6a5f\u80fd\u3067\u306e\u30c7\u30fc\u30bf\u30a2\u30af\u30bb\u30b9\u304c\u3082\u3046\u5c11\u3057\u4fbf\u5229\u306b\u306a\u308b\u3068\u5b09\u3057\u3044\u90e8\u5206\u304c\u3042\u308a\u307e\u3059\u304c\u3001
\n\u30c7\u30fc\u30bf\u304c\u6b63\u3057\u3044\u3053\u3068\u304c\u4fdd\u8a3c\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u3001\u53d6\u308a\u51fa\u3057\u3066\u304b\u3089\u30d1\u30fc\u30b9\u30a8\u30e9\u30fc\u3068\u306a\u308b\u3053\u3068\u304c\u306a\u304f\u306a\u308b\u306a\u3069\u3001
\n\u5229\u7528\u3059\u308b\u5834\u9762\u306f\u3042\u308a\u305d\u3046\u3067\u3059\u3002\u5b9f\u969b\u306e\u5229\u7528\u3067\u306f\u3001\u305d\u306e\u8fba\u308a\u3092\u6ce8\u610f\u3057\u306a\u304c\u3089\u5229\u7528\u3057\u3066\u307f\u308b\u306e\u3082\u826f\u3055\u305d\u3046\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u3053\u3093\u306b\u3061\u308f\u3001\u5317\u5ddd\u3067\u3059\u3002<\/p>\n

PostgreSQL 9.2 \u3067\u8ffd\u52a0\u3055\u308c\u305f JSON\u578b\u3092\u89e6\u3063\u3066\u307f\u307e\u3057\u305f\u306e\u3067\u7d39\u4ecb\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002
\u7d9a\u304d\u3092\u8aad\u3080...<\/a><\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18],"tags":[206,93,92],"_links":{"self":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/2309"}],"collection":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/comments?post=2309"}],"version-history":[{"count":1,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/2309\/revisions"}],"predecessor-version":[{"id":17944,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/2309\/revisions\/17944"}],"wp:attachment":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/media?parent=2309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/categories?post=2309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/tags?post=2309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}