json.sql 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. -- Strings.
  2. SELECT '""'::json; -- OK.
  3. SELECT $$''$$::json; -- ERROR, single quotes are not allowed
  4. SELECT '"abc"'::json; -- OK
  5. SELECT '"abc'::json; -- ERROR, quotes not closed
  6. SELECT '"abc
  7. def"'::json; -- ERROR, unescaped newline in string constant
  8. SELECT '"\n\"\\"'::json; -- OK, legal escapes
  9. SELECT '"\v"'::json; -- ERROR, not a valid JSON escape
  10. -- see json_encoding test for input with unicode escapes
  11. -- Numbers.
  12. SELECT '1'::json; -- OK
  13. SELECT '0'::json; -- OK
  14. SELECT '01'::json; -- ERROR, not valid according to JSON spec
  15. SELECT '0.1'::json; -- OK
  16. SELECT '9223372036854775808'::json; -- OK, even though it's too large for int8
  17. SELECT '1e100'::json; -- OK
  18. SELECT '1.3e100'::json; -- OK
  19. SELECT '1f2'::json; -- ERROR
  20. SELECT '0.x1'::json; -- ERROR
  21. SELECT '1.3ex100'::json; -- ERROR
  22. -- Arrays.
  23. SELECT '[]'::json; -- OK
  24. SELECT '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'::json; -- OK
  25. SELECT '[1,2]'::json; -- OK
  26. SELECT '[1,2,]'::json; -- ERROR, trailing comma
  27. SELECT '[1,2'::json; -- ERROR, no closing bracket
  28. SELECT '[1,[2]'::json; -- ERROR, no closing bracket
  29. -- Objects.
  30. SELECT '{}'::json; -- OK
  31. SELECT '{"abc"}'::json; -- ERROR, no value
  32. SELECT '{"abc":1}'::json; -- OK
  33. SELECT '{1:"abc"}'::json; -- ERROR, keys must be strings
  34. SELECT '{"abc",1}'::json; -- ERROR, wrong separator
  35. SELECT '{"abc"=1}'::json; -- ERROR, totally wrong separator
  36. SELECT '{"abc"::1}'::json; -- ERROR, another wrong separator
  37. SELECT '{"abc":1,"def":2,"ghi":[3,4],"hij":{"klm":5,"nop":[6]}}'::json; -- OK
  38. SELECT '{"abc":1:2}'::json; -- ERROR, colon in wrong spot
  39. SELECT '{"abc":1,3}'::json; -- ERROR, no value
  40. -- Recursion.
  41. SET max_stack_depth = '100kB';
  42. SELECT repeat('[', 10000)::json;
  43. SELECT repeat('{"a":', 10000)::json;
  44. RESET max_stack_depth;
  45. -- Miscellaneous stuff.
  46. SELECT 'true'::json; -- OK
  47. SELECT 'false'::json; -- OK
  48. SELECT 'null'::json; -- OK
  49. SELECT ' true '::json; -- OK, even with extra whitespace
  50. SELECT 'true false'::json; -- ERROR, too many values
  51. SELECT 'true, false'::json; -- ERROR, too many values
  52. SELECT 'truf'::json; -- ERROR, not a keyword
  53. SELECT 'trues'::json; -- ERROR, not a keyword
  54. SELECT ''::json; -- ERROR, no value
  55. SELECT ' '::json; -- ERROR, no value
  56. -- Multi-line JSON input to check ERROR reporting
  57. SELECT '{
  58. "one": 1,
  59. "two":"two",
  60. "three":
  61. true}'::json; -- OK
  62. SELECT '{
  63. "one": 1,
  64. "two":,"two", -- ERROR extraneous comma before field "two"
  65. "three":
  66. true}'::json;
  67. SELECT '{
  68. "one": 1,
  69. "two":"two",
  70. "averyveryveryveryveryveryveryveryveryverylongfieldname":}'::json;
  71. -- ERROR missing value for last field
  72. --constructors
  73. -- array_to_json
  74. SELECT array_to_json(array(select 1 as a));
  75. SELECT array_to_json(array_agg(q),false) from (select x as b, x * 2 as c from generate_series(1,3) x) q;
  76. SELECT array_to_json(array_agg(q),true) from (select x as b, x * 2 as c from generate_series(1,3) x) q;
  77. SELECT array_to_json(array_agg(q),false)
  78. FROM ( SELECT $$a$$ || x AS b, y AS c,
  79. ARRAY[ROW(x.*,ARRAY[1,2,3]),
  80. ROW(y.*,ARRAY[4,5,6])] AS z
  81. FROM generate_series(1,2) x,
  82. generate_series(4,5) y) q;
  83. SELECT array_to_json(array_agg(x),false) from generate_series(5,10) x;
  84. SELECT array_to_json('{{1,5},{99,100}}'::int[]);
  85. -- row_to_json
  86. SELECT row_to_json(row(1,'foo'));
  87. SELECT row_to_json(q)
  88. FROM (SELECT $$a$$ || x AS b,
  89. y AS c,
  90. ARRAY[ROW(x.*,ARRAY[1,2,3]),
  91. ROW(y.*,ARRAY[4,5,6])] AS z
  92. FROM generate_series(1,2) x,
  93. generate_series(4,5) y) q;
  94. SELECT row_to_json(q,true)
  95. FROM (SELECT $$a$$ || x AS b,
  96. y AS c,
  97. ARRAY[ROW(x.*,ARRAY[1,2,3]),
  98. ROW(y.*,ARRAY[4,5,6])] AS z
  99. FROM generate_series(1,2) x,
  100. generate_series(4,5) y) q;
  101. CREATE TEMP TABLE rows AS
  102. SELECT x, 'txt' || x as y
  103. FROM generate_series(1,3) AS x;
  104. SELECT row_to_json(q,true)
  105. FROM rows q;
  106. SELECT row_to_json(row((select array_agg(x) as d from generate_series(5,10) x)),false);
  107. -- anyarray column
  108. analyze rows;
  109. select attname, to_json(histogram_bounds) histogram_bounds
  110. from pg_stats
  111. where tablename = 'rows' and
  112. schemaname = pg_my_temp_schema()::regnamespace::text
  113. order by 1;
  114. -- to_json, timestamps
  115. select to_json(timestamp '2014-05-28 12:22:35.614298');
  116. BEGIN;
  117. SET LOCAL TIME ZONE 10.5;
  118. select to_json(timestamptz '2014-05-28 12:22:35.614298-04');
  119. SET LOCAL TIME ZONE -8;
  120. select to_json(timestamptz '2014-05-28 12:22:35.614298-04');
  121. COMMIT;
  122. select to_json(date '2014-05-28');
  123. select to_json(date 'Infinity');
  124. select to_json(date '-Infinity');
  125. select to_json(timestamp 'Infinity');
  126. select to_json(timestamp '-Infinity');
  127. select to_json(timestamptz 'Infinity');
  128. select to_json(timestamptz '-Infinity');
  129. --json_agg
  130. SELECT json_agg(q)
  131. FROM ( SELECT $$a$$ || x AS b, y AS c,
  132. ARRAY[ROW(x.*,ARRAY[1,2,3]),
  133. ROW(y.*,ARRAY[4,5,6])] AS z
  134. FROM generate_series(1,2) x,
  135. generate_series(4,5) y) q;
  136. SELECT json_agg(q ORDER BY x, y)
  137. FROM rows q;
  138. UPDATE rows SET x = NULL WHERE x = 1;
  139. SELECT json_agg(q ORDER BY x NULLS FIRST, y)
  140. FROM rows q;
  141. -- non-numeric output
  142. SELECT row_to_json(q)
  143. FROM (SELECT 'NaN'::float8 AS "float8field") q;
  144. SELECT row_to_json(q)
  145. FROM (SELECT 'Infinity'::float8 AS "float8field") q;
  146. SELECT row_to_json(q)
  147. FROM (SELECT '-Infinity'::float8 AS "float8field") q;
  148. -- json input
  149. SELECT row_to_json(q)
  150. FROM (SELECT '{"a":1,"b": [2,3,4,"d","e","f"],"c":{"p":1,"q":2}}'::json AS "jsonfield") q;
  151. -- json extraction functions
  152. CREATE TEMP TABLE test_json (
  153. json_type text,
  154. test_json json
  155. );
  156. INSERT INTO test_json VALUES
  157. ('scalar','"a scalar"'),
  158. ('array','["zero", "one","two",null,"four","five", [1,2,3],{"f1":9}]'),
  159. ('object','{"field1":"val1","field2":"val2","field3":null, "field4": 4, "field5": [1,2,3], "field6": {"f1":9}}');
  160. SELECT test_json -> 'x'
  161. FROM test_json
  162. WHERE json_type = 'scalar';
  163. SELECT test_json -> 'x'
  164. FROM test_json
  165. WHERE json_type = 'array';
  166. SELECT test_json -> 'x'
  167. FROM test_json
  168. WHERE json_type = 'object';
  169. SELECT test_json->'field2'
  170. FROM test_json
  171. WHERE json_type = 'object';
  172. SELECT test_json->>'field2'
  173. FROM test_json
  174. WHERE json_type = 'object';
  175. SELECT test_json -> 2
  176. FROM test_json
  177. WHERE json_type = 'scalar';
  178. SELECT test_json -> 2
  179. FROM test_json
  180. WHERE json_type = 'array';
  181. SELECT test_json -> -1
  182. FROM test_json
  183. WHERE json_type = 'array';
  184. SELECT test_json -> 2
  185. FROM test_json
  186. WHERE json_type = 'object';
  187. SELECT test_json->>2
  188. FROM test_json
  189. WHERE json_type = 'array';
  190. SELECT test_json ->> 6 FROM test_json WHERE json_type = 'array';
  191. SELECT test_json ->> 7 FROM test_json WHERE json_type = 'array';
  192. SELECT test_json ->> 'field4' FROM test_json WHERE json_type = 'object';
  193. SELECT test_json ->> 'field5' FROM test_json WHERE json_type = 'object';
  194. SELECT test_json ->> 'field6' FROM test_json WHERE json_type = 'object';
  195. SELECT json_object_keys(test_json)
  196. FROM test_json
  197. WHERE json_type = 'scalar';
  198. SELECT json_object_keys(test_json)
  199. FROM test_json
  200. WHERE json_type = 'array';
  201. SELECT json_object_keys(test_json)
  202. FROM test_json
  203. WHERE json_type = 'object';
  204. -- test extending object_keys resultset - initial resultset size is 256
  205. select count(*) from
  206. (select json_object_keys(json_object(array_agg(g)))
  207. from (select unnest(array['f'||n,n::text])as g
  208. from generate_series(1,300) as n) x ) y;
  209. -- nulls
  210. select (test_json->'field3') is null as expect_false
  211. from test_json
  212. where json_type = 'object';
  213. select (test_json->>'field3') is null as expect_true
  214. from test_json
  215. where json_type = 'object';
  216. select (test_json->3) is null as expect_false
  217. from test_json
  218. where json_type = 'array';
  219. select (test_json->>3) is null as expect_true
  220. from test_json
  221. where json_type = 'array';
  222. -- corner cases
  223. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> null::text;
  224. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> null::int;
  225. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 1;
  226. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> -1;
  227. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 'z';
  228. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> '';
  229. select '[{"b": "c"}, {"b": "cc"}]'::json -> 1;
  230. select '[{"b": "c"}, {"b": "cc"}]'::json -> 3;
  231. select '[{"b": "c"}, {"b": "cc"}]'::json -> 'z';
  232. select '{"a": "c", "b": null}'::json -> 'b';
  233. select '"foo"'::json -> 1;
  234. select '"foo"'::json -> 'z';
  235. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::text;
  236. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::int;
  237. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 1;
  238. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 'z';
  239. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> '';
  240. select '[{"b": "c"}, {"b": "cc"}]'::json ->> 1;
  241. select '[{"b": "c"}, {"b": "cc"}]'::json ->> 3;
  242. select '[{"b": "c"}, {"b": "cc"}]'::json ->> 'z';
  243. select '{"a": "c", "b": null}'::json ->> 'b';
  244. select '"foo"'::json ->> 1;
  245. select '"foo"'::json ->> 'z';
  246. -- array length
  247. SELECT json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]');
  248. SELECT json_array_length('[]');
  249. SELECT json_array_length('{"f1":1,"f2":[5,6]}');
  250. SELECT json_array_length('4');
  251. -- each
  252. select json_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null}');
  253. select * from json_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
  254. select json_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":"null"}');
  255. select * from json_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
  256. -- extract_path, extract_path_as_text
  257. select json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
  258. select json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
  259. select json_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
  260. select json_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
  261. select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
  262. select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
  263. select json_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
  264. select json_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
  265. -- extract_path nulls
  266. select json_extract_path('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') is null as expect_false;
  267. select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') is null as expect_true;
  268. select json_extract_path('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') is null as expect_false;
  269. select json_extract_path_text('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') is null as expect_true;
  270. -- extract_path operators
  271. select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f4','f6'];
  272. select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2'];
  273. select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2','0'];
  274. select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2','1'];
  275. select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f4','f6'];
  276. select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2'];
  277. select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','0'];
  278. select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','1'];
  279. -- corner cases for same
  280. select '{"a": {"b":{"c": "foo"}}}'::json #> '{}';
  281. select '[1,2,3]'::json #> '{}';
  282. select '"foo"'::json #> '{}';
  283. select '42'::json #> '{}';
  284. select 'null'::json #> '{}';
  285. select '{"a": {"b":{"c": "foo"}}}'::json #> array['a'];
  286. select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', null];
  287. select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', ''];
  288. select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b'];
  289. select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b','c'];
  290. select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b','c','d'];
  291. select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','z','c'];
  292. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #> array['a','1','b'];
  293. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #> array['a','z','b'];
  294. select '[{"b": "c"}, {"b": "cc"}]'::json #> array['1','b'];
  295. select '[{"b": "c"}, {"b": "cc"}]'::json #> array['z','b'];
  296. select '[{"b": "c"}, {"b": null}]'::json #> array['1','b'];
  297. select '"foo"'::json #> array['z'];
  298. select '42'::json #> array['f2'];
  299. select '42'::json #> array['0'];
  300. select '{"a": {"b":{"c": "foo"}}}'::json #>> '{}';
  301. select '[1,2,3]'::json #>> '{}';
  302. select '"foo"'::json #>> '{}';
  303. select '42'::json #>> '{}';
  304. select 'null'::json #>> '{}';
  305. select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a'];
  306. select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', null];
  307. select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', ''];
  308. select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b'];
  309. select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b','c'];
  310. select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b','c','d'];
  311. select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','z','c'];
  312. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #>> array['a','1','b'];
  313. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #>> array['a','z','b'];
  314. select '[{"b": "c"}, {"b": "cc"}]'::json #>> array['1','b'];
  315. select '[{"b": "c"}, {"b": "cc"}]'::json #>> array['z','b'];
  316. select '[{"b": "c"}, {"b": null}]'::json #>> array['1','b'];
  317. select '"foo"'::json #>> array['z'];
  318. select '42'::json #>> array['f2'];
  319. select '42'::json #>> array['0'];
  320. -- array_elements
  321. select json_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]');
  322. select * from json_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q;
  323. select json_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]');
  324. select * from json_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q;
  325. -- populate_record
  326. create type jpop as (a text, b int, c timestamp);
  327. CREATE DOMAIN js_int_not_null AS int NOT NULL;
  328. CREATE DOMAIN js_int_array_1d AS int[] CHECK(array_length(VALUE, 1) = 3);
  329. CREATE DOMAIN js_int_array_2d AS int[][] CHECK(array_length(VALUE, 2) = 3);
  330. create type j_unordered_pair as (x int, y int);
  331. create domain j_ordered_pair as j_unordered_pair check((value).x <= (value).y);
  332. CREATE TYPE jsrec AS (
  333. i int,
  334. ia _int4,
  335. ia1 int[],
  336. ia2 int[][],
  337. ia3 int[][][],
  338. ia1d js_int_array_1d,
  339. ia2d js_int_array_2d,
  340. t text,
  341. ta text[],
  342. c char(10),
  343. ca char(10)[],
  344. ts timestamp,
  345. js json,
  346. jsb jsonb,
  347. jsa json[],
  348. rec jpop,
  349. reca jpop[]
  350. );
  351. CREATE TYPE jsrec_i_not_null AS (
  352. i js_int_not_null
  353. );
  354. select * from json_populate_record(null::jpop,'{"a":"blurfl","x":43.2}') q;
  355. select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":"blurfl","x":43.2}') q;
  356. select * from json_populate_record(null::jpop,'{"a":"blurfl","x":43.2}') q;
  357. select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":"blurfl","x":43.2}') q;
  358. select * from json_populate_record(null::jpop,'{"a":[100,200,false],"x":43.2}') q;
  359. select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":[100,200,false],"x":43.2}') q;
  360. select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"c":[100,200,false],"x":43.2}') q;
  361. select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{}') q;
  362. SELECT i FROM json_populate_record(NULL::jsrec_i_not_null, '{"x": 43.2}') q;
  363. SELECT i FROM json_populate_record(NULL::jsrec_i_not_null, '{"i": null}') q;
  364. SELECT i FROM json_populate_record(NULL::jsrec_i_not_null, '{"i": 12345}') q;
  365. SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": null}') q;
  366. SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": 123}') q;
  367. SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": [1, "2", null, 4]}') q;
  368. SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": [[1, 2], [3, 4]]}') q;
  369. SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": [[1], 2]}') q;
  370. SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": [[1], [2, 3]]}') q;
  371. SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": "{1,2,3}"}') q;
  372. SELECT ia1 FROM json_populate_record(NULL::jsrec, '{"ia1": null}') q;
  373. SELECT ia1 FROM json_populate_record(NULL::jsrec, '{"ia1": 123}') q;
  374. SELECT ia1 FROM json_populate_record(NULL::jsrec, '{"ia1": [1, "2", null, 4]}') q;
  375. SELECT ia1 FROM json_populate_record(NULL::jsrec, '{"ia1": [[1, 2, 3]]}') q;
  376. SELECT ia1d FROM json_populate_record(NULL::jsrec, '{"ia1d": null}') q;
  377. SELECT ia1d FROM json_populate_record(NULL::jsrec, '{"ia1d": 123}') q;
  378. SELECT ia1d FROM json_populate_record(NULL::jsrec, '{"ia1d": [1, "2", null, 4]}') q;
  379. SELECT ia1d FROM json_populate_record(NULL::jsrec, '{"ia1d": [1, "2", null]}') q;
  380. SELECT ia2 FROM json_populate_record(NULL::jsrec, '{"ia2": [1, "2", null, 4]}') q;
  381. SELECT ia2 FROM json_populate_record(NULL::jsrec, '{"ia2": [[1, 2], [null, 4]]}') q;
  382. SELECT ia2 FROM json_populate_record(NULL::jsrec, '{"ia2": [[], []]}') q;
  383. SELECT ia2 FROM json_populate_record(NULL::jsrec, '{"ia2": [[1, 2], [3]]}') q;
  384. SELECT ia2 FROM json_populate_record(NULL::jsrec, '{"ia2": [[1, 2], 3, 4]}') q;
  385. SELECT ia2d FROM json_populate_record(NULL::jsrec, '{"ia2d": [[1, "2"], [null, 4]]}') q;
  386. SELECT ia2d FROM json_populate_record(NULL::jsrec, '{"ia2d": [[1, "2", 3], [null, 5, 6]]}') q;
  387. SELECT ia3 FROM json_populate_record(NULL::jsrec, '{"ia3": [1, "2", null, 4]}') q;
  388. SELECT ia3 FROM json_populate_record(NULL::jsrec, '{"ia3": [[1, 2], [null, 4]]}') q;
  389. SELECT ia3 FROM json_populate_record(NULL::jsrec, '{"ia3": [ [[], []], [[], []], [[], []] ]}') q;
  390. SELECT ia3 FROM json_populate_record(NULL::jsrec, '{"ia3": [ [[1, 2]], [[3, 4]] ]}') q;
  391. SELECT ia3 FROM json_populate_record(NULL::jsrec, '{"ia3": [ [[1, 2], [3, 4]], [[5, 6], [7, 8]] ]}') q;
  392. SELECT ia3 FROM json_populate_record(NULL::jsrec, '{"ia3": [ [[1, 2], [3, 4]], [[5, 6], [7, 8], [9, 10]] ]}') q;
  393. SELECT ta FROM json_populate_record(NULL::jsrec, '{"ta": null}') q;
  394. SELECT ta FROM json_populate_record(NULL::jsrec, '{"ta": 123}') q;
  395. SELECT ta FROM json_populate_record(NULL::jsrec, '{"ta": [1, "2", null, 4]}') q;
  396. SELECT ta FROM json_populate_record(NULL::jsrec, '{"ta": [[1, 2, 3], {"k": "v"}]}') q;
  397. SELECT c FROM json_populate_record(NULL::jsrec, '{"c": null}') q;
  398. SELECT c FROM json_populate_record(NULL::jsrec, '{"c": "aaa"}') q;
  399. SELECT c FROM json_populate_record(NULL::jsrec, '{"c": "aaaaaaaaaa"}') q;
  400. SELECT c FROM json_populate_record(NULL::jsrec, '{"c": "aaaaaaaaaaaaa"}') q;
  401. SELECT ca FROM json_populate_record(NULL::jsrec, '{"ca": null}') q;
  402. SELECT ca FROM json_populate_record(NULL::jsrec, '{"ca": 123}') q;
  403. SELECT ca FROM json_populate_record(NULL::jsrec, '{"ca": [1, "2", null, 4]}') q;
  404. SELECT ca FROM json_populate_record(NULL::jsrec, '{"ca": ["aaaaaaaaaaaaaaaa"]}') q;
  405. SELECT ca FROM json_populate_record(NULL::jsrec, '{"ca": [[1, 2, 3], {"k": "v"}]}') q;
  406. SELECT js FROM json_populate_record(NULL::jsrec, '{"js": null}') q;
  407. SELECT js FROM json_populate_record(NULL::jsrec, '{"js": true}') q;
  408. SELECT js FROM json_populate_record(NULL::jsrec, '{"js": 123.45}') q;
  409. SELECT js FROM json_populate_record(NULL::jsrec, '{"js": "123.45"}') q;
  410. SELECT js FROM json_populate_record(NULL::jsrec, '{"js": "abc"}') q;
  411. SELECT js FROM json_populate_record(NULL::jsrec, '{"js": [123, "123", null, {"key": "value"}]}') q;
  412. SELECT js FROM json_populate_record(NULL::jsrec, '{"js": {"a": "bbb", "b": null, "c": 123.45}}') q;
  413. SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": null}') q;
  414. SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": true}') q;
  415. SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": 123.45}') q;
  416. SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": "123.45"}') q;
  417. SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": "abc"}') q;
  418. SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": [123, "123", null, {"key": "value"}]}') q;
  419. SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": {"a": "bbb", "b": null, "c": 123.45}}') q;
  420. SELECT jsa FROM json_populate_record(NULL::jsrec, '{"jsa": null}') q;
  421. SELECT jsa FROM json_populate_record(NULL::jsrec, '{"jsa": 123}') q;
  422. SELECT jsa FROM json_populate_record(NULL::jsrec, '{"jsa": [1, "2", null, 4]}') q;
  423. SELECT jsa FROM json_populate_record(NULL::jsrec, '{"jsa": ["aaa", null, [1, 2, "3", {}], { "k" : "v" }]}') q;
  424. SELECT rec FROM json_populate_record(NULL::jsrec, '{"rec": 123}') q;
  425. SELECT rec FROM json_populate_record(NULL::jsrec, '{"rec": [1, 2]}') q;
  426. SELECT rec FROM json_populate_record(NULL::jsrec, '{"rec": {"a": "abc", "c": "01.02.2003", "x": 43.2}}') q;
  427. SELECT rec FROM json_populate_record(NULL::jsrec, '{"rec": "(abc,42,01.02.2003)"}') q;
  428. SELECT reca FROM json_populate_record(NULL::jsrec, '{"reca": 123}') q;
  429. SELECT reca FROM json_populate_record(NULL::jsrec, '{"reca": [1, 2]}') q;
  430. SELECT reca FROM json_populate_record(NULL::jsrec, '{"reca": [{"a": "abc", "b": 456}, null, {"c": "01.02.2003", "x": 43.2}]}') q;
  431. SELECT reca FROM json_populate_record(NULL::jsrec, '{"reca": ["(abc,42,01.02.2003)"]}') q;
  432. SELECT reca FROM json_populate_record(NULL::jsrec, '{"reca": "{\"(abc,42,01.02.2003)\"}"}') q;
  433. SELECT rec FROM json_populate_record(
  434. row(NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
  435. row('x',3,'2012-12-31 15:30:56')::jpop,NULL)::jsrec,
  436. '{"rec": {"a": "abc", "c": "01.02.2003", "x": 43.2}}'
  437. ) q;
  438. -- anonymous record type
  439. SELECT json_populate_record(null::record, '{"x": 0, "y": 1}');
  440. SELECT json_populate_record(row(1,2), '{"f1": 0, "f2": 1}');
  441. SELECT * FROM
  442. json_populate_record(null::record, '{"x": 776}') AS (x int, y int);
  443. -- composite domain
  444. SELECT json_populate_record(null::j_ordered_pair, '{"x": 0, "y": 1}');
  445. SELECT json_populate_record(row(1,2)::j_ordered_pair, '{"x": 0}');
  446. SELECT json_populate_record(row(1,2)::j_ordered_pair, '{"x": 1, "y": 0}');
  447. -- populate_recordset
  448. select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
  449. select * from json_populate_recordset(row('def',99,null)::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
  450. select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
  451. select * from json_populate_recordset(row('def',99,null)::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
  452. select * from json_populate_recordset(row('def',99,null)::jpop,'[{"a":[100,200,300],"x":43.2},{"a":{"z":true},"b":3,"c":"2012-01-20 10:42:53"}]') q;
  453. select * from json_populate_recordset(row('def',99,null)::jpop,'[{"c":[100,200,300],"x":43.2},{"a":{"z":true},"b":3,"c":"2012-01-20 10:42:53"}]') q;
  454. create type jpop2 as (a int, b json, c int, d int);
  455. select * from json_populate_recordset(null::jpop2, '[{"a":2,"c":3,"b":{"z":4},"d":6}]') q;
  456. select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
  457. select * from json_populate_recordset(row('def',99,null)::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
  458. select * from json_populate_recordset(row('def',99,null)::jpop,'[{"a":[100,200,300],"x":43.2},{"a":{"z":true},"b":3,"c":"2012-01-20 10:42:53"}]') q;
  459. -- anonymous record type
  460. SELECT json_populate_recordset(null::record, '[{"x": 0, "y": 1}]');
  461. SELECT json_populate_recordset(row(1,2), '[{"f1": 0, "f2": 1}]');
  462. SELECT i, json_populate_recordset(row(i,50), '[{"f1":"42"},{"f2":"43"}]')
  463. FROM (VALUES (1),(2)) v(i);
  464. SELECT * FROM
  465. json_populate_recordset(null::record, '[{"x": 776}]') AS (x int, y int);
  466. -- empty array is a corner case
  467. SELECT json_populate_recordset(null::record, '[]');
  468. SELECT json_populate_recordset(row(1,2), '[]');
  469. SELECT * FROM json_populate_recordset(NULL::jpop,'[]') q;
  470. SELECT * FROM
  471. json_populate_recordset(null::record, '[]') AS (x int, y int);
  472. -- composite domain
  473. SELECT json_populate_recordset(null::j_ordered_pair, '[{"x": 0, "y": 1}]');
  474. SELECT json_populate_recordset(row(1,2)::j_ordered_pair, '[{"x": 0}, {"y": 3}]');
  475. SELECT json_populate_recordset(row(1,2)::j_ordered_pair, '[{"x": 1, "y": 0}]');
  476. -- negative cases where the wrong record type is supplied
  477. select * from json_populate_recordset(row(0::int),'[{"a":"1","b":"2"},{"a":"3"}]') q (a text, b text);
  478. select * from json_populate_recordset(row(0::int,0::int),'[{"a":"1","b":"2"},{"a":"3"}]') q (a text, b text);
  479. select * from json_populate_recordset(row(0::int,0::int,0::int),'[{"a":"1","b":"2"},{"a":"3"}]') q (a text, b text);
  480. select * from json_populate_recordset(row(1000000000::int,50::int),'[{"b":"2"},{"a":"3"}]') q (a text, b text);
  481. -- test type info caching in json_populate_record()
  482. CREATE TEMP TABLE jspoptest (js json);
  483. INSERT INTO jspoptest
  484. SELECT '{
  485. "jsa": [1, "2", null, 4],
  486. "rec": {"a": "abc", "c": "01.02.2003", "x": 43.2},
  487. "reca": [{"a": "abc", "b": 456}, null, {"c": "01.02.2003", "x": 43.2}]
  488. }'::json
  489. FROM generate_series(1, 3);
  490. SELECT (json_populate_record(NULL::jsrec, js)).* FROM jspoptest;
  491. DROP TYPE jsrec;
  492. DROP TYPE jsrec_i_not_null;
  493. DROP DOMAIN js_int_not_null;
  494. DROP DOMAIN js_int_array_1d;
  495. DROP DOMAIN js_int_array_2d;
  496. DROP DOMAIN j_ordered_pair;
  497. DROP TYPE j_unordered_pair;
  498. --json_typeof() function
  499. select value, json_typeof(value)
  500. from (values (json '123.4'),
  501. (json '-1'),
  502. (json '"foo"'),
  503. (json 'true'),
  504. (json 'false'),
  505. (json 'null'),
  506. (json '[1, 2, 3]'),
  507. (json '[]'),
  508. (json '{"x":"foo", "y":123}'),
  509. (json '{}'),
  510. (NULL::json))
  511. as data(value);
  512. -- json_build_array, json_build_object, json_object_agg
  513. SELECT json_build_array('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
  514. SELECT json_build_array('a', NULL); -- ok
  515. SELECT json_build_array(VARIADIC NULL::text[]); -- ok
  516. SELECT json_build_array(VARIADIC '{}'::text[]); -- ok
  517. SELECT json_build_array(VARIADIC '{a,b,c}'::text[]); -- ok
  518. SELECT json_build_array(VARIADIC ARRAY['a', NULL]::text[]); -- ok
  519. SELECT json_build_array(VARIADIC '{1,2,3,4}'::text[]); -- ok
  520. SELECT json_build_array(VARIADIC '{1,2,3,4}'::int[]); -- ok
  521. SELECT json_build_array(VARIADIC '{{1,4},{2,5},{3,6}}'::int[][]); -- ok
  522. SELECT json_build_object('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
  523. SELECT json_build_object(
  524. 'a', json_build_object('b',false,'c',99),
  525. 'd', json_build_object('e',array[9,8,7]::int[],
  526. 'f', (select row_to_json(r) from ( select relkind, oid::regclass as name from pg_class where relname = 'pg_class') r)));
  527. SELECT json_build_object('{a,b,c}'::text[]); -- error
  528. SELECT json_build_object('{a,b,c}'::text[], '{d,e,f}'::text[]); -- error, key cannot be array
  529. SELECT json_build_object('a', 'b', 'c'); -- error
  530. SELECT json_build_object(NULL, 'a'); -- error, key cannot be NULL
  531. SELECT json_build_object('a', NULL); -- ok
  532. SELECT json_build_object(VARIADIC NULL::text[]); -- ok
  533. SELECT json_build_object(VARIADIC '{}'::text[]); -- ok
  534. SELECT json_build_object(VARIADIC '{a,b,c}'::text[]); -- error
  535. SELECT json_build_object(VARIADIC ARRAY['a', NULL]::text[]); -- ok
  536. SELECT json_build_object(VARIADIC ARRAY[NULL, 'a']::text[]); -- error, key cannot be NULL
  537. SELECT json_build_object(VARIADIC '{1,2,3,4}'::text[]); -- ok
  538. SELECT json_build_object(VARIADIC '{1,2,3,4}'::int[]); -- ok
  539. SELECT json_build_object(VARIADIC '{{1,4},{2,5},{3,6}}'::int[][]); -- ok
  540. -- empty objects/arrays
  541. SELECT json_build_array();
  542. SELECT json_build_object();
  543. -- make sure keys are quoted
  544. SELECT json_build_object(1,2);
  545. -- keys must be scalar and not null
  546. SELECT json_build_object(null,2);
  547. SELECT json_build_object(r,2) FROM (SELECT 1 AS a, 2 AS b) r;
  548. SELECT json_build_object(json '{"a":1,"b":2}', 3);
  549. SELECT json_build_object('{1,2,3}'::int[], 3);
  550. CREATE TEMP TABLE foo (serial_num int, name text, type text);
  551. INSERT INTO foo VALUES (847001,'t15','GE1043');
  552. INSERT INTO foo VALUES (847002,'t16','GE1043');
  553. INSERT INTO foo VALUES (847003,'sub-alpha','GESS90');
  554. SELECT json_build_object('turbines',json_object_agg(serial_num,json_build_object('name',name,'type',type)))
  555. FROM foo;
  556. SELECT json_object_agg(name, type) FROM foo;
  557. INSERT INTO foo VALUES (999999, NULL, 'bar');
  558. SELECT json_object_agg(name, type) FROM foo;
  559. -- json_object
  560. -- empty object, one dimension
  561. SELECT json_object('{}');
  562. -- empty object, two dimensions
  563. SELECT json_object('{}', '{}');
  564. -- one dimension
  565. SELECT json_object('{a,1,b,2,3,NULL,"d e f","a b c"}');
  566. -- same but with two dimensions
  567. SELECT json_object('{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}');
  568. -- odd number error
  569. SELECT json_object('{a,b,c}');
  570. -- one column error
  571. SELECT json_object('{{a},{b}}');
  572. -- too many columns error
  573. SELECT json_object('{{a,b,c},{b,c,d}}');
  574. -- too many dimensions error
  575. SELECT json_object('{{{a,b},{c,d}},{{b,c},{d,e}}}');
  576. --two argument form of json_object
  577. select json_object('{a,b,c,"d e f"}','{1,2,3,"a b c"}');
  578. -- too many dimensions
  579. SELECT json_object('{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}', '{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}');
  580. -- mismatched dimensions
  581. select json_object('{a,b,c,"d e f",g}','{1,2,3,"a b c"}');
  582. select json_object('{a,b,c,"d e f"}','{1,2,3,"a b c",g}');
  583. -- null key error
  584. select json_object('{a,b,NULL,"d e f"}','{1,2,3,"a b c"}');
  585. -- empty key is allowed
  586. select json_object('{a,b,"","d e f"}','{1,2,3,"a b c"}');
  587. -- json_to_record and json_to_recordset
  588. select * from json_to_record('{"a":1,"b":"foo","c":"bar"}')
  589. as x(a int, b text, d text);
  590. select * from json_to_recordset('[{"a":1,"b":"foo","d":false},{"a":2,"b":"bar","c":true}]')
  591. as x(a int, b text, c boolean);
  592. select * from json_to_recordset('[{"a":1,"b":{"d":"foo"},"c":true},{"a":2,"c":false,"b":{"d":"bar"}}]')
  593. as x(a int, b json, c boolean);
  594. select *, c is null as c_is_null
  595. from json_to_record('{"a":1, "b":{"c":16, "d":2}, "x":8, "ca": ["1 2", 3], "ia": [[1,2],[3,4]], "r": {"a": "aaa", "b": 123}}'::json)
  596. as t(a int, b json, c text, x int, ca char(5)[], ia int[][], r jpop);
  597. select *, c is null as c_is_null
  598. from json_to_recordset('[{"a":1, "b":{"c":16, "d":2}, "x":8}]'::json)
  599. as t(a int, b json, c text, x int);
  600. select * from json_to_record('{"ia": null}') as x(ia _int4);
  601. select * from json_to_record('{"ia": 123}') as x(ia _int4);
  602. select * from json_to_record('{"ia": [1, "2", null, 4]}') as x(ia _int4);
  603. select * from json_to_record('{"ia": [[1, 2], [3, 4]]}') as x(ia _int4);
  604. select * from json_to_record('{"ia": [[1], 2]}') as x(ia _int4);
  605. select * from json_to_record('{"ia": [[1], [2, 3]]}') as x(ia _int4);
  606. select * from json_to_record('{"ia2": [1, 2, 3]}') as x(ia2 int[][]);
  607. select * from json_to_record('{"ia2": [[1, 2], [3, 4]]}') as x(ia2 int4[][]);
  608. select * from json_to_record('{"ia2": [[[1], [2], [3]]]}') as x(ia2 int4[][]);
  609. select * from json_to_record('{"out": {"key": 1}}') as x(out json);
  610. select * from json_to_record('{"out": [{"key": 1}]}') as x(out json);
  611. select * from json_to_record('{"out": "{\"key\": 1}"}') as x(out json);
  612. select * from json_to_record('{"out": {"key": 1}}') as x(out jsonb);
  613. select * from json_to_record('{"out": [{"key": 1}]}') as x(out jsonb);
  614. select * from json_to_record('{"out": "{\"key\": 1}"}') as x(out jsonb);
  615. -- json_strip_nulls
  616. select json_strip_nulls(null);
  617. select json_strip_nulls('1');
  618. select json_strip_nulls('"a string"');
  619. select json_strip_nulls('null');
  620. select json_strip_nulls('[1,2,null,3,4]');
  621. select json_strip_nulls('{"a":1,"b":null,"c":[2,null,3],"d":{"e":4,"f":null}}');
  622. select json_strip_nulls('[1,{"a":1,"b":null,"c":2},3]');
  623. -- an empty object is not null and should not be stripped
  624. select json_strip_nulls('{"a": {"b": null, "c": null}, "d": {} }');
  625. -- json to tsvector
  626. select to_tsvector('{"a": "aaa bbb ddd ccc", "b": ["eee fff ggg"], "c": {"d": "hhh iii"}}'::json);
  627. -- json to tsvector with config
  628. select to_tsvector('simple', '{"a": "aaa bbb ddd ccc", "b": ["eee fff ggg"], "c": {"d": "hhh iii"}}'::json);
  629. -- json to tsvector with stop words
  630. select to_tsvector('english', '{"a": "aaa in bbb ddd ccc", "b": ["the eee fff ggg"], "c": {"d": "hhh. iii"}}'::json);
  631. -- json to tsvector with numeric values
  632. select to_tsvector('english', '{"a": "aaa in bbb ddd ccc", "b": 123, "c": 456}'::json);
  633. -- json_to_tsvector
  634. select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '"all"');
  635. select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '"key"');
  636. select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '"string"');
  637. select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '"numeric"');
  638. select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '"boolean"');
  639. select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '["string", "numeric"]');
  640. select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '"all"');
  641. select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '"key"');
  642. select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '"string"');
  643. select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '"numeric"');
  644. select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '"boolean"');
  645. select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '["string", "numeric"]');
  646. -- to_tsvector corner cases
  647. select to_tsvector('""'::json);
  648. select to_tsvector('{}'::json);
  649. select to_tsvector('[]'::json);
  650. select to_tsvector('null'::json);
  651. -- json_to_tsvector corner cases
  652. select json_to_tsvector('""'::json, '"all"');
  653. select json_to_tsvector('{}'::json, '"all"');
  654. select json_to_tsvector('[]'::json, '"all"');
  655. select json_to_tsvector('null'::json, '"all"');
  656. select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '""');
  657. select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '{}');
  658. select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '[]');
  659. select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, 'null');
  660. select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '["all", null]');
  661. -- ts_headline for json
  662. select ts_headline('{"a": "aaa bbb", "b": {"c": "ccc ddd fff", "c1": "ccc1 ddd1"}, "d": ["ggg hhh", "iii jjj"]}'::json, tsquery('bbb & ddd & hhh'));
  663. select ts_headline('english', '{"a": "aaa bbb", "b": {"c": "ccc ddd fff"}, "d": ["ggg hhh", "iii jjj"]}'::json, tsquery('bbb & ddd & hhh'));
  664. select ts_headline('{"a": "aaa bbb", "b": {"c": "ccc ddd fff", "c1": "ccc1 ddd1"}, "d": ["ggg hhh", "iii jjj"]}'::json, tsquery('bbb & ddd & hhh'), 'StartSel = <, StopSel = >');
  665. select ts_headline('english', '{"a": "aaa bbb", "b": {"c": "ccc ddd fff", "c1": "ccc1 ddd1"}, "d": ["ggg hhh", "iii jjj"]}'::json, tsquery('bbb & ddd & hhh'), 'StartSel = <, StopSel = >');
  666. -- corner cases for ts_headline with json
  667. select ts_headline('null'::json, tsquery('aaa & bbb'));
  668. select ts_headline('{}'::json, tsquery('aaa & bbb'));
  669. select ts_headline('[]'::json, tsquery('aaa & bbb'));