json.out 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. -- Strings.
  2. SELECT '""'::json; -- OK.
  3. json
  4. ------
  5. ""
  6. (1 row)
  7. SELECT $$''$$::json; -- ERROR, single quotes are not allowed
  8. ERROR: invalid input syntax for type json
  9. LINE 1: SELECT $$''$$::json;
  10. ^
  11. DETAIL: Token "'" is invalid.
  12. CONTEXT: JSON data, line 1: '...
  13. SELECT '"abc"'::json; -- OK
  14. json
  15. -------
  16. "abc"
  17. (1 row)
  18. SELECT '"abc'::json; -- ERROR, quotes not closed
  19. ERROR: invalid input syntax for type json
  20. LINE 1: SELECT '"abc'::json;
  21. ^
  22. DETAIL: Token ""abc" is invalid.
  23. CONTEXT: JSON data, line 1: "abc
  24. SELECT '"abc
  25. def"'::json; -- ERROR, unescaped newline in string constant
  26. ERROR: invalid input syntax for type json
  27. LINE 1: SELECT '"abc
  28. ^
  29. DETAIL: Character with value 0x0a must be escaped.
  30. CONTEXT: JSON data, line 1: "abc
  31. SELECT '"\n\"\\"'::json; -- OK, legal escapes
  32. json
  33. ----------
  34. "\n\"\\"
  35. (1 row)
  36. SELECT '"\v"'::json; -- ERROR, not a valid JSON escape
  37. ERROR: invalid input syntax for type json
  38. LINE 1: SELECT '"\v"'::json;
  39. ^
  40. DETAIL: Escape sequence "\v" is invalid.
  41. CONTEXT: JSON data, line 1: "\v...
  42. -- see json_encoding test for input with unicode escapes
  43. -- Numbers.
  44. SELECT '1'::json; -- OK
  45. json
  46. ------
  47. 1
  48. (1 row)
  49. SELECT '0'::json; -- OK
  50. json
  51. ------
  52. 0
  53. (1 row)
  54. SELECT '01'::json; -- ERROR, not valid according to JSON spec
  55. ERROR: invalid input syntax for type json
  56. LINE 1: SELECT '01'::json;
  57. ^
  58. DETAIL: Token "01" is invalid.
  59. CONTEXT: JSON data, line 1: 01
  60. SELECT '0.1'::json; -- OK
  61. json
  62. ------
  63. 0.1
  64. (1 row)
  65. SELECT '9223372036854775808'::json; -- OK, even though it's too large for int8
  66. json
  67. ---------------------
  68. 9223372036854775808
  69. (1 row)
  70. SELECT '1e100'::json; -- OK
  71. json
  72. -------
  73. 1e100
  74. (1 row)
  75. SELECT '1.3e100'::json; -- OK
  76. json
  77. ---------
  78. 1.3e100
  79. (1 row)
  80. SELECT '1f2'::json; -- ERROR
  81. ERROR: invalid input syntax for type json
  82. LINE 1: SELECT '1f2'::json;
  83. ^
  84. DETAIL: Token "1f2" is invalid.
  85. CONTEXT: JSON data, line 1: 1f2
  86. SELECT '0.x1'::json; -- ERROR
  87. ERROR: invalid input syntax for type json
  88. LINE 1: SELECT '0.x1'::json;
  89. ^
  90. DETAIL: Token "0.x1" is invalid.
  91. CONTEXT: JSON data, line 1: 0.x1
  92. SELECT '1.3ex100'::json; -- ERROR
  93. ERROR: invalid input syntax for type json
  94. LINE 1: SELECT '1.3ex100'::json;
  95. ^
  96. DETAIL: Token "1.3ex100" is invalid.
  97. CONTEXT: JSON data, line 1: 1.3ex100
  98. -- Arrays.
  99. SELECT '[]'::json; -- OK
  100. json
  101. ------
  102. []
  103. (1 row)
  104. SELECT '[1,2]'::json; -- OK
  105. json
  106. -------
  107. [1,2]
  108. (1 row)
  109. SELECT '[1,2,]'::json; -- ERROR, trailing comma
  110. ERROR: invalid input syntax for type json
  111. LINE 1: SELECT '[1,2,]'::json;
  112. ^
  113. DETAIL: Expected JSON value, but found "]".
  114. CONTEXT: JSON data, line 1: [1,2,]
  115. SELECT '[1,2'::json; -- ERROR, no closing bracket
  116. ERROR: invalid input syntax for type json
  117. LINE 1: SELECT '[1,2'::json;
  118. ^
  119. DETAIL: The input string ended unexpectedly.
  120. CONTEXT: JSON data, line 1: [1,2
  121. SELECT '[1,[2]'::json; -- ERROR, no closing bracket
  122. ERROR: invalid input syntax for type json
  123. LINE 1: SELECT '[1,[2]'::json;
  124. ^
  125. DETAIL: The input string ended unexpectedly.
  126. CONTEXT: JSON data, line 1: [1,[2]
  127. -- Objects.
  128. SELECT '{}'::json; -- OK
  129. json
  130. ------
  131. {}
  132. (1 row)
  133. SELECT '{"abc"}'::json; -- ERROR, no value
  134. ERROR: invalid input syntax for type json
  135. LINE 1: SELECT '{"abc"}'::json;
  136. ^
  137. DETAIL: Expected ":", but found "}".
  138. CONTEXT: JSON data, line 1: {"abc"}
  139. SELECT '{"abc":1}'::json; -- OK
  140. json
  141. -----------
  142. {"abc":1}
  143. (1 row)
  144. SELECT '{1:"abc"}'::json; -- ERROR, keys must be strings
  145. ERROR: invalid input syntax for type json
  146. LINE 1: SELECT '{1:"abc"}'::json;
  147. ^
  148. DETAIL: Expected string or "}", but found "1".
  149. CONTEXT: JSON data, line 1: {1...
  150. SELECT '{"abc",1}'::json; -- ERROR, wrong separator
  151. ERROR: invalid input syntax for type json
  152. LINE 1: SELECT '{"abc",1}'::json;
  153. ^
  154. DETAIL: Expected ":", but found ",".
  155. CONTEXT: JSON data, line 1: {"abc",...
  156. SELECT '{"abc"=1}'::json; -- ERROR, totally wrong separator
  157. ERROR: invalid input syntax for type json
  158. LINE 1: SELECT '{"abc"=1}'::json;
  159. ^
  160. DETAIL: Token "=" is invalid.
  161. CONTEXT: JSON data, line 1: {"abc"=...
  162. SELECT '{"abc"::1}'::json; -- ERROR, another wrong separator
  163. ERROR: invalid input syntax for type json
  164. LINE 1: SELECT '{"abc"::1}'::json;
  165. ^
  166. DETAIL: Expected JSON value, but found ":".
  167. CONTEXT: JSON data, line 1: {"abc"::...
  168. SELECT '{"abc":1,"def":2,"ghi":[3,4],"hij":{"klm":5,"nop":[6]}}'::json; -- OK
  169. json
  170. ---------------------------------------------------------
  171. {"abc":1,"def":2,"ghi":[3,4],"hij":{"klm":5,"nop":[6]}}
  172. (1 row)
  173. SELECT '{"abc":1:2}'::json; -- ERROR, colon in wrong spot
  174. ERROR: invalid input syntax for type json
  175. LINE 1: SELECT '{"abc":1:2}'::json;
  176. ^
  177. DETAIL: Expected "," or "}", but found ":".
  178. CONTEXT: JSON data, line 1: {"abc":1:...
  179. SELECT '{"abc":1,3}'::json; -- ERROR, no value
  180. ERROR: invalid input syntax for type json
  181. LINE 1: SELECT '{"abc":1,3}'::json;
  182. ^
  183. DETAIL: Expected string, but found "3".
  184. CONTEXT: JSON data, line 1: {"abc":1,3...
  185. -- Miscellaneous stuff.
  186. SELECT 'true'::json; -- OK
  187. json
  188. ------
  189. true
  190. (1 row)
  191. SELECT 'false'::json; -- OK
  192. json
  193. -------
  194. false
  195. (1 row)
  196. SELECT 'null'::json; -- OK
  197. json
  198. ------
  199. null
  200. (1 row)
  201. SELECT ' true '::json; -- OK, even with extra whitespace
  202. json
  203. --------
  204. true
  205. (1 row)
  206. SELECT 'true false'::json; -- ERROR, too many values
  207. ERROR: invalid input syntax for type json
  208. LINE 1: SELECT 'true false'::json;
  209. ^
  210. DETAIL: Expected end of input, but found "false".
  211. CONTEXT: JSON data, line 1: true false
  212. SELECT 'true, false'::json; -- ERROR, too many values
  213. ERROR: invalid input syntax for type json
  214. LINE 1: SELECT 'true, false'::json;
  215. ^
  216. DETAIL: Expected end of input, but found ",".
  217. CONTEXT: JSON data, line 1: true,...
  218. SELECT 'truf'::json; -- ERROR, not a keyword
  219. ERROR: invalid input syntax for type json
  220. LINE 1: SELECT 'truf'::json;
  221. ^
  222. DETAIL: Token "truf" is invalid.
  223. CONTEXT: JSON data, line 1: truf
  224. SELECT 'trues'::json; -- ERROR, not a keyword
  225. ERROR: invalid input syntax for type json
  226. LINE 1: SELECT 'trues'::json;
  227. ^
  228. DETAIL: Token "trues" is invalid.
  229. CONTEXT: JSON data, line 1: trues
  230. SELECT ''::json; -- ERROR, no value
  231. ERROR: invalid input syntax for type json
  232. LINE 1: SELECT ''::json;
  233. ^
  234. DETAIL: The input string ended unexpectedly.
  235. CONTEXT: JSON data, line 1:
  236. SELECT ' '::json; -- ERROR, no value
  237. ERROR: invalid input syntax for type json
  238. LINE 1: SELECT ' '::json;
  239. ^
  240. DETAIL: The input string ended unexpectedly.
  241. CONTEXT: JSON data, line 1:
  242. SELECT '{
  243. "one": 1,
  244. "two":,"two", -- ERROR extraneous comma before field "two"
  245. "three":
  246. true}'::json;
  247. ERROR: invalid input syntax for type json
  248. LINE 1: SELECT '{
  249. ^
  250. DETAIL: Expected JSON value, but found ",".
  251. CONTEXT: JSON data, line 3: "two":,...
  252. SELECT '{
  253. "one": 1,
  254. "two":"two",
  255. "averyveryveryveryveryveryveryveryveryverylongfieldname":}'::json;
  256. ERROR: invalid input syntax for type json
  257. LINE 1: SELECT '{
  258. ^
  259. DETAIL: Expected JSON value, but found "}".
  260. CONTEXT: JSON data, line 4: ...yveryveryveryveryveryveryveryverylongfieldname":}
  261. -- ERROR missing value for last field
  262. --constructors
  263. -- array_to_json
  264. SELECT array_to_json(array(select 1 as a));
  265. array_to_json
  266. ---------------
  267. [1]
  268. (1 row)
  269. SELECT array_to_json(array_agg(x),false) from generate_series(5,10) x;
  270. array_to_json
  271. ----------------
  272. [5,6,7,8,9,10]
  273. (1 row)
  274. SELECT array_to_json('{{1,5},{99,100}}'::int[]);
  275. array_to_json
  276. ------------------
  277. [[1,5],[99,100]]
  278. (1 row)
  279. BEGIN;
  280. SET LOCAL TIME ZONE 10.5;
  281. SET LOCAL TIME ZONE -8;
  282. COMMIT;
  283. -- non-numeric output
  284. SELECT row_to_json(q)
  285. FROM (SELECT 'NaN'::float8 AS "float8field") q;
  286. row_to_json
  287. -----------------------
  288. {"float8field":"NaN"}
  289. (1 row)
  290. SELECT row_to_json(q)
  291. FROM (SELECT 'Infinity'::float8 AS "float8field") q;
  292. row_to_json
  293. ----------------------------
  294. {"float8field":"Infinity"}
  295. (1 row)
  296. SELECT row_to_json(q)
  297. FROM (SELECT '-Infinity'::float8 AS "float8field") q;
  298. row_to_json
  299. -----------------------------
  300. {"float8field":"-Infinity"}
  301. (1 row)
  302. -- json input
  303. SELECT row_to_json(q)
  304. FROM (SELECT '{"a":1,"b": [2,3,4,"d","e","f"],"c":{"p":1,"q":2}}'::json AS "jsonfield") q;
  305. row_to_json
  306. ------------------------------------------------------------------
  307. {"jsonfield":{"a":1,"b": [2,3,4,"d","e","f"],"c":{"p":1,"q":2}}}
  308. (1 row)
  309. -- json extraction functions
  310. CREATE TEMP TABLE test_json (
  311. json_type text,
  312. test_json json
  313. );
  314. INSERT INTO test_json VALUES
  315. ('scalar','"a scalar"'),
  316. ('array','["zero", "one","two",null,"four","five", [1,2,3],{"f1":9}]'),
  317. ('object','{"field1":"val1","field2":"val2","field3":null, "field4": 4, "field5": [1,2,3], "field6": {"f1":9}}');
  318. SELECT test_json -> 'x'
  319. FROM test_json
  320. WHERE json_type = 'scalar';
  321. ?column?
  322. ----------
  323. (1 row)
  324. SELECT test_json -> 'x'
  325. FROM test_json
  326. WHERE json_type = 'array';
  327. ?column?
  328. ----------
  329. (1 row)
  330. SELECT test_json -> 'x'
  331. FROM test_json
  332. WHERE json_type = 'object';
  333. ?column?
  334. ----------
  335. (1 row)
  336. SELECT test_json->'field2'
  337. FROM test_json
  338. WHERE json_type = 'object';
  339. ?column?
  340. ----------
  341. "val2"
  342. (1 row)
  343. SELECT test_json->>'field2'
  344. FROM test_json
  345. WHERE json_type = 'object';
  346. ?column?
  347. ----------
  348. val2
  349. (1 row)
  350. SELECT test_json -> 2
  351. FROM test_json
  352. WHERE json_type = 'scalar';
  353. ?column?
  354. ----------
  355. (1 row)
  356. SELECT test_json -> 2
  357. FROM test_json
  358. WHERE json_type = 'array';
  359. ?column?
  360. ----------
  361. "two"
  362. (1 row)
  363. SELECT test_json -> -1
  364. FROM test_json
  365. WHERE json_type = 'array';
  366. ?column?
  367. ----------
  368. {"f1":9}
  369. (1 row)
  370. SELECT test_json -> 2
  371. FROM test_json
  372. WHERE json_type = 'object';
  373. ?column?
  374. ----------
  375. (1 row)
  376. SELECT test_json->>2
  377. FROM test_json
  378. WHERE json_type = 'array';
  379. ?column?
  380. ----------
  381. two
  382. (1 row)
  383. SELECT test_json ->> 6 FROM test_json WHERE json_type = 'array';
  384. ?column?
  385. ----------
  386. [1,2,3]
  387. (1 row)
  388. SELECT test_json ->> 7 FROM test_json WHERE json_type = 'array';
  389. ?column?
  390. ----------
  391. {"f1":9}
  392. (1 row)
  393. SELECT test_json ->> 'field4' FROM test_json WHERE json_type = 'object';
  394. ?column?
  395. ----------
  396. 4
  397. (1 row)
  398. SELECT test_json ->> 'field5' FROM test_json WHERE json_type = 'object';
  399. ?column?
  400. ----------
  401. [1,2,3]
  402. (1 row)
  403. SELECT test_json ->> 'field6' FROM test_json WHERE json_type = 'object';
  404. ?column?
  405. ----------
  406. {"f1":9}
  407. (1 row)
  408. -- nulls
  409. select (test_json->'field3') is null as expect_false
  410. from test_json
  411. where json_type = 'object';
  412. expect_false
  413. --------------
  414. f
  415. (1 row)
  416. select (test_json->>'field3') is null as expect_true
  417. from test_json
  418. where json_type = 'object';
  419. expect_true
  420. -------------
  421. t
  422. (1 row)
  423. select (test_json->3) is null as expect_false
  424. from test_json
  425. where json_type = 'array';
  426. expect_false
  427. --------------
  428. f
  429. (1 row)
  430. select (test_json->>3) is null as expect_true
  431. from test_json
  432. where json_type = 'array';
  433. expect_true
  434. -------------
  435. t
  436. (1 row)
  437. -- corner cases
  438. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> null::text;
  439. ?column?
  440. ----------
  441. (1 row)
  442. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> null::int;
  443. ?column?
  444. ----------
  445. (1 row)
  446. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 1;
  447. ?column?
  448. ----------
  449. (1 row)
  450. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> -1;
  451. ?column?
  452. ----------
  453. (1 row)
  454. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 'z';
  455. ?column?
  456. ----------
  457. (1 row)
  458. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> '';
  459. ?column?
  460. ----------
  461. (1 row)
  462. select '[{"b": "c"}, {"b": "cc"}]'::json -> 1;
  463. ?column?
  464. -------------
  465. {"b": "cc"}
  466. (1 row)
  467. select '[{"b": "c"}, {"b": "cc"}]'::json -> 3;
  468. ?column?
  469. ----------
  470. (1 row)
  471. select '[{"b": "c"}, {"b": "cc"}]'::json -> 'z';
  472. ?column?
  473. ----------
  474. (1 row)
  475. select '{"a": "c", "b": null}'::json -> 'b';
  476. ?column?
  477. ----------
  478. null
  479. (1 row)
  480. select '"foo"'::json -> 1;
  481. ?column?
  482. ----------
  483. (1 row)
  484. select '"foo"'::json -> 'z';
  485. ?column?
  486. ----------
  487. (1 row)
  488. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::text;
  489. ?column?
  490. ----------
  491. (1 row)
  492. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::int;
  493. ?column?
  494. ----------
  495. (1 row)
  496. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 1;
  497. ?column?
  498. ----------
  499. (1 row)
  500. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 'z';
  501. ?column?
  502. ----------
  503. (1 row)
  504. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> '';
  505. ?column?
  506. ----------
  507. (1 row)
  508. select '[{"b": "c"}, {"b": "cc"}]'::json ->> 1;
  509. ?column?
  510. -------------
  511. {"b": "cc"}
  512. (1 row)
  513. select '[{"b": "c"}, {"b": "cc"}]'::json ->> 3;
  514. ?column?
  515. ----------
  516. (1 row)
  517. select '[{"b": "c"}, {"b": "cc"}]'::json ->> 'z';
  518. ?column?
  519. ----------
  520. (1 row)
  521. select '{"a": "c", "b": null}'::json ->> 'b';
  522. ?column?
  523. ----------
  524. (1 row)
  525. select '"foo"'::json ->> 1;
  526. ?column?
  527. ----------
  528. (1 row)
  529. select '"foo"'::json ->> 'z';
  530. ?column?
  531. ----------
  532. (1 row)
  533. -- array length
  534. SELECT json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]');
  535. json_array_length
  536. -------------------
  537. 5
  538. (1 row)
  539. SELECT json_array_length('[]');
  540. json_array_length
  541. -------------------
  542. 0
  543. (1 row)
  544. SELECT json_array_length('{"f1":1,"f2":[5,6]}');
  545. ERROR: cannot get array length of a non-array
  546. SELECT json_array_length('4');
  547. ERROR: cannot get array length of a scalar
  548. select * from json_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
  549. key | value
  550. -----+-----------
  551. f1 | [1,2,3]
  552. f2 | {"f3":1}
  553. f4 | null
  554. f5 | 99
  555. f6 | "stringy"
  556. (5 rows)
  557. select * from json_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
  558. key | value
  559. -----+----------
  560. f1 | [1,2,3]
  561. f2 | {"f3":1}
  562. f4 |
  563. f5 | 99
  564. f6 | stringy
  565. (5 rows)
  566. -- extract_path, extract_path_as_text
  567. select json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
  568. json_extract_path
  569. -------------------
  570. "stringy"
  571. (1 row)
  572. select json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
  573. json_extract_path
  574. -------------------
  575. {"f3":1}
  576. (1 row)
  577. select json_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
  578. json_extract_path
  579. -------------------
  580. "f3"
  581. (1 row)
  582. select json_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
  583. json_extract_path
  584. -------------------
  585. 1
  586. (1 row)
  587. select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
  588. json_extract_path_text
  589. ------------------------
  590. stringy
  591. (1 row)
  592. select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
  593. json_extract_path_text
  594. ------------------------
  595. {"f3":1}
  596. (1 row)
  597. select json_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
  598. json_extract_path_text
  599. ------------------------
  600. f3
  601. (1 row)
  602. select json_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
  603. json_extract_path_text
  604. ------------------------
  605. 1
  606. (1 row)
  607. -- extract_path nulls
  608. select json_extract_path('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') is null as expect_false;
  609. expect_false
  610. --------------
  611. f
  612. (1 row)
  613. select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') is null as expect_true;
  614. expect_true
  615. -------------
  616. t
  617. (1 row)
  618. select json_extract_path('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') is null as expect_false;
  619. expect_false
  620. --------------
  621. f
  622. (1 row)
  623. select json_extract_path_text('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') is null as expect_true;
  624. expect_true
  625. -------------
  626. t
  627. (1 row)
  628. select * from json_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q;
  629. value
  630. -----------------------
  631. 1
  632. true
  633. [1,[2,3]]
  634. null
  635. {"f1":1,"f2":[7,8,9]}
  636. false
  637. "stringy"
  638. (7 rows)
  639. select * from json_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q;
  640. value
  641. -----------------------
  642. 1
  643. true
  644. [1,[2,3]]
  645. {"f1":1,"f2":[7,8,9]}
  646. false
  647. stringy
  648. (7 rows)
  649. -- test type info caching in json_populate_record()
  650. CREATE TEMP TABLE jspoptest (js json);
  651. INSERT INTO jspoptest
  652. SELECT '{
  653. "jsa": [1, "2", null, 4],
  654. "rec": {"a": "abc", "c": "01.02.2003", "x": 43.2},
  655. "reca": [{"a": "abc", "b": 456}, null, {"c": "01.02.2003", "x": 43.2}]
  656. }'::json
  657. FROM generate_series(1, 3);
  658. --json_typeof() function
  659. select value, json_typeof(value)
  660. from (values (json '123.4'),
  661. (json '-1'),
  662. (json '"foo"'),
  663. (json 'true'),
  664. (json 'false'),
  665. (json 'null'),
  666. (json '[1, 2, 3]'),
  667. (json '[]'),
  668. (json '{"x":"foo", "y":123}'),
  669. (json '{}'),
  670. (NULL::json))
  671. as data(value);
  672. value | json_typeof
  673. ----------------------+-------------
  674. 123.4 | number
  675. -1 | number
  676. "foo" | string
  677. true | boolean
  678. false | boolean
  679. null | null
  680. [1, 2, 3] | array
  681. [] | array
  682. {"x":"foo", "y":123} | object
  683. {} | object
  684. |
  685. (11 rows)
  686. -- json_build_array, json_build_object, json_object_agg
  687. SELECT json_build_array('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
  688. json_build_array
  689. -----------------------------------------------------------------------
  690. ["a", 1, "b", 1.2, "c", true, "d", null, "e", {"x": 3, "y": [1,2,3]}]
  691. (1 row)
  692. SELECT json_build_array('a', NULL); -- ok
  693. json_build_array
  694. ------------------
  695. ["a", null]
  696. (1 row)
  697. SELECT json_build_object('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
  698. json_build_object
  699. ----------------------------------------------------------------------------
  700. {"a" : 1, "b" : 1.2, "c" : true, "d" : null, "e" : {"x": 3, "y": [1,2,3]}}
  701. (1 row)
  702. SELECT json_build_object('{a,b,c}'::text[]); -- error
  703. ERROR: argument list must have even number of elements
  704. HINT: The arguments of json_build_object() must consist of alternating keys and values.
  705. SELECT json_build_object('{a,b,c}'::text[], '{d,e,f}'::text[]); -- error, key cannot be array
  706. ERROR: key value must be scalar, not array, composite, or json
  707. SELECT json_build_object('a', 'b', 'c'); -- error
  708. ERROR: argument list must have even number of elements
  709. HINT: The arguments of json_build_object() must consist of alternating keys and values.
  710. SELECT json_build_object(NULL, 'a'); -- error, key cannot be NULL
  711. ERROR: argument 1 cannot be null
  712. HINT: Object keys should be text.
  713. SELECT json_build_object('a', NULL); -- ok
  714. json_build_object
  715. -------------------
  716. {"a" : null}
  717. (1 row)
  718. -- empty objects/arrays
  719. SELECT json_build_array();
  720. json_build_array
  721. ------------------
  722. []
  723. (1 row)
  724. SELECT json_build_object();
  725. json_build_object
  726. -------------------
  727. {}
  728. (1 row)
  729. -- make sure keys are quoted
  730. SELECT json_build_object(1,2);
  731. json_build_object
  732. -------------------
  733. {"1" : 2}
  734. (1 row)
  735. -- keys must be scalar and not null
  736. SELECT json_build_object(null,2);
  737. ERROR: argument 1 cannot be null
  738. HINT: Object keys should be text.
  739. SELECT json_build_object(r,2) FROM (SELECT 1 AS a, 2 AS b) r;
  740. ERROR: key value must be scalar, not array, composite, or json
  741. SELECT json_build_object(json '{"a":1,"b":2}', 3);
  742. ERROR: key value must be scalar, not array, composite, or json
  743. SELECT json_build_object('{1,2,3}'::int[], 3);
  744. ERROR: key value must be scalar, not array, composite, or json
  745. CREATE TEMP TABLE foo (serial_num int, name text, type text);
  746. INSERT INTO foo VALUES (847001,'t15','GE1043');
  747. INSERT INTO foo VALUES (847002,'t16','GE1043');
  748. INSERT INTO foo VALUES (847003,'sub-alpha','GESS90');
  749. SELECT json_build_object('turbines',json_object_agg(serial_num,json_build_object('name',name,'type',type)))
  750. FROM foo;
  751. json_build_object
  752. -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  753. {"turbines" : { "847001" : {"name" : "t15", "type" : "GE1043"}, "847002" : {"name" : "t16", "type" : "GE1043"}, "847003" : {"name" : "sub-alpha", "type" : "GESS90"} }}
  754. (1 row)
  755. SELECT json_object_agg(name, type) FROM foo;
  756. json_object_agg
  757. ----------------------------------------------------------------
  758. { "t15" : "GE1043", "t16" : "GE1043", "sub-alpha" : "GESS90" }
  759. (1 row)
  760. INSERT INTO foo VALUES (999999, NULL, 'bar');
  761. SELECT json_object_agg(name, type) FROM foo;
  762. ERROR: field name must not be null
  763. -- json_object
  764. -- empty object, one dimension
  765. SELECT json_object('{}');
  766. json_object
  767. -------------
  768. {}
  769. (1 row)
  770. -- empty object, two dimensions
  771. SELECT json_object('{}', '{}');
  772. json_object
  773. -------------
  774. {}
  775. (1 row)
  776. -- one dimension
  777. SELECT json_object('{a,1,b,2,3,NULL,"d e f","a b c"}');
  778. json_object
  779. -------------------------------------------------------
  780. {"a" : "1", "b" : "2", "3" : null, "d e f" : "a b c"}
  781. (1 row)
  782. -- same but with two dimensions
  783. SELECT json_object('{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}');
  784. json_object
  785. -------------------------------------------------------
  786. {"a" : "1", "b" : "2", "3" : null, "d e f" : "a b c"}
  787. (1 row)
  788. -- odd number error
  789. SELECT json_object('{a,b,c}');
  790. ERROR: array must have even number of elements
  791. -- one column error
  792. SELECT json_object('{{a},{b}}');
  793. ERROR: array must have two columns
  794. -- too many columns error
  795. SELECT json_object('{{a,b,c},{b,c,d}}');
  796. ERROR: array must have two columns
  797. -- too many dimensions error
  798. SELECT json_object('{{{a,b},{c,d}},{{b,c},{d,e}}}');
  799. ERROR: wrong number of array subscripts
  800. --two argument form of json_object
  801. select json_object('{a,b,c,"d e f"}','{1,2,3,"a b c"}');
  802. json_object
  803. ------------------------------------------------------
  804. {"a" : "1", "b" : "2", "c" : "3", "d e f" : "a b c"}
  805. (1 row)
  806. -- too many dimensions
  807. 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"}}');
  808. ERROR: wrong number of array subscripts
  809. -- mismatched dimensions
  810. select json_object('{a,b,c,"d e f",g}','{1,2,3,"a b c"}');
  811. ERROR: mismatched array dimensions
  812. select json_object('{a,b,c,"d e f"}','{1,2,3,"a b c",g}');
  813. ERROR: mismatched array dimensions
  814. -- null key error
  815. select json_object('{a,b,NULL,"d e f"}','{1,2,3,"a b c"}');
  816. ERROR: null value not allowed for object key
  817. -- empty key is allowed
  818. select json_object('{a,b,"","d e f"}','{1,2,3,"a b c"}');
  819. json_object
  820. -----------------------------------------------------
  821. {"a" : "1", "b" : "2", "" : "3", "d e f" : "a b c"}
  822. (1 row)
  823. -- json_strip_nulls
  824. select json_strip_nulls(null);
  825. json_strip_nulls
  826. ------------------
  827. (1 row)
  828. select json_strip_nulls('1');
  829. json_strip_nulls
  830. ------------------
  831. 1
  832. (1 row)
  833. select json_strip_nulls('"a string"');
  834. json_strip_nulls
  835. ------------------
  836. "a string"
  837. (1 row)
  838. select json_strip_nulls('null');
  839. json_strip_nulls
  840. ------------------
  841. null
  842. (1 row)
  843. select json_strip_nulls('[1,2,null,3,4]');
  844. json_strip_nulls
  845. ------------------
  846. [1,2,null,3,4]
  847. (1 row)
  848. select json_strip_nulls('{"a":1,"b":null,"c":[2,null,3],"d":{"e":4,"f":null}}');
  849. json_strip_nulls
  850. ------------------------------------
  851. {"a":1,"c":[2,null,3],"d":{"e":4}}
  852. (1 row)
  853. select json_strip_nulls('[1,{"a":1,"b":null,"c":2},3]');
  854. json_strip_nulls
  855. ---------------------
  856. [1,{"a":1,"c":2},3]
  857. (1 row)
  858. -- an empty object is not null and should not be stripped
  859. select json_strip_nulls('{"a": {"b": null, "c": null}, "d": {} }');
  860. json_strip_nulls
  861. ------------------
  862. {"a":{},"d":{}}
  863. (1 row)