json.out 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638
  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 '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'::json; -- OK
  105. json
  106. ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  107. [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
  108. (1 row)
  109. SELECT '[1,2]'::json; -- OK
  110. json
  111. -------
  112. [1,2]
  113. (1 row)
  114. SELECT '[1,2,]'::json; -- ERROR, trailing comma
  115. ERROR: invalid input syntax for type json
  116. LINE 1: SELECT '[1,2,]'::json;
  117. ^
  118. DETAIL: Expected JSON value, but found "]".
  119. CONTEXT: JSON data, line 1: [1,2,]
  120. SELECT '[1,2'::json; -- ERROR, no closing bracket
  121. ERROR: invalid input syntax for type json
  122. LINE 1: SELECT '[1,2'::json;
  123. ^
  124. DETAIL: The input string ended unexpectedly.
  125. CONTEXT: JSON data, line 1: [1,2
  126. SELECT '[1,[2]'::json; -- ERROR, no closing bracket
  127. ERROR: invalid input syntax for type json
  128. LINE 1: SELECT '[1,[2]'::json;
  129. ^
  130. DETAIL: The input string ended unexpectedly.
  131. CONTEXT: JSON data, line 1: [1,[2]
  132. -- Objects.
  133. SELECT '{}'::json; -- OK
  134. json
  135. ------
  136. {}
  137. (1 row)
  138. SELECT '{"abc"}'::json; -- ERROR, no value
  139. ERROR: invalid input syntax for type json
  140. LINE 1: SELECT '{"abc"}'::json;
  141. ^
  142. DETAIL: Expected ":", but found "}".
  143. CONTEXT: JSON data, line 1: {"abc"}
  144. SELECT '{"abc":1}'::json; -- OK
  145. json
  146. -----------
  147. {"abc":1}
  148. (1 row)
  149. SELECT '{1:"abc"}'::json; -- ERROR, keys must be strings
  150. ERROR: invalid input syntax for type json
  151. LINE 1: SELECT '{1:"abc"}'::json;
  152. ^
  153. DETAIL: Expected string or "}", but found "1".
  154. CONTEXT: JSON data, line 1: {1...
  155. SELECT '{"abc",1}'::json; -- ERROR, wrong separator
  156. ERROR: invalid input syntax for type json
  157. LINE 1: SELECT '{"abc",1}'::json;
  158. ^
  159. DETAIL: Expected ":", but found ",".
  160. CONTEXT: JSON data, line 1: {"abc",...
  161. SELECT '{"abc"=1}'::json; -- ERROR, totally wrong separator
  162. ERROR: invalid input syntax for type json
  163. LINE 1: SELECT '{"abc"=1}'::json;
  164. ^
  165. DETAIL: Token "=" is invalid.
  166. CONTEXT: JSON data, line 1: {"abc"=...
  167. SELECT '{"abc"::1}'::json; -- ERROR, another wrong separator
  168. ERROR: invalid input syntax for type json
  169. LINE 1: SELECT '{"abc"::1}'::json;
  170. ^
  171. DETAIL: Expected JSON value, but found ":".
  172. CONTEXT: JSON data, line 1: {"abc"::...
  173. SELECT '{"abc":1,"def":2,"ghi":[3,4],"hij":{"klm":5,"nop":[6]}}'::json; -- OK
  174. json
  175. ---------------------------------------------------------
  176. {"abc":1,"def":2,"ghi":[3,4],"hij":{"klm":5,"nop":[6]}}
  177. (1 row)
  178. SELECT '{"abc":1:2}'::json; -- ERROR, colon in wrong spot
  179. ERROR: invalid input syntax for type json
  180. LINE 1: SELECT '{"abc":1:2}'::json;
  181. ^
  182. DETAIL: Expected "," or "}", but found ":".
  183. CONTEXT: JSON data, line 1: {"abc":1:...
  184. SELECT '{"abc":1,3}'::json; -- ERROR, no value
  185. ERROR: invalid input syntax for type json
  186. LINE 1: SELECT '{"abc":1,3}'::json;
  187. ^
  188. DETAIL: Expected string, but found "3".
  189. CONTEXT: JSON data, line 1: {"abc":1,3...
  190. -- Recursion.
  191. SET max_stack_depth = '100kB';
  192. SELECT repeat('[', 10000)::json;
  193. ERROR: stack depth limit exceeded
  194. HINT: Increase the configuration parameter "max_stack_depth" (currently 100kB), after ensuring the platform's stack depth limit is adequate.
  195. SELECT repeat('{"a":', 10000)::json;
  196. ERROR: stack depth limit exceeded
  197. HINT: Increase the configuration parameter "max_stack_depth" (currently 100kB), after ensuring the platform's stack depth limit is adequate.
  198. RESET max_stack_depth;
  199. -- Miscellaneous stuff.
  200. SELECT 'true'::json; -- OK
  201. json
  202. ------
  203. true
  204. (1 row)
  205. SELECT 'false'::json; -- OK
  206. json
  207. -------
  208. false
  209. (1 row)
  210. SELECT 'null'::json; -- OK
  211. json
  212. ------
  213. null
  214. (1 row)
  215. SELECT ' true '::json; -- OK, even with extra whitespace
  216. json
  217. --------
  218. true
  219. (1 row)
  220. SELECT 'true false'::json; -- ERROR, too many values
  221. ERROR: invalid input syntax for type json
  222. LINE 1: SELECT 'true false'::json;
  223. ^
  224. DETAIL: Expected end of input, but found "false".
  225. CONTEXT: JSON data, line 1: true false
  226. SELECT 'true, false'::json; -- ERROR, too many values
  227. ERROR: invalid input syntax for type json
  228. LINE 1: SELECT 'true, false'::json;
  229. ^
  230. DETAIL: Expected end of input, but found ",".
  231. CONTEXT: JSON data, line 1: true,...
  232. SELECT 'truf'::json; -- ERROR, not a keyword
  233. ERROR: invalid input syntax for type json
  234. LINE 1: SELECT 'truf'::json;
  235. ^
  236. DETAIL: Token "truf" is invalid.
  237. CONTEXT: JSON data, line 1: truf
  238. SELECT 'trues'::json; -- ERROR, not a keyword
  239. ERROR: invalid input syntax for type json
  240. LINE 1: SELECT 'trues'::json;
  241. ^
  242. DETAIL: Token "trues" is invalid.
  243. CONTEXT: JSON data, line 1: trues
  244. SELECT ''::json; -- ERROR, no value
  245. ERROR: invalid input syntax for type json
  246. LINE 1: SELECT ''::json;
  247. ^
  248. DETAIL: The input string ended unexpectedly.
  249. CONTEXT: JSON data, line 1:
  250. SELECT ' '::json; -- ERROR, no value
  251. ERROR: invalid input syntax for type json
  252. LINE 1: SELECT ' '::json;
  253. ^
  254. DETAIL: The input string ended unexpectedly.
  255. CONTEXT: JSON data, line 1:
  256. -- Multi-line JSON input to check ERROR reporting
  257. SELECT '{
  258. "one": 1,
  259. "two":"two",
  260. "three":
  261. true}'::json; -- OK
  262. json
  263. ------------------------------
  264. { +
  265. "one": 1, +
  266. "two":"two",+
  267. "three": +
  268. true}
  269. (1 row)
  270. SELECT '{
  271. "one": 1,
  272. "two":,"two", -- ERROR extraneous comma before field "two"
  273. "three":
  274. true}'::json;
  275. ERROR: invalid input syntax for type json
  276. LINE 1: SELECT '{
  277. ^
  278. DETAIL: Expected JSON value, but found ",".
  279. CONTEXT: JSON data, line 3: "two":,...
  280. SELECT '{
  281. "one": 1,
  282. "two":"two",
  283. "averyveryveryveryveryveryveryveryveryverylongfieldname":}'::json;
  284. ERROR: invalid input syntax for type json
  285. LINE 1: SELECT '{
  286. ^
  287. DETAIL: Expected JSON value, but found "}".
  288. CONTEXT: JSON data, line 4: ...yveryveryveryveryveryveryveryverylongfieldname":}
  289. -- ERROR missing value for last field
  290. --constructors
  291. -- array_to_json
  292. SELECT array_to_json(array(select 1 as a));
  293. array_to_json
  294. ---------------
  295. [1]
  296. (1 row)
  297. SELECT array_to_json(array_agg(q),false) from (select x as b, x * 2 as c from generate_series(1,3) x) q;
  298. array_to_json
  299. ---------------------------------------------
  300. [{"b":1,"c":2},{"b":2,"c":4},{"b":3,"c":6}]
  301. (1 row)
  302. SELECT array_to_json(array_agg(q),true) from (select x as b, x * 2 as c from generate_series(1,3) x) q;
  303. array_to_json
  304. -----------------
  305. [{"b":1,"c":2},+
  306. {"b":2,"c":4},+
  307. {"b":3,"c":6}]
  308. (1 row)
  309. SELECT array_to_json(array_agg(q),false)
  310. FROM ( SELECT $$a$$ || x AS b, y AS c,
  311. ARRAY[ROW(x.*,ARRAY[1,2,3]),
  312. ROW(y.*,ARRAY[4,5,6])] AS z
  313. FROM generate_series(1,2) x,
  314. generate_series(4,5) y) q;
  315. array_to_json
  316. -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  317. [{"b":"a1","c":4,"z":[{"f1":1,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]},{"b":"a1","c":5,"z":[{"f1":1,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]},{"b":"a2","c":4,"z":[{"f1":2,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]},{"b":"a2","c":5,"z":[{"f1":2,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}]
  318. (1 row)
  319. SELECT array_to_json(array_agg(x),false) from generate_series(5,10) x;
  320. array_to_json
  321. ----------------
  322. [5,6,7,8,9,10]
  323. (1 row)
  324. SELECT array_to_json('{{1,5},{99,100}}'::int[]);
  325. array_to_json
  326. ------------------
  327. [[1,5],[99,100]]
  328. (1 row)
  329. -- row_to_json
  330. SELECT row_to_json(row(1,'foo'));
  331. row_to_json
  332. ---------------------
  333. {"f1":1,"f2":"foo"}
  334. (1 row)
  335. SELECT row_to_json(q)
  336. FROM (SELECT $$a$$ || x AS b,
  337. y AS c,
  338. ARRAY[ROW(x.*,ARRAY[1,2,3]),
  339. ROW(y.*,ARRAY[4,5,6])] AS z
  340. FROM generate_series(1,2) x,
  341. generate_series(4,5) y) q;
  342. row_to_json
  343. --------------------------------------------------------------------
  344. {"b":"a1","c":4,"z":[{"f1":1,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}
  345. {"b":"a1","c":5,"z":[{"f1":1,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}
  346. {"b":"a2","c":4,"z":[{"f1":2,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}
  347. {"b":"a2","c":5,"z":[{"f1":2,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}
  348. (4 rows)
  349. SELECT row_to_json(q,true)
  350. FROM (SELECT $$a$$ || x AS b,
  351. y AS c,
  352. ARRAY[ROW(x.*,ARRAY[1,2,3]),
  353. ROW(y.*,ARRAY[4,5,6])] AS z
  354. FROM generate_series(1,2) x,
  355. generate_series(4,5) y) q;
  356. row_to_json
  357. -----------------------------------------------------
  358. {"b":"a1", +
  359. "c":4, +
  360. "z":[{"f1":1,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}
  361. {"b":"a1", +
  362. "c":5, +
  363. "z":[{"f1":1,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}
  364. {"b":"a2", +
  365. "c":4, +
  366. "z":[{"f1":2,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}
  367. {"b":"a2", +
  368. "c":5, +
  369. "z":[{"f1":2,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}
  370. (4 rows)
  371. CREATE TEMP TABLE rows AS
  372. SELECT x, 'txt' || x as y
  373. FROM generate_series(1,3) AS x;
  374. SELECT row_to_json(q,true)
  375. FROM rows q;
  376. row_to_json
  377. --------------
  378. {"x":1, +
  379. "y":"txt1"}
  380. {"x":2, +
  381. "y":"txt2"}
  382. {"x":3, +
  383. "y":"txt3"}
  384. (3 rows)
  385. SELECT row_to_json(row((select array_agg(x) as d from generate_series(5,10) x)),false);
  386. row_to_json
  387. -----------------------
  388. {"f1":[5,6,7,8,9,10]}
  389. (1 row)
  390. -- anyarray column
  391. analyze rows;
  392. select attname, to_json(histogram_bounds) histogram_bounds
  393. from pg_stats
  394. where tablename = 'rows' and
  395. schemaname = pg_my_temp_schema()::regnamespace::text
  396. order by 1;
  397. attname | histogram_bounds
  398. ---------+------------------------
  399. x | [1,2,3]
  400. y | ["txt1","txt2","txt3"]
  401. (2 rows)
  402. -- to_json, timestamps
  403. select to_json(timestamp '2014-05-28 12:22:35.614298');
  404. to_json
  405. ------------------------------
  406. "2014-05-28T12:22:35.614298"
  407. (1 row)
  408. BEGIN;
  409. SET LOCAL TIME ZONE 10.5;
  410. select to_json(timestamptz '2014-05-28 12:22:35.614298-04');
  411. to_json
  412. ------------------------------------
  413. "2014-05-29T02:52:35.614298+10:30"
  414. (1 row)
  415. SET LOCAL TIME ZONE -8;
  416. select to_json(timestamptz '2014-05-28 12:22:35.614298-04');
  417. to_json
  418. ------------------------------------
  419. "2014-05-28T08:22:35.614298-08:00"
  420. (1 row)
  421. COMMIT;
  422. select to_json(date '2014-05-28');
  423. to_json
  424. --------------
  425. "2014-05-28"
  426. (1 row)
  427. select to_json(date 'Infinity');
  428. to_json
  429. ------------
  430. "infinity"
  431. (1 row)
  432. select to_json(date '-Infinity');
  433. to_json
  434. -------------
  435. "-infinity"
  436. (1 row)
  437. select to_json(timestamp 'Infinity');
  438. to_json
  439. ------------
  440. "infinity"
  441. (1 row)
  442. select to_json(timestamp '-Infinity');
  443. to_json
  444. -------------
  445. "-infinity"
  446. (1 row)
  447. select to_json(timestamptz 'Infinity');
  448. to_json
  449. ------------
  450. "infinity"
  451. (1 row)
  452. select to_json(timestamptz '-Infinity');
  453. to_json
  454. -------------
  455. "-infinity"
  456. (1 row)
  457. --json_agg
  458. SELECT json_agg(q)
  459. FROM ( SELECT $$a$$ || x AS b, y AS c,
  460. ARRAY[ROW(x.*,ARRAY[1,2,3]),
  461. ROW(y.*,ARRAY[4,5,6])] AS z
  462. FROM generate_series(1,2) x,
  463. generate_series(4,5) y) q;
  464. json_agg
  465. -----------------------------------------------------------------------
  466. [{"b":"a1","c":4,"z":[{"f1":1,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}, +
  467. {"b":"a1","c":5,"z":[{"f1":1,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}, +
  468. {"b":"a2","c":4,"z":[{"f1":2,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}, +
  469. {"b":"a2","c":5,"z":[{"f1":2,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}]
  470. (1 row)
  471. SELECT json_agg(q ORDER BY x, y)
  472. FROM rows q;
  473. json_agg
  474. -----------------------
  475. [{"x":1,"y":"txt1"}, +
  476. {"x":2,"y":"txt2"}, +
  477. {"x":3,"y":"txt3"}]
  478. (1 row)
  479. UPDATE rows SET x = NULL WHERE x = 1;
  480. SELECT json_agg(q ORDER BY x NULLS FIRST, y)
  481. FROM rows q;
  482. json_agg
  483. --------------------------
  484. [{"x":null,"y":"txt1"}, +
  485. {"x":2,"y":"txt2"}, +
  486. {"x":3,"y":"txt3"}]
  487. (1 row)
  488. -- non-numeric output
  489. SELECT row_to_json(q)
  490. FROM (SELECT 'NaN'::float8 AS "float8field") q;
  491. row_to_json
  492. -----------------------
  493. {"float8field":"NaN"}
  494. (1 row)
  495. SELECT row_to_json(q)
  496. FROM (SELECT 'Infinity'::float8 AS "float8field") q;
  497. row_to_json
  498. ----------------------------
  499. {"float8field":"Infinity"}
  500. (1 row)
  501. SELECT row_to_json(q)
  502. FROM (SELECT '-Infinity'::float8 AS "float8field") q;
  503. row_to_json
  504. -----------------------------
  505. {"float8field":"-Infinity"}
  506. (1 row)
  507. -- json input
  508. SELECT row_to_json(q)
  509. FROM (SELECT '{"a":1,"b": [2,3,4,"d","e","f"],"c":{"p":1,"q":2}}'::json AS "jsonfield") q;
  510. row_to_json
  511. ------------------------------------------------------------------
  512. {"jsonfield":{"a":1,"b": [2,3,4,"d","e","f"],"c":{"p":1,"q":2}}}
  513. (1 row)
  514. -- json extraction functions
  515. CREATE TEMP TABLE test_json (
  516. json_type text,
  517. test_json json
  518. );
  519. INSERT INTO test_json VALUES
  520. ('scalar','"a scalar"'),
  521. ('array','["zero", "one","two",null,"four","five", [1,2,3],{"f1":9}]'),
  522. ('object','{"field1":"val1","field2":"val2","field3":null, "field4": 4, "field5": [1,2,3], "field6": {"f1":9}}');
  523. SELECT test_json -> 'x'
  524. FROM test_json
  525. WHERE json_type = 'scalar';
  526. ?column?
  527. ----------
  528. (1 row)
  529. SELECT test_json -> 'x'
  530. FROM test_json
  531. WHERE json_type = 'array';
  532. ?column?
  533. ----------
  534. (1 row)
  535. SELECT test_json -> 'x'
  536. FROM test_json
  537. WHERE json_type = 'object';
  538. ?column?
  539. ----------
  540. (1 row)
  541. SELECT test_json->'field2'
  542. FROM test_json
  543. WHERE json_type = 'object';
  544. ?column?
  545. ----------
  546. "val2"
  547. (1 row)
  548. SELECT test_json->>'field2'
  549. FROM test_json
  550. WHERE json_type = 'object';
  551. ?column?
  552. ----------
  553. val2
  554. (1 row)
  555. SELECT test_json -> 2
  556. FROM test_json
  557. WHERE json_type = 'scalar';
  558. ?column?
  559. ----------
  560. (1 row)
  561. SELECT test_json -> 2
  562. FROM test_json
  563. WHERE json_type = 'array';
  564. ?column?
  565. ----------
  566. "two"
  567. (1 row)
  568. SELECT test_json -> -1
  569. FROM test_json
  570. WHERE json_type = 'array';
  571. ?column?
  572. ----------
  573. {"f1":9}
  574. (1 row)
  575. SELECT test_json -> 2
  576. FROM test_json
  577. WHERE json_type = 'object';
  578. ?column?
  579. ----------
  580. (1 row)
  581. SELECT test_json->>2
  582. FROM test_json
  583. WHERE json_type = 'array';
  584. ?column?
  585. ----------
  586. two
  587. (1 row)
  588. SELECT test_json ->> 6 FROM test_json WHERE json_type = 'array';
  589. ?column?
  590. ----------
  591. [1,2,3]
  592. (1 row)
  593. SELECT test_json ->> 7 FROM test_json WHERE json_type = 'array';
  594. ?column?
  595. ----------
  596. {"f1":9}
  597. (1 row)
  598. SELECT test_json ->> 'field4' FROM test_json WHERE json_type = 'object';
  599. ?column?
  600. ----------
  601. 4
  602. (1 row)
  603. SELECT test_json ->> 'field5' FROM test_json WHERE json_type = 'object';
  604. ?column?
  605. ----------
  606. [1,2,3]
  607. (1 row)
  608. SELECT test_json ->> 'field6' FROM test_json WHERE json_type = 'object';
  609. ?column?
  610. ----------
  611. {"f1":9}
  612. (1 row)
  613. SELECT json_object_keys(test_json)
  614. FROM test_json
  615. WHERE json_type = 'scalar';
  616. ERROR: cannot call json_object_keys on a scalar
  617. SELECT json_object_keys(test_json)
  618. FROM test_json
  619. WHERE json_type = 'array';
  620. ERROR: cannot call json_object_keys on an array
  621. SELECT json_object_keys(test_json)
  622. FROM test_json
  623. WHERE json_type = 'object';
  624. json_object_keys
  625. ------------------
  626. field1
  627. field2
  628. field3
  629. field4
  630. field5
  631. field6
  632. (6 rows)
  633. -- test extending object_keys resultset - initial resultset size is 256
  634. select count(*) from
  635. (select json_object_keys(json_object(array_agg(g)))
  636. from (select unnest(array['f'||n,n::text])as g
  637. from generate_series(1,300) as n) x ) y;
  638. count
  639. -------
  640. 300
  641. (1 row)
  642. -- nulls
  643. select (test_json->'field3') is null as expect_false
  644. from test_json
  645. where json_type = 'object';
  646. expect_false
  647. --------------
  648. f
  649. (1 row)
  650. select (test_json->>'field3') is null as expect_true
  651. from test_json
  652. where json_type = 'object';
  653. expect_true
  654. -------------
  655. t
  656. (1 row)
  657. select (test_json->3) is null as expect_false
  658. from test_json
  659. where json_type = 'array';
  660. expect_false
  661. --------------
  662. f
  663. (1 row)
  664. select (test_json->>3) is null as expect_true
  665. from test_json
  666. where json_type = 'array';
  667. expect_true
  668. -------------
  669. t
  670. (1 row)
  671. -- corner cases
  672. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> null::text;
  673. ?column?
  674. ----------
  675. (1 row)
  676. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> null::int;
  677. ?column?
  678. ----------
  679. (1 row)
  680. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 1;
  681. ?column?
  682. ----------
  683. (1 row)
  684. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> -1;
  685. ?column?
  686. ----------
  687. (1 row)
  688. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 'z';
  689. ?column?
  690. ----------
  691. (1 row)
  692. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> '';
  693. ?column?
  694. ----------
  695. (1 row)
  696. select '[{"b": "c"}, {"b": "cc"}]'::json -> 1;
  697. ?column?
  698. -------------
  699. {"b": "cc"}
  700. (1 row)
  701. select '[{"b": "c"}, {"b": "cc"}]'::json -> 3;
  702. ?column?
  703. ----------
  704. (1 row)
  705. select '[{"b": "c"}, {"b": "cc"}]'::json -> 'z';
  706. ?column?
  707. ----------
  708. (1 row)
  709. select '{"a": "c", "b": null}'::json -> 'b';
  710. ?column?
  711. ----------
  712. null
  713. (1 row)
  714. select '"foo"'::json -> 1;
  715. ?column?
  716. ----------
  717. (1 row)
  718. select '"foo"'::json -> 'z';
  719. ?column?
  720. ----------
  721. (1 row)
  722. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::text;
  723. ?column?
  724. ----------
  725. (1 row)
  726. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::int;
  727. ?column?
  728. ----------
  729. (1 row)
  730. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 1;
  731. ?column?
  732. ----------
  733. (1 row)
  734. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 'z';
  735. ?column?
  736. ----------
  737. (1 row)
  738. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> '';
  739. ?column?
  740. ----------
  741. (1 row)
  742. select '[{"b": "c"}, {"b": "cc"}]'::json ->> 1;
  743. ?column?
  744. -------------
  745. {"b": "cc"}
  746. (1 row)
  747. select '[{"b": "c"}, {"b": "cc"}]'::json ->> 3;
  748. ?column?
  749. ----------
  750. (1 row)
  751. select '[{"b": "c"}, {"b": "cc"}]'::json ->> 'z';
  752. ?column?
  753. ----------
  754. (1 row)
  755. select '{"a": "c", "b": null}'::json ->> 'b';
  756. ?column?
  757. ----------
  758. (1 row)
  759. select '"foo"'::json ->> 1;
  760. ?column?
  761. ----------
  762. (1 row)
  763. select '"foo"'::json ->> 'z';
  764. ?column?
  765. ----------
  766. (1 row)
  767. -- array length
  768. SELECT json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]');
  769. json_array_length
  770. -------------------
  771. 5
  772. (1 row)
  773. SELECT json_array_length('[]');
  774. json_array_length
  775. -------------------
  776. 0
  777. (1 row)
  778. SELECT json_array_length('{"f1":1,"f2":[5,6]}');
  779. ERROR: cannot get array length of a non-array
  780. SELECT json_array_length('4');
  781. ERROR: cannot get array length of a scalar
  782. -- each
  783. select json_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null}');
  784. json_each
  785. -------------------
  786. (f1,"[1,2,3]")
  787. (f2,"{""f3"":1}")
  788. (f4,null)
  789. (3 rows)
  790. select * from json_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
  791. key | value
  792. -----+-----------
  793. f1 | [1,2,3]
  794. f2 | {"f3":1}
  795. f4 | null
  796. f5 | 99
  797. f6 | "stringy"
  798. (5 rows)
  799. select json_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":"null"}');
  800. json_each_text
  801. -------------------
  802. (f1,"[1,2,3]")
  803. (f2,"{""f3"":1}")
  804. (f4,)
  805. (f5,null)
  806. (4 rows)
  807. select * from json_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
  808. key | value
  809. -----+----------
  810. f1 | [1,2,3]
  811. f2 | {"f3":1}
  812. f4 |
  813. f5 | 99
  814. f6 | stringy
  815. (5 rows)
  816. -- extract_path, extract_path_as_text
  817. select json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
  818. json_extract_path
  819. -------------------
  820. "stringy"
  821. (1 row)
  822. select json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
  823. json_extract_path
  824. -------------------
  825. {"f3":1}
  826. (1 row)
  827. select json_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
  828. json_extract_path
  829. -------------------
  830. "f3"
  831. (1 row)
  832. select json_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
  833. json_extract_path
  834. -------------------
  835. 1
  836. (1 row)
  837. select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
  838. json_extract_path_text
  839. ------------------------
  840. stringy
  841. (1 row)
  842. select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
  843. json_extract_path_text
  844. ------------------------
  845. {"f3":1}
  846. (1 row)
  847. select json_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
  848. json_extract_path_text
  849. ------------------------
  850. f3
  851. (1 row)
  852. select json_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
  853. json_extract_path_text
  854. ------------------------
  855. 1
  856. (1 row)
  857. -- extract_path nulls
  858. select json_extract_path('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') is null as expect_false;
  859. expect_false
  860. --------------
  861. f
  862. (1 row)
  863. select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') is null as expect_true;
  864. expect_true
  865. -------------
  866. t
  867. (1 row)
  868. select json_extract_path('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') is null as expect_false;
  869. expect_false
  870. --------------
  871. f
  872. (1 row)
  873. select json_extract_path_text('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') is null as expect_true;
  874. expect_true
  875. -------------
  876. t
  877. (1 row)
  878. -- extract_path operators
  879. select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f4','f6'];
  880. ?column?
  881. -----------
  882. "stringy"
  883. (1 row)
  884. select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2'];
  885. ?column?
  886. ----------
  887. {"f3":1}
  888. (1 row)
  889. select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2','0'];
  890. ?column?
  891. ----------
  892. "f3"
  893. (1 row)
  894. select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2','1'];
  895. ?column?
  896. ----------
  897. 1
  898. (1 row)
  899. select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f4','f6'];
  900. ?column?
  901. ----------
  902. stringy
  903. (1 row)
  904. select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2'];
  905. ?column?
  906. ----------
  907. {"f3":1}
  908. (1 row)
  909. select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','0'];
  910. ?column?
  911. ----------
  912. f3
  913. (1 row)
  914. select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','1'];
  915. ?column?
  916. ----------
  917. 1
  918. (1 row)
  919. -- corner cases for same
  920. select '{"a": {"b":{"c": "foo"}}}'::json #> '{}';
  921. ?column?
  922. ---------------------------
  923. {"a": {"b":{"c": "foo"}}}
  924. (1 row)
  925. select '[1,2,3]'::json #> '{}';
  926. ?column?
  927. ----------
  928. [1,2,3]
  929. (1 row)
  930. select '"foo"'::json #> '{}';
  931. ?column?
  932. ----------
  933. "foo"
  934. (1 row)
  935. select '42'::json #> '{}';
  936. ?column?
  937. ----------
  938. 42
  939. (1 row)
  940. select 'null'::json #> '{}';
  941. ?column?
  942. ----------
  943. null
  944. (1 row)
  945. select '{"a": {"b":{"c": "foo"}}}'::json #> array['a'];
  946. ?column?
  947. --------------------
  948. {"b":{"c": "foo"}}
  949. (1 row)
  950. select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', null];
  951. ?column?
  952. ----------
  953. (1 row)
  954. select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', ''];
  955. ?column?
  956. ----------
  957. (1 row)
  958. select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b'];
  959. ?column?
  960. --------------
  961. {"c": "foo"}
  962. (1 row)
  963. select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b','c'];
  964. ?column?
  965. ----------
  966. "foo"
  967. (1 row)
  968. select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b','c','d'];
  969. ?column?
  970. ----------
  971. (1 row)
  972. select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','z','c'];
  973. ?column?
  974. ----------
  975. (1 row)
  976. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #> array['a','1','b'];
  977. ?column?
  978. ----------
  979. "cc"
  980. (1 row)
  981. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #> array['a','z','b'];
  982. ?column?
  983. ----------
  984. (1 row)
  985. select '[{"b": "c"}, {"b": "cc"}]'::json #> array['1','b'];
  986. ?column?
  987. ----------
  988. "cc"
  989. (1 row)
  990. select '[{"b": "c"}, {"b": "cc"}]'::json #> array['z','b'];
  991. ?column?
  992. ----------
  993. (1 row)
  994. select '[{"b": "c"}, {"b": null}]'::json #> array['1','b'];
  995. ?column?
  996. ----------
  997. null
  998. (1 row)
  999. select '"foo"'::json #> array['z'];
  1000. ?column?
  1001. ----------
  1002. (1 row)
  1003. select '42'::json #> array['f2'];
  1004. ?column?
  1005. ----------
  1006. (1 row)
  1007. select '42'::json #> array['0'];
  1008. ?column?
  1009. ----------
  1010. (1 row)
  1011. select '{"a": {"b":{"c": "foo"}}}'::json #>> '{}';
  1012. ?column?
  1013. ---------------------------
  1014. {"a": {"b":{"c": "foo"}}}
  1015. (1 row)
  1016. select '[1,2,3]'::json #>> '{}';
  1017. ?column?
  1018. ----------
  1019. [1,2,3]
  1020. (1 row)
  1021. select '"foo"'::json #>> '{}';
  1022. ?column?
  1023. ----------
  1024. foo
  1025. (1 row)
  1026. select '42'::json #>> '{}';
  1027. ?column?
  1028. ----------
  1029. 42
  1030. (1 row)
  1031. select 'null'::json #>> '{}';
  1032. ?column?
  1033. ----------
  1034. (1 row)
  1035. select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a'];
  1036. ?column?
  1037. --------------------
  1038. {"b":{"c": "foo"}}
  1039. (1 row)
  1040. select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', null];
  1041. ?column?
  1042. ----------
  1043. (1 row)
  1044. select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', ''];
  1045. ?column?
  1046. ----------
  1047. (1 row)
  1048. select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b'];
  1049. ?column?
  1050. --------------
  1051. {"c": "foo"}
  1052. (1 row)
  1053. select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b','c'];
  1054. ?column?
  1055. ----------
  1056. foo
  1057. (1 row)
  1058. select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b','c','d'];
  1059. ?column?
  1060. ----------
  1061. (1 row)
  1062. select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','z','c'];
  1063. ?column?
  1064. ----------
  1065. (1 row)
  1066. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #>> array['a','1','b'];
  1067. ?column?
  1068. ----------
  1069. cc
  1070. (1 row)
  1071. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #>> array['a','z','b'];
  1072. ?column?
  1073. ----------
  1074. (1 row)
  1075. select '[{"b": "c"}, {"b": "cc"}]'::json #>> array['1','b'];
  1076. ?column?
  1077. ----------
  1078. cc
  1079. (1 row)
  1080. select '[{"b": "c"}, {"b": "cc"}]'::json #>> array['z','b'];
  1081. ?column?
  1082. ----------
  1083. (1 row)
  1084. select '[{"b": "c"}, {"b": null}]'::json #>> array['1','b'];
  1085. ?column?
  1086. ----------
  1087. (1 row)
  1088. select '"foo"'::json #>> array['z'];
  1089. ?column?
  1090. ----------
  1091. (1 row)
  1092. select '42'::json #>> array['f2'];
  1093. ?column?
  1094. ----------
  1095. (1 row)
  1096. select '42'::json #>> array['0'];
  1097. ?column?
  1098. ----------
  1099. (1 row)
  1100. -- array_elements
  1101. select json_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]');
  1102. json_array_elements
  1103. -----------------------
  1104. 1
  1105. true
  1106. [1,[2,3]]
  1107. null
  1108. {"f1":1,"f2":[7,8,9]}
  1109. false
  1110. "stringy"
  1111. (7 rows)
  1112. select * from json_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q;
  1113. value
  1114. -----------------------
  1115. 1
  1116. true
  1117. [1,[2,3]]
  1118. null
  1119. {"f1":1,"f2":[7,8,9]}
  1120. false
  1121. "stringy"
  1122. (7 rows)
  1123. select json_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]');
  1124. json_array_elements_text
  1125. --------------------------
  1126. 1
  1127. true
  1128. [1,[2,3]]
  1129. {"f1":1,"f2":[7,8,9]}
  1130. false
  1131. stringy
  1132. (7 rows)
  1133. select * from json_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q;
  1134. value
  1135. -----------------------
  1136. 1
  1137. true
  1138. [1,[2,3]]
  1139. {"f1":1,"f2":[7,8,9]}
  1140. false
  1141. stringy
  1142. (7 rows)
  1143. -- populate_record
  1144. create type jpop as (a text, b int, c timestamp);
  1145. CREATE DOMAIN js_int_not_null AS int NOT NULL;
  1146. CREATE DOMAIN js_int_array_1d AS int[] CHECK(array_length(VALUE, 1) = 3);
  1147. CREATE DOMAIN js_int_array_2d AS int[][] CHECK(array_length(VALUE, 2) = 3);
  1148. create type j_unordered_pair as (x int, y int);
  1149. create domain j_ordered_pair as j_unordered_pair check((value).x <= (value).y);
  1150. CREATE TYPE jsrec AS (
  1151. i int,
  1152. ia _int4,
  1153. ia1 int[],
  1154. ia2 int[][],
  1155. ia3 int[][][],
  1156. ia1d js_int_array_1d,
  1157. ia2d js_int_array_2d,
  1158. t text,
  1159. ta text[],
  1160. c char(10),
  1161. ca char(10)[],
  1162. ts timestamp,
  1163. js json,
  1164. jsb jsonb,
  1165. jsa json[],
  1166. rec jpop,
  1167. reca jpop[]
  1168. );
  1169. CREATE TYPE jsrec_i_not_null AS (
  1170. i js_int_not_null
  1171. );
  1172. select * from json_populate_record(null::jpop,'{"a":"blurfl","x":43.2}') q;
  1173. a | b | c
  1174. --------+---+---
  1175. blurfl | |
  1176. (1 row)
  1177. select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":"blurfl","x":43.2}') q;
  1178. a | b | c
  1179. --------+---+--------------------------
  1180. blurfl | 3 | Mon Dec 31 15:30:56 2012
  1181. (1 row)
  1182. select * from json_populate_record(null::jpop,'{"a":"blurfl","x":43.2}') q;
  1183. a | b | c
  1184. --------+---+---
  1185. blurfl | |
  1186. (1 row)
  1187. select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":"blurfl","x":43.2}') q;
  1188. a | b | c
  1189. --------+---+--------------------------
  1190. blurfl | 3 | Mon Dec 31 15:30:56 2012
  1191. (1 row)
  1192. select * from json_populate_record(null::jpop,'{"a":[100,200,false],"x":43.2}') q;
  1193. a | b | c
  1194. -----------------+---+---
  1195. [100,200,false] | |
  1196. (1 row)
  1197. select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":[100,200,false],"x":43.2}') q;
  1198. a | b | c
  1199. -----------------+---+--------------------------
  1200. [100,200,false] | 3 | Mon Dec 31 15:30:56 2012
  1201. (1 row)
  1202. select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"c":[100,200,false],"x":43.2}') q;
  1203. ERROR: invalid input syntax for type timestamp: "[100,200,false]"
  1204. select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{}') q;
  1205. a | b | c
  1206. ---+---+--------------------------
  1207. x | 3 | Mon Dec 31 15:30:56 2012
  1208. (1 row)
  1209. SELECT i FROM json_populate_record(NULL::jsrec_i_not_null, '{"x": 43.2}') q;
  1210. ERROR: domain js_int_not_null does not allow null values
  1211. SELECT i FROM json_populate_record(NULL::jsrec_i_not_null, '{"i": null}') q;
  1212. ERROR: domain js_int_not_null does not allow null values
  1213. SELECT i FROM json_populate_record(NULL::jsrec_i_not_null, '{"i": 12345}') q;
  1214. i
  1215. -------
  1216. 12345
  1217. (1 row)
  1218. SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": null}') q;
  1219. ia
  1220. ----
  1221. (1 row)
  1222. SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": 123}') q;
  1223. ERROR: expected JSON array
  1224. HINT: See the value of key "ia".
  1225. SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": [1, "2", null, 4]}') q;
  1226. ia
  1227. --------------
  1228. {1,2,NULL,4}
  1229. (1 row)
  1230. SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": [[1, 2], [3, 4]]}') q;
  1231. ia
  1232. ---------------
  1233. {{1,2},{3,4}}
  1234. (1 row)
  1235. SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": [[1], 2]}') q;
  1236. ERROR: expected JSON array
  1237. HINT: See the array element [1] of key "ia".
  1238. SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": [[1], [2, 3]]}') q;
  1239. ERROR: malformed JSON array
  1240. DETAIL: Multidimensional arrays must have sub-arrays with matching dimensions.
  1241. SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": "{1,2,3}"}') q;
  1242. ia
  1243. ---------
  1244. {1,2,3}
  1245. (1 row)
  1246. SELECT ia1 FROM json_populate_record(NULL::jsrec, '{"ia1": null}') q;
  1247. ia1
  1248. -----
  1249. (1 row)
  1250. SELECT ia1 FROM json_populate_record(NULL::jsrec, '{"ia1": 123}') q;
  1251. ERROR: expected JSON array
  1252. HINT: See the value of key "ia1".
  1253. SELECT ia1 FROM json_populate_record(NULL::jsrec, '{"ia1": [1, "2", null, 4]}') q;
  1254. ia1
  1255. --------------
  1256. {1,2,NULL,4}
  1257. (1 row)
  1258. SELECT ia1 FROM json_populate_record(NULL::jsrec, '{"ia1": [[1, 2, 3]]}') q;
  1259. ia1
  1260. -----------
  1261. {{1,2,3}}
  1262. (1 row)
  1263. SELECT ia1d FROM json_populate_record(NULL::jsrec, '{"ia1d": null}') q;
  1264. ia1d
  1265. ------
  1266. (1 row)
  1267. SELECT ia1d FROM json_populate_record(NULL::jsrec, '{"ia1d": 123}') q;
  1268. ERROR: expected JSON array
  1269. HINT: See the value of key "ia1d".
  1270. SELECT ia1d FROM json_populate_record(NULL::jsrec, '{"ia1d": [1, "2", null, 4]}') q;
  1271. ERROR: value for domain js_int_array_1d violates check constraint "js_int_array_1d_check"
  1272. SELECT ia1d FROM json_populate_record(NULL::jsrec, '{"ia1d": [1, "2", null]}') q;
  1273. ia1d
  1274. ------------
  1275. {1,2,NULL}
  1276. (1 row)
  1277. SELECT ia2 FROM json_populate_record(NULL::jsrec, '{"ia2": [1, "2", null, 4]}') q;
  1278. ia2
  1279. --------------
  1280. {1,2,NULL,4}
  1281. (1 row)
  1282. SELECT ia2 FROM json_populate_record(NULL::jsrec, '{"ia2": [[1, 2], [null, 4]]}') q;
  1283. ia2
  1284. ------------------
  1285. {{1,2},{NULL,4}}
  1286. (1 row)
  1287. SELECT ia2 FROM json_populate_record(NULL::jsrec, '{"ia2": [[], []]}') q;
  1288. ia2
  1289. -----
  1290. {}
  1291. (1 row)
  1292. SELECT ia2 FROM json_populate_record(NULL::jsrec, '{"ia2": [[1, 2], [3]]}') q;
  1293. ERROR: malformed JSON array
  1294. DETAIL: Multidimensional arrays must have sub-arrays with matching dimensions.
  1295. SELECT ia2 FROM json_populate_record(NULL::jsrec, '{"ia2": [[1, 2], 3, 4]}') q;
  1296. ERROR: expected JSON array
  1297. HINT: See the array element [1] of key "ia2".
  1298. SELECT ia2d FROM json_populate_record(NULL::jsrec, '{"ia2d": [[1, "2"], [null, 4]]}') q;
  1299. ERROR: value for domain js_int_array_2d violates check constraint "js_int_array_2d_check"
  1300. SELECT ia2d FROM json_populate_record(NULL::jsrec, '{"ia2d": [[1, "2", 3], [null, 5, 6]]}') q;
  1301. ia2d
  1302. ----------------------
  1303. {{1,2,3},{NULL,5,6}}
  1304. (1 row)
  1305. SELECT ia3 FROM json_populate_record(NULL::jsrec, '{"ia3": [1, "2", null, 4]}') q;
  1306. ia3
  1307. --------------
  1308. {1,2,NULL,4}
  1309. (1 row)
  1310. SELECT ia3 FROM json_populate_record(NULL::jsrec, '{"ia3": [[1, 2], [null, 4]]}') q;
  1311. ia3
  1312. ------------------
  1313. {{1,2},{NULL,4}}
  1314. (1 row)
  1315. SELECT ia3 FROM json_populate_record(NULL::jsrec, '{"ia3": [ [[], []], [[], []], [[], []] ]}') q;
  1316. ia3
  1317. -----
  1318. {}
  1319. (1 row)
  1320. SELECT ia3 FROM json_populate_record(NULL::jsrec, '{"ia3": [ [[1, 2]], [[3, 4]] ]}') q;
  1321. ia3
  1322. -------------------
  1323. {{{1,2}},{{3,4}}}
  1324. (1 row)
  1325. SELECT ia3 FROM json_populate_record(NULL::jsrec, '{"ia3": [ [[1, 2], [3, 4]], [[5, 6], [7, 8]] ]}') q;
  1326. ia3
  1327. -------------------------------
  1328. {{{1,2},{3,4}},{{5,6},{7,8}}}
  1329. (1 row)
  1330. SELECT ia3 FROM json_populate_record(NULL::jsrec, '{"ia3": [ [[1, 2], [3, 4]], [[5, 6], [7, 8], [9, 10]] ]}') q;
  1331. ERROR: malformed JSON array
  1332. DETAIL: Multidimensional arrays must have sub-arrays with matching dimensions.
  1333. SELECT ta FROM json_populate_record(NULL::jsrec, '{"ta": null}') q;
  1334. ta
  1335. ----
  1336. (1 row)
  1337. SELECT ta FROM json_populate_record(NULL::jsrec, '{"ta": 123}') q;
  1338. ERROR: expected JSON array
  1339. HINT: See the value of key "ta".
  1340. SELECT ta FROM json_populate_record(NULL::jsrec, '{"ta": [1, "2", null, 4]}') q;
  1341. ta
  1342. --------------
  1343. {1,2,NULL,4}
  1344. (1 row)
  1345. SELECT ta FROM json_populate_record(NULL::jsrec, '{"ta": [[1, 2, 3], {"k": "v"}]}') q;
  1346. ERROR: expected JSON array
  1347. HINT: See the array element [1] of key "ta".
  1348. SELECT c FROM json_populate_record(NULL::jsrec, '{"c": null}') q;
  1349. c
  1350. ---
  1351. (1 row)
  1352. SELECT c FROM json_populate_record(NULL::jsrec, '{"c": "aaa"}') q;
  1353. c
  1354. ------------
  1355. aaa
  1356. (1 row)
  1357. SELECT c FROM json_populate_record(NULL::jsrec, '{"c": "aaaaaaaaaa"}') q;
  1358. c
  1359. ------------
  1360. aaaaaaaaaa
  1361. (1 row)
  1362. SELECT c FROM json_populate_record(NULL::jsrec, '{"c": "aaaaaaaaaaaaa"}') q;
  1363. ERROR: value too long for type character(10)
  1364. SELECT ca FROM json_populate_record(NULL::jsrec, '{"ca": null}') q;
  1365. ca
  1366. ----
  1367. (1 row)
  1368. SELECT ca FROM json_populate_record(NULL::jsrec, '{"ca": 123}') q;
  1369. ERROR: expected JSON array
  1370. HINT: See the value of key "ca".
  1371. SELECT ca FROM json_populate_record(NULL::jsrec, '{"ca": [1, "2", null, 4]}') q;
  1372. ca
  1373. -----------------------------------------------
  1374. {"1 ","2 ",NULL,"4 "}
  1375. (1 row)
  1376. SELECT ca FROM json_populate_record(NULL::jsrec, '{"ca": ["aaaaaaaaaaaaaaaa"]}') q;
  1377. ERROR: value too long for type character(10)
  1378. SELECT ca FROM json_populate_record(NULL::jsrec, '{"ca": [[1, 2, 3], {"k": "v"}]}') q;
  1379. ERROR: expected JSON array
  1380. HINT: See the array element [1] of key "ca".
  1381. SELECT js FROM json_populate_record(NULL::jsrec, '{"js": null}') q;
  1382. js
  1383. ----
  1384. (1 row)
  1385. SELECT js FROM json_populate_record(NULL::jsrec, '{"js": true}') q;
  1386. js
  1387. ------
  1388. true
  1389. (1 row)
  1390. SELECT js FROM json_populate_record(NULL::jsrec, '{"js": 123.45}') q;
  1391. js
  1392. --------
  1393. 123.45
  1394. (1 row)
  1395. SELECT js FROM json_populate_record(NULL::jsrec, '{"js": "123.45"}') q;
  1396. js
  1397. ----------
  1398. "123.45"
  1399. (1 row)
  1400. SELECT js FROM json_populate_record(NULL::jsrec, '{"js": "abc"}') q;
  1401. js
  1402. -------
  1403. "abc"
  1404. (1 row)
  1405. SELECT js FROM json_populate_record(NULL::jsrec, '{"js": [123, "123", null, {"key": "value"}]}') q;
  1406. js
  1407. --------------------------------------
  1408. [123, "123", null, {"key": "value"}]
  1409. (1 row)
  1410. SELECT js FROM json_populate_record(NULL::jsrec, '{"js": {"a": "bbb", "b": null, "c": 123.45}}') q;
  1411. js
  1412. --------------------------------------
  1413. {"a": "bbb", "b": null, "c": 123.45}
  1414. (1 row)
  1415. SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": null}') q;
  1416. jsb
  1417. -----
  1418. (1 row)
  1419. SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": true}') q;
  1420. jsb
  1421. ------
  1422. true
  1423. (1 row)
  1424. SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": 123.45}') q;
  1425. jsb
  1426. --------
  1427. 123.45
  1428. (1 row)
  1429. SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": "123.45"}') q;
  1430. jsb
  1431. ----------
  1432. "123.45"
  1433. (1 row)
  1434. SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": "abc"}') q;
  1435. jsb
  1436. -------
  1437. "abc"
  1438. (1 row)
  1439. SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": [123, "123", null, {"key": "value"}]}') q;
  1440. jsb
  1441. --------------------------------------
  1442. [123, "123", null, {"key": "value"}]
  1443. (1 row)
  1444. SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": {"a": "bbb", "b": null, "c": 123.45}}') q;
  1445. jsb
  1446. --------------------------------------
  1447. {"a": "bbb", "b": null, "c": 123.45}
  1448. (1 row)
  1449. SELECT jsa FROM json_populate_record(NULL::jsrec, '{"jsa": null}') q;
  1450. jsa
  1451. -----
  1452. (1 row)
  1453. SELECT jsa FROM json_populate_record(NULL::jsrec, '{"jsa": 123}') q;
  1454. ERROR: expected JSON array
  1455. HINT: See the value of key "jsa".
  1456. SELECT jsa FROM json_populate_record(NULL::jsrec, '{"jsa": [1, "2", null, 4]}') q;
  1457. jsa
  1458. --------------------
  1459. {1,"\"2\"",NULL,4}
  1460. (1 row)
  1461. SELECT jsa FROM json_populate_record(NULL::jsrec, '{"jsa": ["aaa", null, [1, 2, "3", {}], { "k" : "v" }]}') q;
  1462. jsa
  1463. ----------------------------------------------------------
  1464. {"\"aaa\"",NULL,"[1, 2, \"3\", {}]","{ \"k\" : \"v\" }"}
  1465. (1 row)
  1466. SELECT rec FROM json_populate_record(NULL::jsrec, '{"rec": 123}') q;
  1467. ERROR: cannot call populate_composite on a scalar
  1468. SELECT rec FROM json_populate_record(NULL::jsrec, '{"rec": [1, 2]}') q;
  1469. ERROR: cannot call populate_composite on an array
  1470. SELECT rec FROM json_populate_record(NULL::jsrec, '{"rec": {"a": "abc", "c": "01.02.2003", "x": 43.2}}') q;
  1471. rec
  1472. -----------------------------------
  1473. (abc,,"Thu Jan 02 00:00:00 2003")
  1474. (1 row)
  1475. SELECT rec FROM json_populate_record(NULL::jsrec, '{"rec": "(abc,42,01.02.2003)"}') q;
  1476. rec
  1477. -------------------------------------
  1478. (abc,42,"Thu Jan 02 00:00:00 2003")
  1479. (1 row)
  1480. SELECT reca FROM json_populate_record(NULL::jsrec, '{"reca": 123}') q;
  1481. ERROR: expected JSON array
  1482. HINT: See the value of key "reca".
  1483. SELECT reca FROM json_populate_record(NULL::jsrec, '{"reca": [1, 2]}') q;
  1484. ERROR: cannot call populate_composite on a scalar
  1485. SELECT reca FROM json_populate_record(NULL::jsrec, '{"reca": [{"a": "abc", "b": 456}, null, {"c": "01.02.2003", "x": 43.2}]}') q;
  1486. reca
  1487. --------------------------------------------------------
  1488. {"(abc,456,)",NULL,"(,,\"Thu Jan 02 00:00:00 2003\")"}
  1489. (1 row)
  1490. SELECT reca FROM json_populate_record(NULL::jsrec, '{"reca": ["(abc,42,01.02.2003)"]}') q;
  1491. reca
  1492. -------------------------------------------
  1493. {"(abc,42,\"Thu Jan 02 00:00:00 2003\")"}
  1494. (1 row)
  1495. SELECT reca FROM json_populate_record(NULL::jsrec, '{"reca": "{\"(abc,42,01.02.2003)\"}"}') q;
  1496. reca
  1497. -------------------------------------------
  1498. {"(abc,42,\"Thu Jan 02 00:00:00 2003\")"}
  1499. (1 row)
  1500. SELECT rec FROM json_populate_record(
  1501. row(NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
  1502. row('x',3,'2012-12-31 15:30:56')::jpop,NULL)::jsrec,
  1503. '{"rec": {"a": "abc", "c": "01.02.2003", "x": 43.2}}'
  1504. ) q;
  1505. rec
  1506. ------------------------------------
  1507. (abc,3,"Thu Jan 02 00:00:00 2003")
  1508. (1 row)
  1509. -- anonymous record type
  1510. SELECT json_populate_record(null::record, '{"x": 0, "y": 1}');
  1511. ERROR: could not determine row type for result of json_populate_record
  1512. HINT: Provide a non-null record argument, or call the function in the FROM clause using a column definition list.
  1513. SELECT json_populate_record(row(1,2), '{"f1": 0, "f2": 1}');
  1514. json_populate_record
  1515. ----------------------
  1516. (0,1)
  1517. (1 row)
  1518. SELECT * FROM
  1519. json_populate_record(null::record, '{"x": 776}') AS (x int, y int);
  1520. x | y
  1521. -----+---
  1522. 776 |
  1523. (1 row)
  1524. -- composite domain
  1525. SELECT json_populate_record(null::j_ordered_pair, '{"x": 0, "y": 1}');
  1526. json_populate_record
  1527. ----------------------
  1528. (0,1)
  1529. (1 row)
  1530. SELECT json_populate_record(row(1,2)::j_ordered_pair, '{"x": 0}');
  1531. json_populate_record
  1532. ----------------------
  1533. (0,2)
  1534. (1 row)
  1535. SELECT json_populate_record(row(1,2)::j_ordered_pair, '{"x": 1, "y": 0}');
  1536. ERROR: value for domain j_ordered_pair violates check constraint "j_ordered_pair_check"
  1537. -- populate_recordset
  1538. select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
  1539. a | b | c
  1540. --------+---+--------------------------
  1541. blurfl | |
  1542. | 3 | Fri Jan 20 10:42:53 2012
  1543. (2 rows)
  1544. 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;
  1545. a | b | c
  1546. --------+----+--------------------------
  1547. blurfl | 99 |
  1548. def | 3 | Fri Jan 20 10:42:53 2012
  1549. (2 rows)
  1550. select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
  1551. a | b | c
  1552. --------+---+--------------------------
  1553. blurfl | |
  1554. | 3 | Fri Jan 20 10:42:53 2012
  1555. (2 rows)
  1556. 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;
  1557. a | b | c
  1558. --------+----+--------------------------
  1559. blurfl | 99 |
  1560. def | 3 | Fri Jan 20 10:42:53 2012
  1561. (2 rows)
  1562. 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;
  1563. a | b | c
  1564. ---------------+----+--------------------------
  1565. [100,200,300] | 99 |
  1566. {"z":true} | 3 | Fri Jan 20 10:42:53 2012
  1567. (2 rows)
  1568. 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;
  1569. ERROR: invalid input syntax for type timestamp: "[100,200,300]"
  1570. create type jpop2 as (a int, b json, c int, d int);
  1571. select * from json_populate_recordset(null::jpop2, '[{"a":2,"c":3,"b":{"z":4},"d":6}]') q;
  1572. a | b | c | d
  1573. ---+---------+---+---
  1574. 2 | {"z":4} | 3 | 6
  1575. (1 row)
  1576. select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
  1577. a | b | c
  1578. --------+---+--------------------------
  1579. blurfl | |
  1580. | 3 | Fri Jan 20 10:42:53 2012
  1581. (2 rows)
  1582. 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;
  1583. a | b | c
  1584. --------+----+--------------------------
  1585. blurfl | 99 |
  1586. def | 3 | Fri Jan 20 10:42:53 2012
  1587. (2 rows)
  1588. 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;
  1589. a | b | c
  1590. ---------------+----+--------------------------
  1591. [100,200,300] | 99 |
  1592. {"z":true} | 3 | Fri Jan 20 10:42:53 2012
  1593. (2 rows)
  1594. -- anonymous record type
  1595. SELECT json_populate_recordset(null::record, '[{"x": 0, "y": 1}]');
  1596. ERROR: could not determine row type for result of json_populate_recordset
  1597. HINT: Provide a non-null record argument, or call the function in the FROM clause using a column definition list.
  1598. SELECT json_populate_recordset(row(1,2), '[{"f1": 0, "f2": 1}]');
  1599. json_populate_recordset
  1600. -------------------------
  1601. (0,1)
  1602. (1 row)
  1603. SELECT i, json_populate_recordset(row(i,50), '[{"f1":"42"},{"f2":"43"}]')
  1604. FROM (VALUES (1),(2)) v(i);
  1605. i | json_populate_recordset
  1606. ---+-------------------------
  1607. 1 | (42,50)
  1608. 1 | (1,43)
  1609. 2 | (42,50)
  1610. 2 | (2,43)
  1611. (4 rows)
  1612. SELECT * FROM
  1613. json_populate_recordset(null::record, '[{"x": 776}]') AS (x int, y int);
  1614. x | y
  1615. -----+---
  1616. 776 |
  1617. (1 row)
  1618. -- empty array is a corner case
  1619. SELECT json_populate_recordset(null::record, '[]');
  1620. ERROR: could not determine row type for result of json_populate_recordset
  1621. HINT: Provide a non-null record argument, or call the function in the FROM clause using a column definition list.
  1622. SELECT json_populate_recordset(row(1,2), '[]');
  1623. json_populate_recordset
  1624. -------------------------
  1625. (0 rows)
  1626. SELECT * FROM json_populate_recordset(NULL::jpop,'[]') q;
  1627. a | b | c
  1628. ---+---+---
  1629. (0 rows)
  1630. SELECT * FROM
  1631. json_populate_recordset(null::record, '[]') AS (x int, y int);
  1632. x | y
  1633. ---+---
  1634. (0 rows)
  1635. -- composite domain
  1636. SELECT json_populate_recordset(null::j_ordered_pair, '[{"x": 0, "y": 1}]');
  1637. json_populate_recordset
  1638. -------------------------
  1639. (0,1)
  1640. (1 row)
  1641. SELECT json_populate_recordset(row(1,2)::j_ordered_pair, '[{"x": 0}, {"y": 3}]');
  1642. json_populate_recordset
  1643. -------------------------
  1644. (0,2)
  1645. (1,3)
  1646. (2 rows)
  1647. SELECT json_populate_recordset(row(1,2)::j_ordered_pair, '[{"x": 1, "y": 0}]');
  1648. ERROR: value for domain j_ordered_pair violates check constraint "j_ordered_pair_check"
  1649. -- negative cases where the wrong record type is supplied
  1650. select * from json_populate_recordset(row(0::int),'[{"a":"1","b":"2"},{"a":"3"}]') q (a text, b text);
  1651. ERROR: function return row and query-specified return row do not match
  1652. DETAIL: Returned row contains 1 attribute, but query expects 2.
  1653. select * from json_populate_recordset(row(0::int,0::int),'[{"a":"1","b":"2"},{"a":"3"}]') q (a text, b text);
  1654. ERROR: function return row and query-specified return row do not match
  1655. DETAIL: Returned type integer at ordinal position 1, but query expects text.
  1656. select * from json_populate_recordset(row(0::int,0::int,0::int),'[{"a":"1","b":"2"},{"a":"3"}]') q (a text, b text);
  1657. ERROR: function return row and query-specified return row do not match
  1658. DETAIL: Returned row contains 3 attributes, but query expects 2.
  1659. select * from json_populate_recordset(row(1000000000::int,50::int),'[{"b":"2"},{"a":"3"}]') q (a text, b text);
  1660. ERROR: function return row and query-specified return row do not match
  1661. DETAIL: Returned type integer at ordinal position 1, but query expects text.
  1662. -- test type info caching in json_populate_record()
  1663. CREATE TEMP TABLE jspoptest (js json);
  1664. INSERT INTO jspoptest
  1665. SELECT '{
  1666. "jsa": [1, "2", null, 4],
  1667. "rec": {"a": "abc", "c": "01.02.2003", "x": 43.2},
  1668. "reca": [{"a": "abc", "b": 456}, null, {"c": "01.02.2003", "x": 43.2}]
  1669. }'::json
  1670. FROM generate_series(1, 3);
  1671. SELECT (json_populate_record(NULL::jsrec, js)).* FROM jspoptest;
  1672. i | ia | ia1 | ia2 | ia3 | ia1d | ia2d | t | ta | c | ca | ts | js | jsb | jsa | rec | reca
  1673. ---+----+-----+-----+-----+------+------+---+----+---+----+----+----+-----+--------------------+-----------------------------------+--------------------------------------------------------
  1674. | | | | | | | | | | | | | | {1,"\"2\"",NULL,4} | (abc,,"Thu Jan 02 00:00:00 2003") | {"(abc,456,)",NULL,"(,,\"Thu Jan 02 00:00:00 2003\")"}
  1675. | | | | | | | | | | | | | | {1,"\"2\"",NULL,4} | (abc,,"Thu Jan 02 00:00:00 2003") | {"(abc,456,)",NULL,"(,,\"Thu Jan 02 00:00:00 2003\")"}
  1676. | | | | | | | | | | | | | | {1,"\"2\"",NULL,4} | (abc,,"Thu Jan 02 00:00:00 2003") | {"(abc,456,)",NULL,"(,,\"Thu Jan 02 00:00:00 2003\")"}
  1677. (3 rows)
  1678. DROP TYPE jsrec;
  1679. DROP TYPE jsrec_i_not_null;
  1680. DROP DOMAIN js_int_not_null;
  1681. DROP DOMAIN js_int_array_1d;
  1682. DROP DOMAIN js_int_array_2d;
  1683. DROP DOMAIN j_ordered_pair;
  1684. DROP TYPE j_unordered_pair;
  1685. --json_typeof() function
  1686. select value, json_typeof(value)
  1687. from (values (json '123.4'),
  1688. (json '-1'),
  1689. (json '"foo"'),
  1690. (json 'true'),
  1691. (json 'false'),
  1692. (json 'null'),
  1693. (json '[1, 2, 3]'),
  1694. (json '[]'),
  1695. (json '{"x":"foo", "y":123}'),
  1696. (json '{}'),
  1697. (NULL::json))
  1698. as data(value);
  1699. value | json_typeof
  1700. ----------------------+-------------
  1701. 123.4 | number
  1702. -1 | number
  1703. "foo" | string
  1704. true | boolean
  1705. false | boolean
  1706. null | null
  1707. [1, 2, 3] | array
  1708. [] | array
  1709. {"x":"foo", "y":123} | object
  1710. {} | object
  1711. |
  1712. (11 rows)
  1713. -- json_build_array, json_build_object, json_object_agg
  1714. SELECT json_build_array('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
  1715. json_build_array
  1716. -----------------------------------------------------------------------
  1717. ["a", 1, "b", 1.2, "c", true, "d", null, "e", {"x": 3, "y": [1,2,3]}]
  1718. (1 row)
  1719. SELECT json_build_array('a', NULL); -- ok
  1720. json_build_array
  1721. ------------------
  1722. ["a", null]
  1723. (1 row)
  1724. SELECT json_build_array(VARIADIC NULL::text[]); -- ok
  1725. json_build_array
  1726. ------------------
  1727. (1 row)
  1728. SELECT json_build_array(VARIADIC '{}'::text[]); -- ok
  1729. json_build_array
  1730. ------------------
  1731. []
  1732. (1 row)
  1733. SELECT json_build_array(VARIADIC '{a,b,c}'::text[]); -- ok
  1734. json_build_array
  1735. ------------------
  1736. ["a", "b", "c"]
  1737. (1 row)
  1738. SELECT json_build_array(VARIADIC ARRAY['a', NULL]::text[]); -- ok
  1739. json_build_array
  1740. ------------------
  1741. ["a", null]
  1742. (1 row)
  1743. SELECT json_build_array(VARIADIC '{1,2,3,4}'::text[]); -- ok
  1744. json_build_array
  1745. ----------------------
  1746. ["1", "2", "3", "4"]
  1747. (1 row)
  1748. SELECT json_build_array(VARIADIC '{1,2,3,4}'::int[]); -- ok
  1749. json_build_array
  1750. ------------------
  1751. [1, 2, 3, 4]
  1752. (1 row)
  1753. SELECT json_build_array(VARIADIC '{{1,4},{2,5},{3,6}}'::int[][]); -- ok
  1754. json_build_array
  1755. --------------------
  1756. [1, 4, 2, 5, 3, 6]
  1757. (1 row)
  1758. SELECT json_build_object('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
  1759. json_build_object
  1760. ----------------------------------------------------------------------------
  1761. {"a" : 1, "b" : 1.2, "c" : true, "d" : null, "e" : {"x": 3, "y": [1,2,3]}}
  1762. (1 row)
  1763. SELECT json_build_object(
  1764. 'a', json_build_object('b',false,'c',99),
  1765. 'd', json_build_object('e',array[9,8,7]::int[],
  1766. 'f', (select row_to_json(r) from ( select relkind, oid::regclass as name from pg_class where relname = 'pg_class') r)));
  1767. json_build_object
  1768. -------------------------------------------------------------------------------------------------
  1769. {"a" : {"b" : false, "c" : 99}, "d" : {"e" : [9,8,7], "f" : {"relkind":"r","name":"pg_class"}}}
  1770. (1 row)
  1771. SELECT json_build_object('{a,b,c}'::text[]); -- error
  1772. ERROR: argument list must have even number of elements
  1773. HINT: The arguments of json_build_object() must consist of alternating keys and values.
  1774. SELECT json_build_object('{a,b,c}'::text[], '{d,e,f}'::text[]); -- error, key cannot be array
  1775. ERROR: key value must be scalar, not array, composite, or json
  1776. SELECT json_build_object('a', 'b', 'c'); -- error
  1777. ERROR: argument list must have even number of elements
  1778. HINT: The arguments of json_build_object() must consist of alternating keys and values.
  1779. SELECT json_build_object(NULL, 'a'); -- error, key cannot be NULL
  1780. ERROR: argument 1 cannot be null
  1781. HINT: Object keys should be text.
  1782. SELECT json_build_object('a', NULL); -- ok
  1783. json_build_object
  1784. -------------------
  1785. {"a" : null}
  1786. (1 row)
  1787. SELECT json_build_object(VARIADIC NULL::text[]); -- ok
  1788. json_build_object
  1789. -------------------
  1790. (1 row)
  1791. SELECT json_build_object(VARIADIC '{}'::text[]); -- ok
  1792. json_build_object
  1793. -------------------
  1794. {}
  1795. (1 row)
  1796. SELECT json_build_object(VARIADIC '{a,b,c}'::text[]); -- error
  1797. ERROR: argument list must have even number of elements
  1798. HINT: The arguments of json_build_object() must consist of alternating keys and values.
  1799. SELECT json_build_object(VARIADIC ARRAY['a', NULL]::text[]); -- ok
  1800. json_build_object
  1801. -------------------
  1802. {"a" : null}
  1803. (1 row)
  1804. SELECT json_build_object(VARIADIC ARRAY[NULL, 'a']::text[]); -- error, key cannot be NULL
  1805. ERROR: argument 1 cannot be null
  1806. HINT: Object keys should be text.
  1807. SELECT json_build_object(VARIADIC '{1,2,3,4}'::text[]); -- ok
  1808. json_build_object
  1809. ------------------------
  1810. {"1" : "2", "3" : "4"}
  1811. (1 row)
  1812. SELECT json_build_object(VARIADIC '{1,2,3,4}'::int[]); -- ok
  1813. json_build_object
  1814. --------------------
  1815. {"1" : 2, "3" : 4}
  1816. (1 row)
  1817. SELECT json_build_object(VARIADIC '{{1,4},{2,5},{3,6}}'::int[][]); -- ok
  1818. json_build_object
  1819. -----------------------------
  1820. {"1" : 4, "2" : 5, "3" : 6}
  1821. (1 row)
  1822. -- empty objects/arrays
  1823. SELECT json_build_array();
  1824. json_build_array
  1825. ------------------
  1826. []
  1827. (1 row)
  1828. SELECT json_build_object();
  1829. json_build_object
  1830. -------------------
  1831. {}
  1832. (1 row)
  1833. -- make sure keys are quoted
  1834. SELECT json_build_object(1,2);
  1835. json_build_object
  1836. -------------------
  1837. {"1" : 2}
  1838. (1 row)
  1839. -- keys must be scalar and not null
  1840. SELECT json_build_object(null,2);
  1841. ERROR: argument 1 cannot be null
  1842. HINT: Object keys should be text.
  1843. SELECT json_build_object(r,2) FROM (SELECT 1 AS a, 2 AS b) r;
  1844. ERROR: key value must be scalar, not array, composite, or json
  1845. SELECT json_build_object(json '{"a":1,"b":2}', 3);
  1846. ERROR: key value must be scalar, not array, composite, or json
  1847. SELECT json_build_object('{1,2,3}'::int[], 3);
  1848. ERROR: key value must be scalar, not array, composite, or json
  1849. CREATE TEMP TABLE foo (serial_num int, name text, type text);
  1850. INSERT INTO foo VALUES (847001,'t15','GE1043');
  1851. INSERT INTO foo VALUES (847002,'t16','GE1043');
  1852. INSERT INTO foo VALUES (847003,'sub-alpha','GESS90');
  1853. SELECT json_build_object('turbines',json_object_agg(serial_num,json_build_object('name',name,'type',type)))
  1854. FROM foo;
  1855. json_build_object
  1856. -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  1857. {"turbines" : { "847001" : {"name" : "t15", "type" : "GE1043"}, "847002" : {"name" : "t16", "type" : "GE1043"}, "847003" : {"name" : "sub-alpha", "type" : "GESS90"} }}
  1858. (1 row)
  1859. SELECT json_object_agg(name, type) FROM foo;
  1860. json_object_agg
  1861. ----------------------------------------------------------------
  1862. { "t15" : "GE1043", "t16" : "GE1043", "sub-alpha" : "GESS90" }
  1863. (1 row)
  1864. INSERT INTO foo VALUES (999999, NULL, 'bar');
  1865. SELECT json_object_agg(name, type) FROM foo;
  1866. ERROR: field name must not be null
  1867. -- json_object
  1868. -- empty object, one dimension
  1869. SELECT json_object('{}');
  1870. json_object
  1871. -------------
  1872. {}
  1873. (1 row)
  1874. -- empty object, two dimensions
  1875. SELECT json_object('{}', '{}');
  1876. json_object
  1877. -------------
  1878. {}
  1879. (1 row)
  1880. -- one dimension
  1881. SELECT json_object('{a,1,b,2,3,NULL,"d e f","a b c"}');
  1882. json_object
  1883. -------------------------------------------------------
  1884. {"a" : "1", "b" : "2", "3" : null, "d e f" : "a b c"}
  1885. (1 row)
  1886. -- same but with two dimensions
  1887. SELECT json_object('{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}');
  1888. json_object
  1889. -------------------------------------------------------
  1890. {"a" : "1", "b" : "2", "3" : null, "d e f" : "a b c"}
  1891. (1 row)
  1892. -- odd number error
  1893. SELECT json_object('{a,b,c}');
  1894. ERROR: array must have even number of elements
  1895. -- one column error
  1896. SELECT json_object('{{a},{b}}');
  1897. ERROR: array must have two columns
  1898. -- too many columns error
  1899. SELECT json_object('{{a,b,c},{b,c,d}}');
  1900. ERROR: array must have two columns
  1901. -- too many dimensions error
  1902. SELECT json_object('{{{a,b},{c,d}},{{b,c},{d,e}}}');
  1903. ERROR: wrong number of array subscripts
  1904. --two argument form of json_object
  1905. select json_object('{a,b,c,"d e f"}','{1,2,3,"a b c"}');
  1906. json_object
  1907. ------------------------------------------------------
  1908. {"a" : "1", "b" : "2", "c" : "3", "d e f" : "a b c"}
  1909. (1 row)
  1910. -- too many dimensions
  1911. 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"}}');
  1912. ERROR: wrong number of array subscripts
  1913. -- mismatched dimensions
  1914. select json_object('{a,b,c,"d e f",g}','{1,2,3,"a b c"}');
  1915. ERROR: mismatched array dimensions
  1916. select json_object('{a,b,c,"d e f"}','{1,2,3,"a b c",g}');
  1917. ERROR: mismatched array dimensions
  1918. -- null key error
  1919. select json_object('{a,b,NULL,"d e f"}','{1,2,3,"a b c"}');
  1920. ERROR: null value not allowed for object key
  1921. -- empty key is allowed
  1922. select json_object('{a,b,"","d e f"}','{1,2,3,"a b c"}');
  1923. json_object
  1924. -----------------------------------------------------
  1925. {"a" : "1", "b" : "2", "" : "3", "d e f" : "a b c"}
  1926. (1 row)
  1927. -- json_to_record and json_to_recordset
  1928. select * from json_to_record('{"a":1,"b":"foo","c":"bar"}')
  1929. as x(a int, b text, d text);
  1930. a | b | d
  1931. ---+-----+---
  1932. 1 | foo |
  1933. (1 row)
  1934. select * from json_to_recordset('[{"a":1,"b":"foo","d":false},{"a":2,"b":"bar","c":true}]')
  1935. as x(a int, b text, c boolean);
  1936. a | b | c
  1937. ---+-----+---
  1938. 1 | foo |
  1939. 2 | bar | t
  1940. (2 rows)
  1941. select * from json_to_recordset('[{"a":1,"b":{"d":"foo"},"c":true},{"a":2,"c":false,"b":{"d":"bar"}}]')
  1942. as x(a int, b json, c boolean);
  1943. a | b | c
  1944. ---+-------------+---
  1945. 1 | {"d":"foo"} | t
  1946. 2 | {"d":"bar"} | f
  1947. (2 rows)
  1948. select *, c is null as c_is_null
  1949. 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)
  1950. as t(a int, b json, c text, x int, ca char(5)[], ia int[][], r jpop);
  1951. a | b | c | x | ca | ia | r | c_is_null
  1952. ---+-----------------+---+---+-------------------+---------------+------------+-----------
  1953. 1 | {"c":16, "d":2} | | 8 | {"1 2 ","3 "} | {{1,2},{3,4}} | (aaa,123,) | t
  1954. (1 row)
  1955. select *, c is null as c_is_null
  1956. from json_to_recordset('[{"a":1, "b":{"c":16, "d":2}, "x":8}]'::json)
  1957. as t(a int, b json, c text, x int);
  1958. a | b | c | x | c_is_null
  1959. ---+-----------------+---+---+-----------
  1960. 1 | {"c":16, "d":2} | | 8 | t
  1961. (1 row)
  1962. select * from json_to_record('{"ia": null}') as x(ia _int4);
  1963. ia
  1964. ----
  1965. (1 row)
  1966. select * from json_to_record('{"ia": 123}') as x(ia _int4);
  1967. ERROR: expected JSON array
  1968. HINT: See the value of key "ia".
  1969. select * from json_to_record('{"ia": [1, "2", null, 4]}') as x(ia _int4);
  1970. ia
  1971. --------------
  1972. {1,2,NULL,4}
  1973. (1 row)
  1974. select * from json_to_record('{"ia": [[1, 2], [3, 4]]}') as x(ia _int4);
  1975. ia
  1976. ---------------
  1977. {{1,2},{3,4}}
  1978. (1 row)
  1979. select * from json_to_record('{"ia": [[1], 2]}') as x(ia _int4);
  1980. ERROR: expected JSON array
  1981. HINT: See the array element [1] of key "ia".
  1982. select * from json_to_record('{"ia": [[1], [2, 3]]}') as x(ia _int4);
  1983. ERROR: malformed JSON array
  1984. DETAIL: Multidimensional arrays must have sub-arrays with matching dimensions.
  1985. select * from json_to_record('{"ia2": [1, 2, 3]}') as x(ia2 int[][]);
  1986. ia2
  1987. ---------
  1988. {1,2,3}
  1989. (1 row)
  1990. select * from json_to_record('{"ia2": [[1, 2], [3, 4]]}') as x(ia2 int4[][]);
  1991. ia2
  1992. ---------------
  1993. {{1,2},{3,4}}
  1994. (1 row)
  1995. select * from json_to_record('{"ia2": [[[1], [2], [3]]]}') as x(ia2 int4[][]);
  1996. ia2
  1997. -----------------
  1998. {{{1},{2},{3}}}
  1999. (1 row)
  2000. select * from json_to_record('{"out": {"key": 1}}') as x(out json);
  2001. out
  2002. ------------
  2003. {"key": 1}
  2004. (1 row)
  2005. select * from json_to_record('{"out": [{"key": 1}]}') as x(out json);
  2006. out
  2007. --------------
  2008. [{"key": 1}]
  2009. (1 row)
  2010. select * from json_to_record('{"out": "{\"key\": 1}"}') as x(out json);
  2011. out
  2012. ----------------
  2013. "{\"key\": 1}"
  2014. (1 row)
  2015. select * from json_to_record('{"out": {"key": 1}}') as x(out jsonb);
  2016. out
  2017. ------------
  2018. {"key": 1}
  2019. (1 row)
  2020. select * from json_to_record('{"out": [{"key": 1}]}') as x(out jsonb);
  2021. out
  2022. --------------
  2023. [{"key": 1}]
  2024. (1 row)
  2025. select * from json_to_record('{"out": "{\"key\": 1}"}') as x(out jsonb);
  2026. out
  2027. ----------------
  2028. "{\"key\": 1}"
  2029. (1 row)
  2030. -- json_strip_nulls
  2031. select json_strip_nulls(null);
  2032. json_strip_nulls
  2033. ------------------
  2034. (1 row)
  2035. select json_strip_nulls('1');
  2036. json_strip_nulls
  2037. ------------------
  2038. 1
  2039. (1 row)
  2040. select json_strip_nulls('"a string"');
  2041. json_strip_nulls
  2042. ------------------
  2043. "a string"
  2044. (1 row)
  2045. select json_strip_nulls('null');
  2046. json_strip_nulls
  2047. ------------------
  2048. null
  2049. (1 row)
  2050. select json_strip_nulls('[1,2,null,3,4]');
  2051. json_strip_nulls
  2052. ------------------
  2053. [1,2,null,3,4]
  2054. (1 row)
  2055. select json_strip_nulls('{"a":1,"b":null,"c":[2,null,3],"d":{"e":4,"f":null}}');
  2056. json_strip_nulls
  2057. ------------------------------------
  2058. {"a":1,"c":[2,null,3],"d":{"e":4}}
  2059. (1 row)
  2060. select json_strip_nulls('[1,{"a":1,"b":null,"c":2},3]');
  2061. json_strip_nulls
  2062. ---------------------
  2063. [1,{"a":1,"c":2},3]
  2064. (1 row)
  2065. -- an empty object is not null and should not be stripped
  2066. select json_strip_nulls('{"a": {"b": null, "c": null}, "d": {} }');
  2067. json_strip_nulls
  2068. ------------------
  2069. {"a":{},"d":{}}
  2070. (1 row)
  2071. -- json to tsvector
  2072. select to_tsvector('{"a": "aaa bbb ddd ccc", "b": ["eee fff ggg"], "c": {"d": "hhh iii"}}'::json);
  2073. to_tsvector
  2074. ---------------------------------------------------------------------------
  2075. 'aaa':1 'bbb':2 'ccc':4 'ddd':3 'eee':6 'fff':7 'ggg':8 'hhh':10 'iii':11
  2076. (1 row)
  2077. -- json to tsvector with config
  2078. select to_tsvector('simple', '{"a": "aaa bbb ddd ccc", "b": ["eee fff ggg"], "c": {"d": "hhh iii"}}'::json);
  2079. to_tsvector
  2080. ---------------------------------------------------------------------------
  2081. 'aaa':1 'bbb':2 'ccc':4 'ddd':3 'eee':6 'fff':7 'ggg':8 'hhh':10 'iii':11
  2082. (1 row)
  2083. -- json to tsvector with stop words
  2084. select to_tsvector('english', '{"a": "aaa in bbb ddd ccc", "b": ["the eee fff ggg"], "c": {"d": "hhh. iii"}}'::json);
  2085. to_tsvector
  2086. ----------------------------------------------------------------------------
  2087. 'aaa':1 'bbb':3 'ccc':5 'ddd':4 'eee':8 'fff':9 'ggg':10 'hhh':12 'iii':13
  2088. (1 row)
  2089. -- json to tsvector with numeric values
  2090. select to_tsvector('english', '{"a": "aaa in bbb ddd ccc", "b": 123, "c": 456}'::json);
  2091. to_tsvector
  2092. ---------------------------------
  2093. 'aaa':1 'bbb':3 'ccc':5 'ddd':4
  2094. (1 row)
  2095. -- json_to_tsvector
  2096. select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '"all"');
  2097. json_to_tsvector
  2098. ----------------------------------------------------------------------------------------
  2099. '123':8 '456':12 'aaa':2 'b':6 'bbb':4 'c':10 'd':14 'f':18 'fals':20 'g':22 'true':16
  2100. (1 row)
  2101. select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '"key"');
  2102. json_to_tsvector
  2103. --------------------------------
  2104. 'b':2 'c':4 'd':6 'f':8 'g':10
  2105. (1 row)
  2106. select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '"string"');
  2107. json_to_tsvector
  2108. ------------------
  2109. 'aaa':1 'bbb':3
  2110. (1 row)
  2111. select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '"numeric"');
  2112. json_to_tsvector
  2113. ------------------
  2114. '123':1 '456':3
  2115. (1 row)
  2116. select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '"boolean"');
  2117. json_to_tsvector
  2118. -------------------
  2119. 'fals':3 'true':1
  2120. (1 row)
  2121. select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '["string", "numeric"]');
  2122. json_to_tsvector
  2123. ---------------------------------
  2124. '123':5 '456':7 'aaa':1 'bbb':3
  2125. (1 row)
  2126. select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '"all"');
  2127. json_to_tsvector
  2128. ----------------------------------------------------------------------------------------
  2129. '123':8 '456':12 'aaa':2 'b':6 'bbb':4 'c':10 'd':14 'f':18 'fals':20 'g':22 'true':16
  2130. (1 row)
  2131. select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '"key"');
  2132. json_to_tsvector
  2133. --------------------------------
  2134. 'b':2 'c':4 'd':6 'f':8 'g':10
  2135. (1 row)
  2136. select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '"string"');
  2137. json_to_tsvector
  2138. ------------------
  2139. 'aaa':1 'bbb':3
  2140. (1 row)
  2141. select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '"numeric"');
  2142. json_to_tsvector
  2143. ------------------
  2144. '123':1 '456':3
  2145. (1 row)
  2146. select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '"boolean"');
  2147. json_to_tsvector
  2148. -------------------
  2149. 'fals':3 'true':1
  2150. (1 row)
  2151. select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '["string", "numeric"]');
  2152. json_to_tsvector
  2153. ---------------------------------
  2154. '123':5 '456':7 'aaa':1 'bbb':3
  2155. (1 row)
  2156. -- to_tsvector corner cases
  2157. select to_tsvector('""'::json);
  2158. to_tsvector
  2159. -------------
  2160. (1 row)
  2161. select to_tsvector('{}'::json);
  2162. to_tsvector
  2163. -------------
  2164. (1 row)
  2165. select to_tsvector('[]'::json);
  2166. to_tsvector
  2167. -------------
  2168. (1 row)
  2169. select to_tsvector('null'::json);
  2170. to_tsvector
  2171. -------------
  2172. (1 row)
  2173. -- json_to_tsvector corner cases
  2174. select json_to_tsvector('""'::json, '"all"');
  2175. json_to_tsvector
  2176. ------------------
  2177. (1 row)
  2178. select json_to_tsvector('{}'::json, '"all"');
  2179. json_to_tsvector
  2180. ------------------
  2181. (1 row)
  2182. select json_to_tsvector('[]'::json, '"all"');
  2183. json_to_tsvector
  2184. ------------------
  2185. (1 row)
  2186. select json_to_tsvector('null'::json, '"all"');
  2187. json_to_tsvector
  2188. ------------------
  2189. (1 row)
  2190. select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '""');
  2191. ERROR: wrong flag in flag array: ""
  2192. HINT: Possible values are: "string", "numeric", "boolean", "key", and "all".
  2193. select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '{}');
  2194. ERROR: wrong flag type, only arrays and scalars are allowed
  2195. select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '[]');
  2196. json_to_tsvector
  2197. ------------------
  2198. (1 row)
  2199. select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, 'null');
  2200. ERROR: flag array element is not a string
  2201. HINT: Possible values are: "string", "numeric", "boolean", "key", and "all".
  2202. select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '["all", null]');
  2203. ERROR: flag array element is not a string
  2204. HINT: Possible values are: "string", "numeric", "boolean", "key", and "all".
  2205. -- ts_headline for json
  2206. select ts_headline('{"a": "aaa bbb", "b": {"c": "ccc ddd fff", "c1": "ccc1 ddd1"}, "d": ["ggg hhh", "iii jjj"]}'::json, tsquery('bbb & ddd & hhh'));
  2207. ts_headline
  2208. ---------------------------------------------------------------------------------------------------------
  2209. {"a":"aaa <b>bbb</b>","b":{"c":"ccc <b>ddd</b> fff","c1":"ccc1 ddd1"},"d":["ggg <b>hhh</b>","iii jjj"]}
  2210. (1 row)
  2211. select ts_headline('english', '{"a": "aaa bbb", "b": {"c": "ccc ddd fff"}, "d": ["ggg hhh", "iii jjj"]}'::json, tsquery('bbb & ddd & hhh'));
  2212. ts_headline
  2213. ----------------------------------------------------------------------------------------
  2214. {"a":"aaa <b>bbb</b>","b":{"c":"ccc <b>ddd</b> fff"},"d":["ggg <b>hhh</b>","iii jjj"]}
  2215. (1 row)
  2216. 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 = >');
  2217. ts_headline
  2218. ------------------------------------------------------------------------------------------
  2219. {"a":"aaa <bbb>","b":{"c":"ccc <ddd> fff","c1":"ccc1 ddd1"},"d":["ggg <hhh>","iii jjj"]}
  2220. (1 row)
  2221. 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 = >');
  2222. ts_headline
  2223. ------------------------------------------------------------------------------------------
  2224. {"a":"aaa <bbb>","b":{"c":"ccc <ddd> fff","c1":"ccc1 ddd1"},"d":["ggg <hhh>","iii jjj"]}
  2225. (1 row)
  2226. -- corner cases for ts_headline with json
  2227. select ts_headline('null'::json, tsquery('aaa & bbb'));
  2228. ts_headline
  2229. -------------
  2230. null
  2231. (1 row)
  2232. select ts_headline('{}'::json, tsquery('aaa & bbb'));
  2233. ts_headline
  2234. -------------
  2235. {}
  2236. (1 row)
  2237. select ts_headline('[]'::json, tsquery('aaa & bbb'));
  2238. ts_headline
  2239. -------------
  2240. []
  2241. (1 row)