jsonb.out 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987
  1. -- Strings.
  2. SELECT '""'::jsonb; -- OK.
  3. jsonb
  4. -------
  5. ""
  6. (1 row)
  7. SELECT $$''$$::jsonb; -- ERROR, single quotes are not allowed
  8. ERROR: invalid input syntax for type json
  9. LINE 1: SELECT $$''$$::jsonb;
  10. ^
  11. DETAIL: Token "'" is invalid.
  12. CONTEXT: JSON data, line 1: '...
  13. SELECT '"abc"'::jsonb; -- OK
  14. jsonb
  15. -------
  16. "abc"
  17. (1 row)
  18. SELECT '"abc'::jsonb; -- ERROR, quotes not closed
  19. ERROR: invalid input syntax for type json
  20. LINE 1: SELECT '"abc'::jsonb;
  21. ^
  22. DETAIL: Token ""abc" is invalid.
  23. CONTEXT: JSON data, line 1: "abc
  24. SELECT '"abc
  25. def"'::jsonb; -- 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\"\\"'::jsonb; -- OK, legal escapes
  32. jsonb
  33. ----------
  34. "\n\"\\"
  35. (1 row)
  36. SELECT '"\v"'::jsonb; -- ERROR, not a valid JSON escape
  37. ERROR: invalid input syntax for type json
  38. LINE 1: SELECT '"\v"'::jsonb;
  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'::jsonb; -- OK
  45. jsonb
  46. -------
  47. 1
  48. (1 row)
  49. SELECT '0'::jsonb; -- OK
  50. jsonb
  51. -------
  52. 0
  53. (1 row)
  54. SELECT '01'::jsonb; -- ERROR, not valid according to JSON spec
  55. ERROR: invalid input syntax for type json
  56. LINE 1: SELECT '01'::jsonb;
  57. ^
  58. DETAIL: Token "01" is invalid.
  59. CONTEXT: JSON data, line 1: 01
  60. SELECT '0.1'::jsonb; -- OK
  61. jsonb
  62. -------
  63. 0.1
  64. (1 row)
  65. SELECT '9223372036854775808'::jsonb; -- OK, even though it's too large for int8
  66. jsonb
  67. ---------------------
  68. 9223372036854775808
  69. (1 row)
  70. SELECT '1f2'::jsonb; -- ERROR
  71. ERROR: invalid input syntax for type json
  72. LINE 1: SELECT '1f2'::jsonb;
  73. ^
  74. DETAIL: Token "1f2" is invalid.
  75. CONTEXT: JSON data, line 1: 1f2
  76. SELECT '0.x1'::jsonb; -- ERROR
  77. ERROR: invalid input syntax for type json
  78. LINE 1: SELECT '0.x1'::jsonb;
  79. ^
  80. DETAIL: Token "0.x1" is invalid.
  81. CONTEXT: JSON data, line 1: 0.x1
  82. SELECT '1.3ex100'::jsonb; -- ERROR
  83. ERROR: invalid input syntax for type json
  84. LINE 1: SELECT '1.3ex100'::jsonb;
  85. ^
  86. DETAIL: Token "1.3ex100" is invalid.
  87. CONTEXT: JSON data, line 1: 1.3ex100
  88. -- Arrays.
  89. SELECT '[]'::jsonb; -- OK
  90. jsonb
  91. -------
  92. []
  93. (1 row)
  94. SELECT '[1,2]'::jsonb; -- OK
  95. jsonb
  96. --------
  97. [1, 2]
  98. (1 row)
  99. SELECT '[1,2,]'::jsonb; -- ERROR, trailing comma
  100. ERROR: invalid input syntax for type json
  101. LINE 1: SELECT '[1,2,]'::jsonb;
  102. ^
  103. DETAIL: Expected JSON value, but found "]".
  104. CONTEXT: JSON data, line 1: [1,2,]
  105. SELECT '[1,2'::jsonb; -- ERROR, no closing bracket
  106. ERROR: invalid input syntax for type json
  107. LINE 1: SELECT '[1,2'::jsonb;
  108. ^
  109. DETAIL: The input string ended unexpectedly.
  110. CONTEXT: JSON data, line 1: [1,2
  111. SELECT '[1,[2]'::jsonb; -- ERROR, no closing bracket
  112. ERROR: invalid input syntax for type json
  113. LINE 1: SELECT '[1,[2]'::jsonb;
  114. ^
  115. DETAIL: The input string ended unexpectedly.
  116. CONTEXT: JSON data, line 1: [1,[2]
  117. -- Objects.
  118. SELECT '{}'::jsonb; -- OK
  119. jsonb
  120. -------
  121. {}
  122. (1 row)
  123. SELECT '{"abc"}'::jsonb; -- ERROR, no value
  124. ERROR: invalid input syntax for type json
  125. LINE 1: SELECT '{"abc"}'::jsonb;
  126. ^
  127. DETAIL: Expected ":", but found "}".
  128. CONTEXT: JSON data, line 1: {"abc"}
  129. SELECT '{"abc":1}'::jsonb; -- OK
  130. jsonb
  131. ------------
  132. {"abc": 1}
  133. (1 row)
  134. SELECT '{1:"abc"}'::jsonb; -- ERROR, keys must be strings
  135. ERROR: invalid input syntax for type json
  136. LINE 1: SELECT '{1:"abc"}'::jsonb;
  137. ^
  138. DETAIL: Expected string or "}", but found "1".
  139. CONTEXT: JSON data, line 1: {1...
  140. SELECT '{"abc",1}'::jsonb; -- ERROR, wrong separator
  141. ERROR: invalid input syntax for type json
  142. LINE 1: SELECT '{"abc",1}'::jsonb;
  143. ^
  144. DETAIL: Expected ":", but found ",".
  145. CONTEXT: JSON data, line 1: {"abc",...
  146. SELECT '{"abc"=1}'::jsonb; -- ERROR, totally wrong separator
  147. ERROR: invalid input syntax for type json
  148. LINE 1: SELECT '{"abc"=1}'::jsonb;
  149. ^
  150. DETAIL: Token "=" is invalid.
  151. CONTEXT: JSON data, line 1: {"abc"=...
  152. SELECT '{"abc"::1}'::jsonb; -- ERROR, another wrong separator
  153. ERROR: invalid input syntax for type json
  154. LINE 1: SELECT '{"abc"::1}'::jsonb;
  155. ^
  156. DETAIL: Expected JSON value, but found ":".
  157. CONTEXT: JSON data, line 1: {"abc"::...
  158. SELECT '{"abc":1,"def":2,"ghi":[3,4],"hij":{"klm":5,"nop":[6]}}'::jsonb; -- OK
  159. jsonb
  160. --------------------------------------------------------------------
  161. {"abc": 1, "def": 2, "ghi": [3, 4], "hij": {"klm": 5, "nop": [6]}}
  162. (1 row)
  163. SELECT '{"abc":1:2}'::jsonb; -- ERROR, colon in wrong spot
  164. ERROR: invalid input syntax for type json
  165. LINE 1: SELECT '{"abc":1:2}'::jsonb;
  166. ^
  167. DETAIL: Expected "," or "}", but found ":".
  168. CONTEXT: JSON data, line 1: {"abc":1:...
  169. SELECT '{"abc":1,3}'::jsonb; -- ERROR, no value
  170. ERROR: invalid input syntax for type json
  171. LINE 1: SELECT '{"abc":1,3}'::jsonb;
  172. ^
  173. DETAIL: Expected string, but found "3".
  174. CONTEXT: JSON data, line 1: {"abc":1,3...
  175. -- Miscellaneous stuff.
  176. SELECT 'true'::jsonb; -- OK
  177. jsonb
  178. -------
  179. true
  180. (1 row)
  181. SELECT 'false'::jsonb; -- OK
  182. jsonb
  183. -------
  184. false
  185. (1 row)
  186. SELECT 'null'::jsonb; -- OK
  187. jsonb
  188. -------
  189. null
  190. (1 row)
  191. SELECT ' true '::jsonb; -- OK, even with extra whitespace
  192. jsonb
  193. -------
  194. true
  195. (1 row)
  196. SELECT 'true false'::jsonb; -- ERROR, too many values
  197. ERROR: invalid input syntax for type json
  198. LINE 1: SELECT 'true false'::jsonb;
  199. ^
  200. DETAIL: Expected end of input, but found "false".
  201. CONTEXT: JSON data, line 1: true false
  202. SELECT 'true, false'::jsonb; -- ERROR, too many values
  203. ERROR: invalid input syntax for type json
  204. LINE 1: SELECT 'true, false'::jsonb;
  205. ^
  206. DETAIL: Expected end of input, but found ",".
  207. CONTEXT: JSON data, line 1: true,...
  208. SELECT 'truf'::jsonb; -- ERROR, not a keyword
  209. ERROR: invalid input syntax for type json
  210. LINE 1: SELECT 'truf'::jsonb;
  211. ^
  212. DETAIL: Token "truf" is invalid.
  213. CONTEXT: JSON data, line 1: truf
  214. SELECT 'trues'::jsonb; -- ERROR, not a keyword
  215. ERROR: invalid input syntax for type json
  216. LINE 1: SELECT 'trues'::jsonb;
  217. ^
  218. DETAIL: Token "trues" is invalid.
  219. CONTEXT: JSON data, line 1: trues
  220. SELECT ''::jsonb; -- ERROR, no value
  221. ERROR: invalid input syntax for type json
  222. LINE 1: SELECT ''::jsonb;
  223. ^
  224. DETAIL: The input string ended unexpectedly.
  225. CONTEXT: JSON data, line 1:
  226. SELECT ' '::jsonb; -- ERROR, no value
  227. ERROR: invalid input syntax for type json
  228. LINE 1: SELECT ' '::jsonb;
  229. ^
  230. DETAIL: The input string ended unexpectedly.
  231. CONTEXT: JSON data, line 1:
  232. -- Multi-line JSON input to check ERROR reporting
  233. SELECT '{
  234. "one": 1,
  235. "two":"two",
  236. "three":
  237. true}'::jsonb; -- OK
  238. jsonb
  239. -----------------------------------------
  240. {"one": 1, "two": "two", "three": true}
  241. (1 row)
  242. SELECT '{
  243. "one": 1,
  244. "two":,"two", -- ERROR extraneous comma before field "two"
  245. "three":
  246. true}'::jsonb;
  247. ERROR: invalid input syntax for type json
  248. LINE 1: SELECT '{
  249. ^
  250. DETAIL: Expected JSON value, but found ",".
  251. CONTEXT: JSON data, line 3: "two":,...
  252. SELECT '{
  253. "one": 1,
  254. "two":"two",
  255. "averyveryveryveryveryveryveryveryveryverylongfieldname":}'::jsonb;
  256. ERROR: invalid input syntax for type json
  257. LINE 1: SELECT '{
  258. ^
  259. DETAIL: Expected JSON value, but found "}".
  260. CONTEXT: JSON data, line 4: ...yveryveryveryveryveryveryveryverylongfieldname":}
  261. -- ERROR missing value for last field
  262. -- make sure jsonb is passed through json generators without being escaped
  263. SELECT array_to_json(ARRAY [jsonb '{"a":1}', jsonb '{"b":[2,3]}']);
  264. array_to_json
  265. --------------------------
  266. [{"a": 1},{"b": [2, 3]}]
  267. (1 row)
  268. BEGIN;
  269. SET LOCAL TIME ZONE 10.5;
  270. SET LOCAL TIME ZONE -8;
  271. COMMIT;
  272. -- jsonb extraction functions
  273. CREATE TEMP TABLE test_jsonb (
  274. json_type text,
  275. test_json jsonb
  276. );
  277. INSERT INTO test_jsonb VALUES
  278. ('scalar','"a scalar"'),
  279. ('array','["zero", "one","two",null,"four","five", [1,2,3],{"f1":9}]'),
  280. ('object','{"field1":"val1","field2":"val2","field3":null, "field4": 4, "field5": [1,2,3], "field6": {"f1":9}}');
  281. SELECT test_json -> 'x' FROM test_jsonb WHERE json_type = 'scalar';
  282. ?column?
  283. ----------
  284. (1 row)
  285. SELECT test_json -> 'x' FROM test_jsonb WHERE json_type = 'array';
  286. ?column?
  287. ----------
  288. (1 row)
  289. SELECT test_json -> 'x' FROM test_jsonb WHERE json_type = 'object';
  290. ?column?
  291. ----------
  292. (1 row)
  293. SELECT test_json -> 'field2' FROM test_jsonb WHERE json_type = 'object';
  294. ?column?
  295. ----------
  296. "val2"
  297. (1 row)
  298. SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'scalar';
  299. ?column?
  300. ----------
  301. (1 row)
  302. SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'array';
  303. ?column?
  304. ----------
  305. (1 row)
  306. SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'object';
  307. ?column?
  308. ----------
  309. val2
  310. (1 row)
  311. SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'scalar';
  312. ?column?
  313. ----------
  314. (1 row)
  315. SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'array';
  316. ?column?
  317. ----------
  318. "two"
  319. (1 row)
  320. SELECT test_json -> 9 FROM test_jsonb WHERE json_type = 'array';
  321. ?column?
  322. ----------
  323. (1 row)
  324. SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'object';
  325. ?column?
  326. ----------
  327. (1 row)
  328. SELECT test_json ->> 6 FROM test_jsonb WHERE json_type = 'array';
  329. ?column?
  330. -----------
  331. [1, 2, 3]
  332. (1 row)
  333. SELECT test_json ->> 7 FROM test_jsonb WHERE json_type = 'array';
  334. ?column?
  335. -----------
  336. {"f1": 9}
  337. (1 row)
  338. SELECT test_json ->> 'field4' FROM test_jsonb WHERE json_type = 'object';
  339. ?column?
  340. ----------
  341. 4
  342. (1 row)
  343. SELECT test_json ->> 'field5' FROM test_jsonb WHERE json_type = 'object';
  344. ?column?
  345. -----------
  346. [1, 2, 3]
  347. (1 row)
  348. SELECT test_json ->> 'field6' FROM test_jsonb WHERE json_type = 'object';
  349. ?column?
  350. -----------
  351. {"f1": 9}
  352. (1 row)
  353. SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'scalar';
  354. ?column?
  355. ----------
  356. (1 row)
  357. SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'array';
  358. ?column?
  359. ----------
  360. two
  361. (1 row)
  362. SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'object';
  363. ?column?
  364. ----------
  365. (1 row)
  366. -- nulls
  367. SELECT (test_json->'field3') IS NULL AS expect_false FROM test_jsonb WHERE json_type = 'object';
  368. expect_false
  369. --------------
  370. f
  371. (1 row)
  372. SELECT (test_json->>'field3') IS NULL AS expect_true FROM test_jsonb WHERE json_type = 'object';
  373. expect_true
  374. -------------
  375. t
  376. (1 row)
  377. SELECT (test_json->3) IS NULL AS expect_false FROM test_jsonb WHERE json_type = 'array';
  378. expect_false
  379. --------------
  380. f
  381. (1 row)
  382. SELECT (test_json->>3) IS NULL AS expect_true FROM test_jsonb WHERE json_type = 'array';
  383. expect_true
  384. -------------
  385. t
  386. (1 row)
  387. -- corner cases
  388. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> null::text;
  389. ?column?
  390. ----------
  391. (1 row)
  392. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> null::int;
  393. ?column?
  394. ----------
  395. (1 row)
  396. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> 1;
  397. ?column?
  398. ----------
  399. (1 row)
  400. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> 'z';
  401. ?column?
  402. ----------
  403. (1 row)
  404. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> '';
  405. ?column?
  406. ----------
  407. (1 row)
  408. select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 1;
  409. ?column?
  410. -------------
  411. {"b": "cc"}
  412. (1 row)
  413. select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 3;
  414. ?column?
  415. ----------
  416. (1 row)
  417. select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 'z';
  418. ?column?
  419. ----------
  420. (1 row)
  421. select '{"a": "c", "b": null}'::jsonb -> 'b';
  422. ?column?
  423. ----------
  424. null
  425. (1 row)
  426. select '"foo"'::jsonb -> 1;
  427. ?column?
  428. ----------
  429. (1 row)
  430. select '"foo"'::jsonb -> 'z';
  431. ?column?
  432. ----------
  433. (1 row)
  434. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::text;
  435. ?column?
  436. ----------
  437. (1 row)
  438. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::int;
  439. ?column?
  440. ----------
  441. (1 row)
  442. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> 1;
  443. ?column?
  444. ----------
  445. (1 row)
  446. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> 'z';
  447. ?column?
  448. ----------
  449. (1 row)
  450. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> '';
  451. ?column?
  452. ----------
  453. (1 row)
  454. select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 1;
  455. ?column?
  456. -------------
  457. {"b": "cc"}
  458. (1 row)
  459. select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 3;
  460. ?column?
  461. ----------
  462. (1 row)
  463. select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 'z';
  464. ?column?
  465. ----------
  466. (1 row)
  467. select '{"a": "c", "b": null}'::jsonb ->> 'b';
  468. ?column?
  469. ----------
  470. (1 row)
  471. select '"foo"'::jsonb ->> 1;
  472. ?column?
  473. ----------
  474. (1 row)
  475. select '"foo"'::jsonb ->> 'z';
  476. ?column?
  477. ----------
  478. (1 row)
  479. -- equality and inequality
  480. SELECT '{"x":"y"}'::jsonb = '{"x":"y"}'::jsonb;
  481. ?column?
  482. ----------
  483. t
  484. (1 row)
  485. SELECT '{"x":"y"}'::jsonb = '{"x":"z"}'::jsonb;
  486. ?column?
  487. ----------
  488. f
  489. (1 row)
  490. SELECT '{"x":"y"}'::jsonb <> '{"x":"y"}'::jsonb;
  491. ?column?
  492. ----------
  493. f
  494. (1 row)
  495. SELECT '{"x":"y"}'::jsonb <> '{"x":"z"}'::jsonb;
  496. ?column?
  497. ----------
  498. t
  499. (1 row)
  500. -- containment
  501. SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b"}');
  502. jsonb_contains
  503. ----------------
  504. t
  505. (1 row)
  506. SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b", "c":null}');
  507. jsonb_contains
  508. ----------------
  509. t
  510. (1 row)
  511. SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b", "g":null}');
  512. jsonb_contains
  513. ----------------
  514. f
  515. (1 row)
  516. SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"g":null}');
  517. jsonb_contains
  518. ----------------
  519. f
  520. (1 row)
  521. SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"c"}');
  522. jsonb_contains
  523. ----------------
  524. f
  525. (1 row)
  526. SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b"}');
  527. jsonb_contains
  528. ----------------
  529. t
  530. (1 row)
  531. SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b", "c":"q"}');
  532. jsonb_contains
  533. ----------------
  534. f
  535. (1 row)
  536. SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b"}';
  537. ?column?
  538. ----------
  539. t
  540. (1 row)
  541. SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b", "c":null}';
  542. ?column?
  543. ----------
  544. t
  545. (1 row)
  546. SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b", "g":null}';
  547. ?column?
  548. ----------
  549. f
  550. (1 row)
  551. SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"g":null}';
  552. ?column?
  553. ----------
  554. f
  555. (1 row)
  556. SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"c"}';
  557. ?column?
  558. ----------
  559. f
  560. (1 row)
  561. SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b"}';
  562. ?column?
  563. ----------
  564. t
  565. (1 row)
  566. SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b", "c":"q"}';
  567. ?column?
  568. ----------
  569. f
  570. (1 row)
  571. SELECT '[1,2]'::jsonb @> '[1,2,2]'::jsonb;
  572. ?column?
  573. ----------
  574. t
  575. (1 row)
  576. SELECT '[1,1,2]'::jsonb @> '[1,2,2]'::jsonb;
  577. ?column?
  578. ----------
  579. t
  580. (1 row)
  581. SELECT '[[1,2]]'::jsonb @> '[[1,2,2]]'::jsonb;
  582. ?column?
  583. ----------
  584. t
  585. (1 row)
  586. SELECT '[1,2,2]'::jsonb <@ '[1,2]'::jsonb;
  587. ?column?
  588. ----------
  589. t
  590. (1 row)
  591. SELECT '[1,2,2]'::jsonb <@ '[1,1,2]'::jsonb;
  592. ?column?
  593. ----------
  594. t
  595. (1 row)
  596. SELECT '[[1,2,2]]'::jsonb <@ '[[1,2]]'::jsonb;
  597. ?column?
  598. ----------
  599. t
  600. (1 row)
  601. SELECT jsonb_contained('{"a":"b"}', '{"a":"b", "b":1, "c":null}');
  602. jsonb_contained
  603. -----------------
  604. t
  605. (1 row)
  606. SELECT jsonb_contained('{"a":"b", "c":null}', '{"a":"b", "b":1, "c":null}');
  607. jsonb_contained
  608. -----------------
  609. t
  610. (1 row)
  611. SELECT jsonb_contained('{"a":"b", "g":null}', '{"a":"b", "b":1, "c":null}');
  612. jsonb_contained
  613. -----------------
  614. f
  615. (1 row)
  616. SELECT jsonb_contained('{"g":null}', '{"a":"b", "b":1, "c":null}');
  617. jsonb_contained
  618. -----------------
  619. f
  620. (1 row)
  621. SELECT jsonb_contained('{"a":"c"}', '{"a":"b", "b":1, "c":null}');
  622. jsonb_contained
  623. -----------------
  624. f
  625. (1 row)
  626. SELECT jsonb_contained('{"a":"b"}', '{"a":"b", "b":1, "c":null}');
  627. jsonb_contained
  628. -----------------
  629. t
  630. (1 row)
  631. SELECT jsonb_contained('{"a":"b", "c":"q"}', '{"a":"b", "b":1, "c":null}');
  632. jsonb_contained
  633. -----------------
  634. f
  635. (1 row)
  636. SELECT '{"a":"b"}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
  637. ?column?
  638. ----------
  639. t
  640. (1 row)
  641. SELECT '{"a":"b", "c":null}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
  642. ?column?
  643. ----------
  644. t
  645. (1 row)
  646. SELECT '{"a":"b", "g":null}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
  647. ?column?
  648. ----------
  649. f
  650. (1 row)
  651. SELECT '{"g":null}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
  652. ?column?
  653. ----------
  654. f
  655. (1 row)
  656. SELECT '{"a":"c"}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
  657. ?column?
  658. ----------
  659. f
  660. (1 row)
  661. SELECT '{"a":"b"}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
  662. ?column?
  663. ----------
  664. t
  665. (1 row)
  666. SELECT '{"a":"b", "c":"q"}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
  667. ?column?
  668. ----------
  669. f
  670. (1 row)
  671. -- Raw scalar may contain another raw scalar, array may contain a raw scalar
  672. SELECT '[5]'::jsonb @> '[5]';
  673. ?column?
  674. ----------
  675. t
  676. (1 row)
  677. SELECT '5'::jsonb @> '5';
  678. ?column?
  679. ----------
  680. t
  681. (1 row)
  682. SELECT '[5]'::jsonb @> '5';
  683. ?column?
  684. ----------
  685. t
  686. (1 row)
  687. -- But a raw scalar cannot contain an array
  688. SELECT '5'::jsonb @> '[5]';
  689. ?column?
  690. ----------
  691. f
  692. (1 row)
  693. -- In general, one thing should always contain itself. Test array containment:
  694. SELECT '["9", ["7", "3"], 1]'::jsonb @> '["9", ["7", "3"], 1]'::jsonb;
  695. ?column?
  696. ----------
  697. t
  698. (1 row)
  699. SELECT '["9", ["7", "3"], ["1"]]'::jsonb @> '["9", ["7", "3"], ["1"]]'::jsonb;
  700. ?column?
  701. ----------
  702. t
  703. (1 row)
  704. -- array containment string matching confusion bug
  705. SELECT '{ "name": "Bob", "tags": [ "enim", "qui"]}'::jsonb @> '{"tags":["qu"]}';
  706. ?column?
  707. ----------
  708. f
  709. (1 row)
  710. -- array length
  711. SELECT jsonb_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]');
  712. jsonb_array_length
  713. --------------------
  714. 5
  715. (1 row)
  716. SELECT jsonb_array_length('[]');
  717. jsonb_array_length
  718. --------------------
  719. 0
  720. (1 row)
  721. SELECT jsonb_array_length('{"f1":1,"f2":[5,6]}');
  722. ERROR: cannot get array length of a non-array
  723. SELECT jsonb_array_length('4');
  724. ERROR: cannot get array length of a scalar
  725. SELECT * FROM jsonb_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
  726. key | value
  727. -----+-----------
  728. f1 | [1, 2, 3]
  729. f2 | {"f3": 1}
  730. f4 | null
  731. f5 | 99
  732. f6 | "stringy"
  733. (5 rows)
  734. SELECT * FROM jsonb_each('{"a":{"b":"c","c":"b","1":"first"},"b":[1,2],"c":"cc","1":"first","n":null}'::jsonb) AS q;
  735. key | value
  736. -----+------------------------------------
  737. 1 | "first"
  738. a | {"1": "first", "b": "c", "c": "b"}
  739. b | [1, 2]
  740. c | "cc"
  741. n | null
  742. (5 rows)
  743. SELECT * FROM jsonb_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
  744. key | value
  745. -----+-----------
  746. f1 | [1, 2, 3]
  747. f2 | {"f3": 1}
  748. f4 |
  749. f5 | 99
  750. f6 | stringy
  751. (5 rows)
  752. SELECT * FROM jsonb_each_text('{"a":{"b":"c","c":"b","1":"first"},"b":[1,2],"c":"cc","1":"first","n":null}'::jsonb) AS q;
  753. key | value
  754. -----+------------------------------------
  755. 1 | first
  756. a | {"1": "first", "b": "c", "c": "b"}
  757. b | [1, 2]
  758. c | cc
  759. n |
  760. (5 rows)
  761. -- exists
  762. SELECT jsonb_exists('{"a":null, "b":"qq"}', 'a');
  763. jsonb_exists
  764. --------------
  765. t
  766. (1 row)
  767. SELECT jsonb_exists('{"a":null, "b":"qq"}', 'b');
  768. jsonb_exists
  769. --------------
  770. t
  771. (1 row)
  772. SELECT jsonb_exists('{"a":null, "b":"qq"}', 'c');
  773. jsonb_exists
  774. --------------
  775. f
  776. (1 row)
  777. SELECT jsonb_exists('{"a":"null", "b":"qq"}', 'a');
  778. jsonb_exists
  779. --------------
  780. t
  781. (1 row)
  782. SELECT jsonb '{"a":null, "b":"qq"}' ? 'a';
  783. ?column?
  784. ----------
  785. t
  786. (1 row)
  787. SELECT jsonb '{"a":null, "b":"qq"}' ? 'b';
  788. ?column?
  789. ----------
  790. t
  791. (1 row)
  792. SELECT jsonb '{"a":null, "b":"qq"}' ? 'c';
  793. ?column?
  794. ----------
  795. f
  796. (1 row)
  797. SELECT jsonb '{"a":"null", "b":"qq"}' ? 'a';
  798. ?column?
  799. ----------
  800. t
  801. (1 row)
  802. -- array exists - array elements should behave as keys
  803. SELECT count(*) from testjsonb WHERE j->'array' ? 'bar';
  804. count
  805. -------
  806. 3
  807. (1 row)
  808. -- type sensitive array exists - should return no rows (since "exists" only
  809. -- matches strings that are either object keys or array elements)
  810. SELECT count(*) from testjsonb WHERE j->'array' ? '5'::text;
  811. count
  812. -------
  813. 0
  814. (1 row)
  815. -- However, a raw scalar is *contained* within the array
  816. SELECT count(*) from testjsonb WHERE j->'array' @> '5'::jsonb;
  817. count
  818. -------
  819. 1
  820. (1 row)
  821. SELECT jsonb_exists_any('{"a":null, "b":"qq"}', ARRAY['a','b']);
  822. jsonb_exists_any
  823. ------------------
  824. t
  825. (1 row)
  826. SELECT jsonb_exists_any('{"a":null, "b":"qq"}', ARRAY['b','a']);
  827. jsonb_exists_any
  828. ------------------
  829. t
  830. (1 row)
  831. SELECT jsonb_exists_any('{"a":null, "b":"qq"}', ARRAY['c','a']);
  832. jsonb_exists_any
  833. ------------------
  834. t
  835. (1 row)
  836. SELECT jsonb_exists_any('{"a":null, "b":"qq"}', ARRAY['c','d']);
  837. jsonb_exists_any
  838. ------------------
  839. f
  840. (1 row)
  841. SELECT jsonb_exists_any('{"a":null, "b":"qq"}', '{}'::text[]);
  842. jsonb_exists_any
  843. ------------------
  844. f
  845. (1 row)
  846. SELECT jsonb '{"a":null, "b":"qq"}' ?| ARRAY['a','b'];
  847. ?column?
  848. ----------
  849. t
  850. (1 row)
  851. SELECT jsonb '{"a":null, "b":"qq"}' ?| ARRAY['b','a'];
  852. ?column?
  853. ----------
  854. t
  855. (1 row)
  856. SELECT jsonb '{"a":null, "b":"qq"}' ?| ARRAY['c','a'];
  857. ?column?
  858. ----------
  859. t
  860. (1 row)
  861. SELECT jsonb '{"a":null, "b":"qq"}' ?| ARRAY['c','d'];
  862. ?column?
  863. ----------
  864. f
  865. (1 row)
  866. SELECT jsonb '{"a":null, "b":"qq"}' ?| '{}'::text[];
  867. ?column?
  868. ----------
  869. f
  870. (1 row)
  871. SELECT jsonb_exists_all('{"a":null, "b":"qq"}', ARRAY['a','b']);
  872. jsonb_exists_all
  873. ------------------
  874. t
  875. (1 row)
  876. SELECT jsonb_exists_all('{"a":null, "b":"qq"}', ARRAY['b','a']);
  877. jsonb_exists_all
  878. ------------------
  879. t
  880. (1 row)
  881. SELECT jsonb_exists_all('{"a":null, "b":"qq"}', ARRAY['c','a']);
  882. jsonb_exists_all
  883. ------------------
  884. f
  885. (1 row)
  886. SELECT jsonb_exists_all('{"a":null, "b":"qq"}', ARRAY['c','d']);
  887. jsonb_exists_all
  888. ------------------
  889. f
  890. (1 row)
  891. SELECT jsonb_exists_all('{"a":null, "b":"qq"}', '{}'::text[]);
  892. jsonb_exists_all
  893. ------------------
  894. t
  895. (1 row)
  896. SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['a','b'];
  897. ?column?
  898. ----------
  899. t
  900. (1 row)
  901. SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['b','a'];
  902. ?column?
  903. ----------
  904. t
  905. (1 row)
  906. SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['c','a'];
  907. ?column?
  908. ----------
  909. f
  910. (1 row)
  911. SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['c','d'];
  912. ?column?
  913. ----------
  914. f
  915. (1 row)
  916. SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['a','a', 'b', 'b', 'b'];
  917. ?column?
  918. ----------
  919. t
  920. (1 row)
  921. SELECT jsonb '{"a":null, "b":"qq"}' ?& '{}'::text[];
  922. ?column?
  923. ----------
  924. t
  925. (1 row)
  926. -- typeof
  927. SELECT jsonb_typeof('{}') AS object;
  928. object
  929. --------
  930. object
  931. (1 row)
  932. SELECT jsonb_typeof('{"c":3,"p":"o"}') AS object;
  933. object
  934. --------
  935. object
  936. (1 row)
  937. SELECT jsonb_typeof('[]') AS array;
  938. array
  939. -------
  940. array
  941. (1 row)
  942. SELECT jsonb_typeof('["a", 1]') AS array;
  943. array
  944. -------
  945. array
  946. (1 row)
  947. SELECT jsonb_typeof('null') AS "null";
  948. null
  949. ------
  950. null
  951. (1 row)
  952. SELECT jsonb_typeof('1') AS number;
  953. number
  954. --------
  955. number
  956. (1 row)
  957. SELECT jsonb_typeof('-1') AS number;
  958. number
  959. --------
  960. number
  961. (1 row)
  962. SELECT jsonb_typeof('1.0') AS number;
  963. number
  964. --------
  965. number
  966. (1 row)
  967. SELECT jsonb_typeof('1e2') AS number;
  968. number
  969. --------
  970. number
  971. (1 row)
  972. SELECT jsonb_typeof('-1.0') AS number;
  973. number
  974. --------
  975. number
  976. (1 row)
  977. SELECT jsonb_typeof('true') AS boolean;
  978. boolean
  979. ---------
  980. boolean
  981. (1 row)
  982. SELECT jsonb_typeof('false') AS boolean;
  983. boolean
  984. ---------
  985. boolean
  986. (1 row)
  987. SELECT jsonb_typeof('"hello"') AS string;
  988. string
  989. --------
  990. string
  991. (1 row)
  992. SELECT jsonb_typeof('"true"') AS string;
  993. string
  994. --------
  995. string
  996. (1 row)
  997. SELECT jsonb_typeof('"1.0"') AS string;
  998. string
  999. --------
  1000. string
  1001. (1 row)
  1002. SELECT jsonb_build_object('{a,b,c}'::text[]); -- error
  1003. ERROR: argument list must have even number of elements
  1004. HINT: The arguments of jsonb_build_object() must consist of alternating keys and values.
  1005. SELECT jsonb_build_object('{a,b,c}'::text[], '{d,e,f}'::text[]); -- error, key cannot be array
  1006. ERROR: key value must be scalar, not array, composite, or json
  1007. -- empty objects/arrays
  1008. SELECT jsonb_build_array();
  1009. jsonb_build_array
  1010. -------------------
  1011. []
  1012. (1 row)
  1013. SELECT jsonb_build_object();
  1014. jsonb_build_object
  1015. --------------------
  1016. {}
  1017. (1 row)
  1018. -- make sure keys are quoted
  1019. SELECT jsonb_build_object(1,2);
  1020. jsonb_build_object
  1021. --------------------
  1022. {"1": 2}
  1023. (1 row)
  1024. SELECT jsonb_build_object(r,2) FROM (SELECT 1 AS a, 2 AS b) r;
  1025. ERROR: key value must be scalar, not array, composite, or json
  1026. SELECT jsonb_build_object(json '{"a":1,"b":2}', 3);
  1027. ERROR: key value must be scalar, not array, composite, or json
  1028. SELECT jsonb_build_object('{1,2,3}'::int[], 3);
  1029. ERROR: key value must be scalar, not array, composite, or json
  1030. -- handling of NULL values
  1031. SELECT jsonb_object_agg(1, NULL::jsonb);
  1032. jsonb_object_agg
  1033. ------------------
  1034. {"1": null}
  1035. (1 row)
  1036. SELECT jsonb_object_agg(NULL, '{"a":1}');
  1037. ERROR: field name must not be null
  1038. CREATE TEMP TABLE foo (serial_num int, name text, type text);
  1039. INSERT INTO foo VALUES (847001,'t15','GE1043');
  1040. INSERT INTO foo VALUES (847002,'t16','GE1043');
  1041. INSERT INTO foo VALUES (847003,'sub-alpha','GESS90');
  1042. SELECT jsonb_object_agg(name, type) FROM foo;
  1043. jsonb_object_agg
  1044. -----------------------------------------------------------
  1045. {"t15": "GE1043", "t16": "GE1043", "sub-alpha": "GESS90"}
  1046. (1 row)
  1047. INSERT INTO foo VALUES (999999, NULL, 'bar');
  1048. SELECT jsonb_object_agg(name, type) FROM foo;
  1049. ERROR: field name must not be null
  1050. -- jsonb_object
  1051. -- empty object, one dimension
  1052. SELECT jsonb_object('{}');
  1053. jsonb_object
  1054. --------------
  1055. {}
  1056. (1 row)
  1057. -- empty object, two dimensions
  1058. SELECT jsonb_object('{}', '{}');
  1059. jsonb_object
  1060. --------------
  1061. {}
  1062. (1 row)
  1063. -- one dimension
  1064. SELECT jsonb_object('{a,1,b,2,3,NULL,"d e f","a b c"}');
  1065. jsonb_object
  1066. ---------------------------------------------------
  1067. {"3": null, "a": "1", "b": "2", "d e f": "a b c"}
  1068. (1 row)
  1069. -- same but with two dimensions
  1070. SELECT jsonb_object('{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}');
  1071. jsonb_object
  1072. ---------------------------------------------------
  1073. {"3": null, "a": "1", "b": "2", "d e f": "a b c"}
  1074. (1 row)
  1075. -- odd number error
  1076. SELECT jsonb_object('{a,b,c}');
  1077. ERROR: array must have even number of elements
  1078. -- one column error
  1079. SELECT jsonb_object('{{a},{b}}');
  1080. ERROR: array must have two columns
  1081. -- too many columns error
  1082. SELECT jsonb_object('{{a,b,c},{b,c,d}}');
  1083. ERROR: array must have two columns
  1084. -- too many dimensions error
  1085. SELECT jsonb_object('{{{a,b},{c,d}},{{b,c},{d,e}}}');
  1086. ERROR: wrong number of array subscripts
  1087. --two argument form of jsonb_object
  1088. select jsonb_object('{a,b,c,"d e f"}','{1,2,3,"a b c"}');
  1089. jsonb_object
  1090. --------------------------------------------------
  1091. {"a": "1", "b": "2", "c": "3", "d e f": "a b c"}
  1092. (1 row)
  1093. -- too many dimensions
  1094. SELECT jsonb_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"}}');
  1095. ERROR: wrong number of array subscripts
  1096. -- mismatched dimensions
  1097. select jsonb_object('{a,b,c,"d e f",g}','{1,2,3,"a b c"}');
  1098. ERROR: mismatched array dimensions
  1099. select jsonb_object('{a,b,c,"d e f"}','{1,2,3,"a b c",g}');
  1100. ERROR: mismatched array dimensions
  1101. -- null key error
  1102. select jsonb_object('{a,b,NULL,"d e f"}','{1,2,3,"a b c"}');
  1103. ERROR: null value not allowed for object key
  1104. -- empty key is allowed
  1105. select jsonb_object('{a,b,"","d e f"}','{1,2,3,"a b c"}');
  1106. jsonb_object
  1107. -------------------------------------------------
  1108. {"": "3", "a": "1", "b": "2", "d e f": "a b c"}
  1109. (1 row)
  1110. -- extract_path, extract_path_as_text
  1111. SELECT jsonb_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
  1112. jsonb_extract_path
  1113. --------------------
  1114. "stringy"
  1115. (1 row)
  1116. SELECT jsonb_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
  1117. jsonb_extract_path
  1118. --------------------
  1119. {"f3": 1}
  1120. (1 row)
  1121. SELECT jsonb_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
  1122. jsonb_extract_path
  1123. --------------------
  1124. "f3"
  1125. (1 row)
  1126. SELECT jsonb_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
  1127. jsonb_extract_path
  1128. --------------------
  1129. 1
  1130. (1 row)
  1131. SELECT jsonb_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
  1132. jsonb_extract_path_text
  1133. -------------------------
  1134. stringy
  1135. (1 row)
  1136. SELECT jsonb_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
  1137. jsonb_extract_path_text
  1138. -------------------------
  1139. {"f3": 1}
  1140. (1 row)
  1141. SELECT jsonb_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
  1142. jsonb_extract_path_text
  1143. -------------------------
  1144. f3
  1145. (1 row)
  1146. SELECT jsonb_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
  1147. jsonb_extract_path_text
  1148. -------------------------
  1149. 1
  1150. (1 row)
  1151. -- extract_path nulls
  1152. SELECT jsonb_extract_path('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') IS NULL AS expect_false;
  1153. expect_false
  1154. --------------
  1155. f
  1156. (1 row)
  1157. SELECT jsonb_extract_path_text('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') IS NULL AS expect_true;
  1158. expect_true
  1159. -------------
  1160. t
  1161. (1 row)
  1162. SELECT jsonb_extract_path('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') IS NULL AS expect_false;
  1163. expect_false
  1164. --------------
  1165. f
  1166. (1 row)
  1167. SELECT jsonb_extract_path_text('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') IS NULL AS expect_true;
  1168. expect_true
  1169. -------------
  1170. t
  1171. (1 row)
  1172. SELECT * FROM jsonb_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false]') q;
  1173. value
  1174. ----------------------------
  1175. 1
  1176. true
  1177. [1, [2, 3]]
  1178. null
  1179. {"f1": 1, "f2": [7, 8, 9]}
  1180. false
  1181. (6 rows)
  1182. SELECT * FROM jsonb_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q;
  1183. value
  1184. ----------------------------
  1185. 1
  1186. true
  1187. [1, [2, 3]]
  1188. {"f1": 1, "f2": [7, 8, 9]}
  1189. false
  1190. stringy
  1191. (7 rows)
  1192. -- test type info caching in jsonb_populate_record()
  1193. CREATE TEMP TABLE jsbpoptest (js jsonb);
  1194. INSERT INTO jsbpoptest
  1195. SELECT '{
  1196. "jsa": [1, "2", null, 4],
  1197. "rec": {"a": "abc", "c": "01.02.2003", "x": 43.2},
  1198. "reca": [{"a": "abc", "b": 456}, null, {"c": "01.02.2003", "x": 43.2}]
  1199. }'::jsonb
  1200. FROM generate_series(1, 3);
  1201. -- indexing
  1202. SELECT count(*) FROM testjsonb WHERE j @> '{"wait":null}';
  1203. count
  1204. -------
  1205. 1
  1206. (1 row)
  1207. SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC"}';
  1208. count
  1209. -------
  1210. 15
  1211. (1 row)
  1212. SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC", "public":true}';
  1213. count
  1214. -------
  1215. 2
  1216. (1 row)
  1217. SELECT count(*) FROM testjsonb WHERE j @> '{"age":25}';
  1218. count
  1219. -------
  1220. 2
  1221. (1 row)
  1222. SELECT count(*) FROM testjsonb WHERE j @> '{"age":25.0}';
  1223. count
  1224. -------
  1225. 2
  1226. (1 row)
  1227. SELECT count(*) FROM testjsonb WHERE j ? 'public';
  1228. count
  1229. -------
  1230. 194
  1231. (1 row)
  1232. SELECT count(*) FROM testjsonb WHERE j ? 'bar';
  1233. count
  1234. -------
  1235. 0
  1236. (1 row)
  1237. SELECT count(*) FROM testjsonb WHERE j ?| ARRAY['public','disabled'];
  1238. count
  1239. -------
  1240. 337
  1241. (1 row)
  1242. SELECT count(*) FROM testjsonb WHERE j ?& ARRAY['public','disabled'];
  1243. count
  1244. -------
  1245. 42
  1246. (1 row)
  1247. SELECT count(*) FROM testjsonb WHERE j @@ '$.wait == null';
  1248. count
  1249. -------
  1250. 1
  1251. (1 row)
  1252. SELECT count(*) FROM testjsonb WHERE j @@ '"CC" == $.wait';
  1253. count
  1254. -------
  1255. 15
  1256. (1 row)
  1257. SELECT count(*) FROM testjsonb WHERE j @@ '$.wait == "CC" && true == $.public';
  1258. count
  1259. -------
  1260. 2
  1261. (1 row)
  1262. SELECT count(*) FROM testjsonb WHERE j @@ '$.age == 25';
  1263. count
  1264. -------
  1265. 2
  1266. (1 row)
  1267. SELECT count(*) FROM testjsonb WHERE j @@ '$.age == 25.0';
  1268. count
  1269. -------
  1270. 2
  1271. (1 row)
  1272. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($)';
  1273. count
  1274. -------
  1275. 1012
  1276. (1 row)
  1277. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.public)';
  1278. count
  1279. -------
  1280. 194
  1281. (1 row)
  1282. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.bar)';
  1283. count
  1284. -------
  1285. 0
  1286. (1 row)
  1287. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.public) || exists($.disabled)';
  1288. count
  1289. -------
  1290. 337
  1291. (1 row)
  1292. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.public) && exists($.disabled)';
  1293. count
  1294. -------
  1295. 42
  1296. (1 row)
  1297. SELECT count(*) FROM testjsonb WHERE j @? '$.wait ? (@ == null)';
  1298. count
  1299. -------
  1300. 1
  1301. (1 row)
  1302. SELECT count(*) FROM testjsonb WHERE j @? '$.wait ? ("CC" == @)';
  1303. count
  1304. -------
  1305. 15
  1306. (1 row)
  1307. SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.wait == "CC" && true == @.public)';
  1308. count
  1309. -------
  1310. 2
  1311. (1 row)
  1312. SELECT count(*) FROM testjsonb WHERE j @? '$.age ? (@ == 25)';
  1313. count
  1314. -------
  1315. 2
  1316. (1 row)
  1317. SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.age == 25.0)';
  1318. count
  1319. -------
  1320. 2
  1321. (1 row)
  1322. SELECT count(*) FROM testjsonb WHERE j @? '$';
  1323. count
  1324. -------
  1325. 1012
  1326. (1 row)
  1327. SELECT count(*) FROM testjsonb WHERE j @? '$.public';
  1328. count
  1329. -------
  1330. 194
  1331. (1 row)
  1332. SELECT count(*) FROM testjsonb WHERE j @? '$.bar';
  1333. count
  1334. -------
  1335. 0
  1336. (1 row)
  1337. CREATE INDEX jidx ON testjsonb USING gin (j);
  1338. SELECT count(*) FROM testjsonb WHERE j @> '{"wait":null}';
  1339. count
  1340. -------
  1341. 1
  1342. (1 row)
  1343. SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC"}';
  1344. count
  1345. -------
  1346. 15
  1347. (1 row)
  1348. SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC", "public":true}';
  1349. count
  1350. -------
  1351. 2
  1352. (1 row)
  1353. SELECT count(*) FROM testjsonb WHERE j @> '{"age":25}';
  1354. count
  1355. -------
  1356. 2
  1357. (1 row)
  1358. SELECT count(*) FROM testjsonb WHERE j @> '{"age":25.0}';
  1359. count
  1360. -------
  1361. 2
  1362. (1 row)
  1363. SELECT count(*) FROM testjsonb WHERE j @> '{"array":["foo"]}';
  1364. count
  1365. -------
  1366. 3
  1367. (1 row)
  1368. SELECT count(*) FROM testjsonb WHERE j @> '{"array":["bar"]}';
  1369. count
  1370. -------
  1371. 3
  1372. (1 row)
  1373. -- exercise GIN_SEARCH_MODE_ALL
  1374. SELECT count(*) FROM testjsonb WHERE j @> '{}';
  1375. count
  1376. -------
  1377. 1012
  1378. (1 row)
  1379. SELECT count(*) FROM testjsonb WHERE j ? 'public';
  1380. count
  1381. -------
  1382. 194
  1383. (1 row)
  1384. SELECT count(*) FROM testjsonb WHERE j ? 'bar';
  1385. count
  1386. -------
  1387. 0
  1388. (1 row)
  1389. SELECT count(*) FROM testjsonb WHERE j ?| ARRAY['public','disabled'];
  1390. count
  1391. -------
  1392. 337
  1393. (1 row)
  1394. SELECT count(*) FROM testjsonb WHERE j ?& ARRAY['public','disabled'];
  1395. count
  1396. -------
  1397. 42
  1398. (1 row)
  1399. SELECT count(*) FROM testjsonb WHERE j @@ '$.wait == null';
  1400. count
  1401. -------
  1402. 1
  1403. (1 row)
  1404. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($ ? (@.wait == null))';
  1405. count
  1406. -------
  1407. 1
  1408. (1 row)
  1409. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.wait ? (@ == null))';
  1410. count
  1411. -------
  1412. 1
  1413. (1 row)
  1414. SELECT count(*) FROM testjsonb WHERE j @@ '"CC" == $.wait';
  1415. count
  1416. -------
  1417. 15
  1418. (1 row)
  1419. SELECT count(*) FROM testjsonb WHERE j @@ '$.wait == "CC" && true == $.public';
  1420. count
  1421. -------
  1422. 2
  1423. (1 row)
  1424. SELECT count(*) FROM testjsonb WHERE j @@ '$.age == 25';
  1425. count
  1426. -------
  1427. 2
  1428. (1 row)
  1429. SELECT count(*) FROM testjsonb WHERE j @@ '$.age == 25.0';
  1430. count
  1431. -------
  1432. 2
  1433. (1 row)
  1434. SELECT count(*) FROM testjsonb WHERE j @@ '$.array[*] == "foo"';
  1435. count
  1436. -------
  1437. 3
  1438. (1 row)
  1439. SELECT count(*) FROM testjsonb WHERE j @@ '$.array[*] == "bar"';
  1440. count
  1441. -------
  1442. 3
  1443. (1 row)
  1444. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($ ? (@.array[*] == "bar"))';
  1445. count
  1446. -------
  1447. 3
  1448. (1 row)
  1449. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.array ? (@[*] == "bar"))';
  1450. count
  1451. -------
  1452. 3
  1453. (1 row)
  1454. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.array[*] ? (@ == "bar"))';
  1455. count
  1456. -------
  1457. 3
  1458. (1 row)
  1459. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($)';
  1460. count
  1461. -------
  1462. 1012
  1463. (1 row)
  1464. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.public)';
  1465. count
  1466. -------
  1467. 194
  1468. (1 row)
  1469. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.bar)';
  1470. count
  1471. -------
  1472. 0
  1473. (1 row)
  1474. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.public) || exists($.disabled)';
  1475. count
  1476. -------
  1477. 337
  1478. (1 row)
  1479. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.public) && exists($.disabled)';
  1480. count
  1481. -------
  1482. 42
  1483. (1 row)
  1484. SELECT count(*) FROM testjsonb WHERE j @? '$.wait ? (@ == null)';
  1485. count
  1486. -------
  1487. 1
  1488. (1 row)
  1489. SELECT count(*) FROM testjsonb WHERE j @? '$.wait ? ("CC" == @)';
  1490. count
  1491. -------
  1492. 15
  1493. (1 row)
  1494. SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.wait == "CC" && true == @.public)';
  1495. count
  1496. -------
  1497. 2
  1498. (1 row)
  1499. SELECT count(*) FROM testjsonb WHERE j @? '$.age ? (@ == 25)';
  1500. count
  1501. -------
  1502. 2
  1503. (1 row)
  1504. SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.age == 25.0)';
  1505. count
  1506. -------
  1507. 2
  1508. (1 row)
  1509. SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.array[*] == "bar")';
  1510. count
  1511. -------
  1512. 3
  1513. (1 row)
  1514. SELECT count(*) FROM testjsonb WHERE j @? '$.array ? (@[*] == "bar")';
  1515. count
  1516. -------
  1517. 3
  1518. (1 row)
  1519. SELECT count(*) FROM testjsonb WHERE j @? '$.array[*] ? (@ == "bar")';
  1520. count
  1521. -------
  1522. 3
  1523. (1 row)
  1524. SELECT count(*) FROM testjsonb WHERE j @? '$';
  1525. count
  1526. -------
  1527. 1012
  1528. (1 row)
  1529. SELECT count(*) FROM testjsonb WHERE j @? '$.public';
  1530. count
  1531. -------
  1532. 194
  1533. (1 row)
  1534. SELECT count(*) FROM testjsonb WHERE j @? '$.bar';
  1535. count
  1536. -------
  1537. 0
  1538. (1 row)
  1539. SELECT count(*) from testjsonb WHERE j->'array' ? 'bar';
  1540. count
  1541. -------
  1542. 3
  1543. (1 row)
  1544. -- type sensitive array exists - should return no rows (since "exists" only
  1545. -- matches strings that are either object keys or array elements)
  1546. SELECT count(*) from testjsonb WHERE j->'array' ? '5'::text;
  1547. count
  1548. -------
  1549. 0
  1550. (1 row)
  1551. -- However, a raw scalar is *contained* within the array
  1552. SELECT count(*) from testjsonb WHERE j->'array' @> '5'::jsonb;
  1553. count
  1554. -------
  1555. 1
  1556. (1 row)
  1557. -- btree
  1558. CREATE INDEX jidx ON testjsonb USING btree (j);
  1559. SELECT count(*) FROM testjsonb WHERE j > '{"p":1}';
  1560. count
  1561. -------
  1562. 884
  1563. (1 row)
  1564. SELECT count(*) FROM testjsonb WHERE j = '{"pos":98, "line":371, "node":"CBA", "indexed":true}';
  1565. count
  1566. -------
  1567. 1
  1568. (1 row)
  1569. CREATE INDEX jidx ON testjsonb USING gin (j jsonb_path_ops);
  1570. SELECT count(*) FROM testjsonb WHERE j @> '{"wait":null}';
  1571. count
  1572. -------
  1573. 1
  1574. (1 row)
  1575. SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC"}';
  1576. count
  1577. -------
  1578. 15
  1579. (1 row)
  1580. SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC", "public":true}';
  1581. count
  1582. -------
  1583. 2
  1584. (1 row)
  1585. SELECT count(*) FROM testjsonb WHERE j @> '{"age":25}';
  1586. count
  1587. -------
  1588. 2
  1589. (1 row)
  1590. SELECT count(*) FROM testjsonb WHERE j @> '{"age":25.0}';
  1591. count
  1592. -------
  1593. 2
  1594. (1 row)
  1595. -- exercise GIN_SEARCH_MODE_ALL
  1596. SELECT count(*) FROM testjsonb WHERE j @> '{}';
  1597. count
  1598. -------
  1599. 1012
  1600. (1 row)
  1601. SELECT count(*) FROM testjsonb WHERE j @@ '$.wait == null';
  1602. count
  1603. -------
  1604. 1
  1605. (1 row)
  1606. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($ ? (@.wait == null))';
  1607. count
  1608. -------
  1609. 1
  1610. (1 row)
  1611. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.wait ? (@ == null))';
  1612. count
  1613. -------
  1614. 1
  1615. (1 row)
  1616. SELECT count(*) FROM testjsonb WHERE j @@ '"CC" == $.wait';
  1617. count
  1618. -------
  1619. 15
  1620. (1 row)
  1621. SELECT count(*) FROM testjsonb WHERE j @@ '$.wait == "CC" && true == $.public';
  1622. count
  1623. -------
  1624. 2
  1625. (1 row)
  1626. SELECT count(*) FROM testjsonb WHERE j @@ '$.age == 25';
  1627. count
  1628. -------
  1629. 2
  1630. (1 row)
  1631. SELECT count(*) FROM testjsonb WHERE j @@ '$.age == 25.0';
  1632. count
  1633. -------
  1634. 2
  1635. (1 row)
  1636. SELECT count(*) FROM testjsonb WHERE j @@ '$.array[*] == "foo"';
  1637. count
  1638. -------
  1639. 3
  1640. (1 row)
  1641. SELECT count(*) FROM testjsonb WHERE j @@ '$.array[*] == "bar"';
  1642. count
  1643. -------
  1644. 3
  1645. (1 row)
  1646. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($ ? (@.array[*] == "bar"))';
  1647. count
  1648. -------
  1649. 3
  1650. (1 row)
  1651. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.array ? (@[*] == "bar"))';
  1652. count
  1653. -------
  1654. 3
  1655. (1 row)
  1656. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.array[*] ? (@ == "bar"))';
  1657. count
  1658. -------
  1659. 3
  1660. (1 row)
  1661. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($)';
  1662. count
  1663. -------
  1664. 1012
  1665. (1 row)
  1666. SELECT count(*) FROM testjsonb WHERE j @? '$.wait ? (@ == null)';
  1667. count
  1668. -------
  1669. 1
  1670. (1 row)
  1671. SELECT count(*) FROM testjsonb WHERE j @? '$.wait ? ("CC" == @)';
  1672. count
  1673. -------
  1674. 15
  1675. (1 row)
  1676. SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.wait == "CC" && true == @.public)';
  1677. count
  1678. -------
  1679. 2
  1680. (1 row)
  1681. SELECT count(*) FROM testjsonb WHERE j @? '$.age ? (@ == 25)';
  1682. count
  1683. -------
  1684. 2
  1685. (1 row)
  1686. SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.age == 25.0)';
  1687. count
  1688. -------
  1689. 2
  1690. (1 row)
  1691. SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.array[*] == "bar")';
  1692. count
  1693. -------
  1694. 3
  1695. (1 row)
  1696. SELECT count(*) FROM testjsonb WHERE j @? '$.array ? (@[*] == "bar")';
  1697. count
  1698. -------
  1699. 3
  1700. (1 row)
  1701. SELECT count(*) FROM testjsonb WHERE j @? '$.array[*] ? (@ == "bar")';
  1702. count
  1703. -------
  1704. 3
  1705. (1 row)
  1706. SELECT count(*) FROM testjsonb WHERE j @? '$';
  1707. count
  1708. -------
  1709. 1012
  1710. (1 row)
  1711. SELECT count(*) FROM testjsonb WHERE j @? '$.public';
  1712. count
  1713. -------
  1714. 194
  1715. (1 row)
  1716. SELECT count(*) FROM testjsonb WHERE j @? '$.bar';
  1717. count
  1718. -------
  1719. 0
  1720. (1 row)
  1721. -- nested tests
  1722. SELECT '{"ff":{"a":12,"b":16}}'::jsonb;
  1723. jsonb
  1724. ----------------------------
  1725. {"ff": {"a": 12, "b": 16}}
  1726. (1 row)
  1727. SELECT '{"ff":{"a":12,"b":16},"qq":123}'::jsonb;
  1728. jsonb
  1729. ---------------------------------------
  1730. {"ff": {"a": 12, "b": 16}, "qq": 123}
  1731. (1 row)
  1732. SELECT '{"aa":["a","aaa"],"qq":{"a":12,"b":16,"c":["c1","c2"],"d":{"d1":"d1","d2":"d2","d1":"d3"}}}'::jsonb;
  1733. jsonb
  1734. --------------------------------------------------------------------------------------------------
  1735. {"aa": ["a", "aaa"], "qq": {"a": 12, "b": 16, "c": ["c1", "c2"], "d": {"d1": "d3", "d2": "d2"}}}
  1736. (1 row)
  1737. SELECT '{"ff":["a","aaa"]}'::jsonb;
  1738. jsonb
  1739. ----------------------
  1740. {"ff": ["a", "aaa"]}
  1741. (1 row)
  1742. SELECT
  1743. '{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb -> 'ff',
  1744. '{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb -> 'qq',
  1745. ('{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb -> 'Y') IS NULL AS f,
  1746. ('{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb ->> 'Y') IS NULL AS t,
  1747. '{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb -> 'x';
  1748. ?column? | ?column? | f | t | ?column?
  1749. --------------------+----------+---+---+----------
  1750. {"a": 12, "b": 16} | 123 | f | t | [1, 2]
  1751. (1 row)
  1752. -- nested containment
  1753. SELECT '{"a":[1,2],"c":"b"}'::jsonb @> '{"a":[1,2]}';
  1754. ?column?
  1755. ----------
  1756. t
  1757. (1 row)
  1758. SELECT '{"a":[2,1],"c":"b"}'::jsonb @> '{"a":[1,2]}';
  1759. ?column?
  1760. ----------
  1761. t
  1762. (1 row)
  1763. SELECT '{"a":{"1":2},"c":"b"}'::jsonb @> '{"a":[1,2]}';
  1764. ?column?
  1765. ----------
  1766. f
  1767. (1 row)
  1768. SELECT '{"a":{"2":1},"c":"b"}'::jsonb @> '{"a":[1,2]}';
  1769. ?column?
  1770. ----------
  1771. f
  1772. (1 row)
  1773. SELECT '{"a":{"1":2},"c":"b"}'::jsonb @> '{"a":{"1":2}}';
  1774. ?column?
  1775. ----------
  1776. t
  1777. (1 row)
  1778. SELECT '{"a":{"2":1},"c":"b"}'::jsonb @> '{"a":{"1":2}}';
  1779. ?column?
  1780. ----------
  1781. f
  1782. (1 row)
  1783. SELECT '["a","b"]'::jsonb @> '["a","b","c","b"]';
  1784. ?column?
  1785. ----------
  1786. f
  1787. (1 row)
  1788. SELECT '["a","b","c","b"]'::jsonb @> '["a","b"]';
  1789. ?column?
  1790. ----------
  1791. t
  1792. (1 row)
  1793. SELECT '["a","b","c",[1,2]]'::jsonb @> '["a",[1,2]]';
  1794. ?column?
  1795. ----------
  1796. t
  1797. (1 row)
  1798. SELECT '["a","b","c",[1,2]]'::jsonb @> '["b",[1,2]]';
  1799. ?column?
  1800. ----------
  1801. t
  1802. (1 row)
  1803. SELECT '{"a":[1,2],"c":"b"}'::jsonb @> '{"a":[1]}';
  1804. ?column?
  1805. ----------
  1806. t
  1807. (1 row)
  1808. SELECT '{"a":[1,2],"c":"b"}'::jsonb @> '{"a":[2]}';
  1809. ?column?
  1810. ----------
  1811. t
  1812. (1 row)
  1813. SELECT '{"a":[1,2],"c":"b"}'::jsonb @> '{"a":[3]}';
  1814. ?column?
  1815. ----------
  1816. f
  1817. (1 row)
  1818. SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"c":3}]}';
  1819. ?column?
  1820. ----------
  1821. t
  1822. (1 row)
  1823. SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"x":4}]}';
  1824. ?column?
  1825. ----------
  1826. t
  1827. (1 row)
  1828. SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"x":4},3]}';
  1829. ?column?
  1830. ----------
  1831. f
  1832. (1 row)
  1833. SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"x":4},1]}';
  1834. ?column?
  1835. ----------
  1836. t
  1837. (1 row)
  1838. -- check some corner cases for indexed nested containment (bug #13756)
  1839. create temp table nestjsonb (j jsonb);
  1840. insert into nestjsonb (j) values ('{"a":[["b",{"x":1}],["b",{"x":2}]],"c":3}');
  1841. insert into nestjsonb (j) values ('[[14,2,3]]');
  1842. insert into nestjsonb (j) values ('[1,[14,2,3]]');
  1843. create index on nestjsonb using gin(j jsonb_path_ops);
  1844. select * from nestjsonb where j @> '{"a":[[{"x":2}]]}'::jsonb;
  1845. j
  1846. ---------------------------------------------------
  1847. {"a": [["b", {"x": 1}], ["b", {"x": 2}]], "c": 3}
  1848. (1 row)
  1849. select * from nestjsonb where j @> '{"c":3}';
  1850. j
  1851. ---------------------------------------------------
  1852. {"a": [["b", {"x": 1}], ["b", {"x": 2}]], "c": 3}
  1853. (1 row)
  1854. select * from nestjsonb where j @> '[[14]]';
  1855. j
  1856. -----------------
  1857. [[14, 2, 3]]
  1858. [1, [14, 2, 3]]
  1859. (2 rows)
  1860. select * from nestjsonb where j @> '{"a":[[{"x":2}]]}'::jsonb;
  1861. j
  1862. ---------------------------------------------------
  1863. {"a": [["b", {"x": 1}], ["b", {"x": 2}]], "c": 3}
  1864. (1 row)
  1865. select * from nestjsonb where j @> '{"c":3}';
  1866. j
  1867. ---------------------------------------------------
  1868. {"a": [["b", {"x": 1}], ["b", {"x": 2}]], "c": 3}
  1869. (1 row)
  1870. select * from nestjsonb where j @> '[[14]]';
  1871. j
  1872. -----------------
  1873. [[14, 2, 3]]
  1874. [1, [14, 2, 3]]
  1875. (2 rows)
  1876. -- nested object field / array index lookup
  1877. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'n';
  1878. ?column?
  1879. ----------
  1880. null
  1881. (1 row)
  1882. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'a';
  1883. ?column?
  1884. ----------
  1885. 1
  1886. (1 row)
  1887. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'b';
  1888. ?column?
  1889. ----------
  1890. [1, 2]
  1891. (1 row)
  1892. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'c';
  1893. ?column?
  1894. ----------
  1895. {"1": 2}
  1896. (1 row)
  1897. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'd';
  1898. ?column?
  1899. ---------------
  1900. {"1": [2, 3]}
  1901. (1 row)
  1902. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'd' -> '1';
  1903. ?column?
  1904. ----------
  1905. [2, 3]
  1906. (1 row)
  1907. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'e';
  1908. ?column?
  1909. ----------
  1910. (1 row)
  1911. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 0; --expecting error
  1912. ?column?
  1913. ----------
  1914. (1 row)
  1915. SELECT '["a","b","c",[1,2],null]'::jsonb -> 0;
  1916. ?column?
  1917. ----------
  1918. "a"
  1919. (1 row)
  1920. SELECT '["a","b","c",[1,2],null]'::jsonb -> 1;
  1921. ?column?
  1922. ----------
  1923. "b"
  1924. (1 row)
  1925. SELECT '["a","b","c",[1,2],null]'::jsonb -> 2;
  1926. ?column?
  1927. ----------
  1928. "c"
  1929. (1 row)
  1930. SELECT '["a","b","c",[1,2],null]'::jsonb -> 3;
  1931. ?column?
  1932. ----------
  1933. [1, 2]
  1934. (1 row)
  1935. SELECT '["a","b","c",[1,2],null]'::jsonb -> 3 -> 1;
  1936. ?column?
  1937. ----------
  1938. 2
  1939. (1 row)
  1940. SELECT '["a","b","c",[1,2],null]'::jsonb -> 4;
  1941. ?column?
  1942. ----------
  1943. null
  1944. (1 row)
  1945. SELECT '["a","b","c",[1,2],null]'::jsonb -> 5;
  1946. ?column?
  1947. ----------
  1948. (1 row)
  1949. SELECT '["a","b","c",[1,2],null]'::jsonb -> -1;
  1950. ?column?
  1951. ----------
  1952. null
  1953. (1 row)
  1954. SELECT '["a","b","c",[1,2],null]'::jsonb -> -5;
  1955. ?column?
  1956. ----------
  1957. "a"
  1958. (1 row)
  1959. SELECT '["a","b","c",[1,2],null]'::jsonb -> -6;
  1960. ?column?
  1961. ----------
  1962. (1 row)
  1963. --nested exists
  1964. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'n';
  1965. ?column?
  1966. ----------
  1967. t
  1968. (1 row)
  1969. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'a';
  1970. ?column?
  1971. ----------
  1972. t
  1973. (1 row)
  1974. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'b';
  1975. ?column?
  1976. ----------
  1977. t
  1978. (1 row)
  1979. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'c';
  1980. ?column?
  1981. ----------
  1982. t
  1983. (1 row)
  1984. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'd';
  1985. ?column?
  1986. ----------
  1987. t
  1988. (1 row)
  1989. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'e';
  1990. ?column?
  1991. ----------
  1992. f
  1993. (1 row)
  1994. -- jsonb_strip_nulls
  1995. select jsonb_strip_nulls(null);
  1996. jsonb_strip_nulls
  1997. -------------------
  1998. (1 row)
  1999. select jsonb_strip_nulls('1');
  2000. jsonb_strip_nulls
  2001. -------------------
  2002. 1
  2003. (1 row)
  2004. select jsonb_strip_nulls('"a string"');
  2005. jsonb_strip_nulls
  2006. -------------------
  2007. "a string"
  2008. (1 row)
  2009. select jsonb_strip_nulls('null');
  2010. jsonb_strip_nulls
  2011. -------------------
  2012. null
  2013. (1 row)
  2014. select jsonb_strip_nulls('[1,2,null,3,4]');
  2015. jsonb_strip_nulls
  2016. --------------------
  2017. [1, 2, null, 3, 4]
  2018. (1 row)
  2019. select jsonb_strip_nulls('{"a":1,"b":null,"c":[2,null,3],"d":{"e":4,"f":null}}');
  2020. jsonb_strip_nulls
  2021. --------------------------------------------
  2022. {"a": 1, "c": [2, null, 3], "d": {"e": 4}}
  2023. (1 row)
  2024. select jsonb_strip_nulls('[1,{"a":1,"b":null,"c":2},3]');
  2025. jsonb_strip_nulls
  2026. --------------------------
  2027. [1, {"a": 1, "c": 2}, 3]
  2028. (1 row)
  2029. -- an empty object is not null and should not be stripped
  2030. select jsonb_strip_nulls('{"a": {"b": null, "c": null}, "d": {} }');
  2031. jsonb_strip_nulls
  2032. --------------------
  2033. {"a": {}, "d": {}}
  2034. (1 row)
  2035. select jsonb_concat('{"d": "test", "a": [1, 2]}', '{"g": "test2", "c": {"c1":1, "c2":2}}');
  2036. jsonb_concat
  2037. -------------------------------------------------------------------
  2038. {"a": [1, 2], "c": {"c1": 1, "c2": 2}, "d": "test", "g": "test2"}
  2039. (1 row)
  2040. select '{"aa":1 , "b":2, "cq":3}'::jsonb || '{"cq":"l", "b":"g", "fg":false}';
  2041. ?column?
  2042. ---------------------------------------------
  2043. {"b": "g", "aa": 1, "cq": "l", "fg": false}
  2044. (1 row)
  2045. select '{"aa":1 , "b":2, "cq":3}'::jsonb || '{"aq":"l"}';
  2046. ?column?
  2047. ---------------------------------------
  2048. {"b": 2, "aa": 1, "aq": "l", "cq": 3}
  2049. (1 row)
  2050. select '{"aa":1 , "b":2, "cq":3}'::jsonb || '{"aa":"l"}';
  2051. ?column?
  2052. ------------------------------
  2053. {"b": 2, "aa": "l", "cq": 3}
  2054. (1 row)
  2055. select '{"aa":1 , "b":2, "cq":3}'::jsonb || '{}';
  2056. ?column?
  2057. ----------------------------
  2058. {"b": 2, "aa": 1, "cq": 3}
  2059. (1 row)
  2060. select '["a", "b"]'::jsonb || '["c"]';
  2061. ?column?
  2062. -----------------
  2063. ["a", "b", "c"]
  2064. (1 row)
  2065. select '["a", "b"]'::jsonb || '["c", "d"]';
  2066. ?column?
  2067. ----------------------
  2068. ["a", "b", "c", "d"]
  2069. (1 row)
  2070. select '["c"]' || '["a", "b"]'::jsonb;
  2071. ?column?
  2072. -----------------
  2073. ["c", "a", "b"]
  2074. (1 row)
  2075. select '["a", "b"]'::jsonb || '"c"';
  2076. ?column?
  2077. -----------------
  2078. ["a", "b", "c"]
  2079. (1 row)
  2080. select '"c"' || '["a", "b"]'::jsonb;
  2081. ?column?
  2082. -----------------
  2083. ["c", "a", "b"]
  2084. (1 row)
  2085. select '[]'::jsonb || '["a"]'::jsonb;
  2086. ?column?
  2087. ----------
  2088. ["a"]
  2089. (1 row)
  2090. select '[]'::jsonb || '"a"'::jsonb;
  2091. ?column?
  2092. ----------
  2093. ["a"]
  2094. (1 row)
  2095. select '"b"'::jsonb || '"a"'::jsonb;
  2096. ?column?
  2097. ------------
  2098. ["b", "a"]
  2099. (1 row)
  2100. select '{}'::jsonb || '{"a":"b"}'::jsonb;
  2101. ?column?
  2102. ------------
  2103. {"a": "b"}
  2104. (1 row)
  2105. select '[]'::jsonb || '{"a":"b"}'::jsonb;
  2106. ?column?
  2107. --------------
  2108. [{"a": "b"}]
  2109. (1 row)
  2110. select '{"a":"b"}'::jsonb || '[]'::jsonb;
  2111. ?column?
  2112. --------------
  2113. [{"a": "b"}]
  2114. (1 row)
  2115. select '"a"'::jsonb || '{"a":1}';
  2116. ?column?
  2117. -----------------
  2118. ["a", {"a": 1}]
  2119. (1 row)
  2120. select '{"a":1}' || '"a"'::jsonb;
  2121. ?column?
  2122. -----------------
  2123. [{"a": 1}, "a"]
  2124. (1 row)
  2125. select '[3]'::jsonb || '{}'::jsonb;
  2126. ?column?
  2127. ----------
  2128. [3, {}]
  2129. (1 row)
  2130. select '3'::jsonb || '[]'::jsonb;
  2131. ?column?
  2132. ----------
  2133. [3]
  2134. (1 row)
  2135. select '3'::jsonb || '4'::jsonb;
  2136. ?column?
  2137. ----------
  2138. [3, 4]
  2139. (1 row)
  2140. select '3'::jsonb || '{}'::jsonb;
  2141. ?column?
  2142. ----------
  2143. [3, {}]
  2144. (1 row)
  2145. select '["a", "b"]'::jsonb || '{"c":1}';
  2146. ?column?
  2147. ----------------------
  2148. ["a", "b", {"c": 1}]
  2149. (1 row)
  2150. select '{"c": 1}'::jsonb || '["a", "b"]';
  2151. ?column?
  2152. ----------------------
  2153. [{"c": 1}, "a", "b"]
  2154. (1 row)
  2155. select '{}'::jsonb || '{"cq":"l", "b":"g", "fg":false}';
  2156. ?column?
  2157. ------------------------------------
  2158. {"b": "g", "cq": "l", "fg": false}
  2159. (1 row)
  2160. select pg_column_size('{}'::jsonb || '{}'::jsonb) = pg_column_size('{}'::jsonb);
  2161. ?column?
  2162. ----------
  2163. t
  2164. (1 row)
  2165. select pg_column_size('{"aa":1}'::jsonb || '{"b":2}'::jsonb) = pg_column_size('{"aa":1, "b":2}'::jsonb);
  2166. ?column?
  2167. ----------
  2168. t
  2169. (1 row)
  2170. select pg_column_size('{"aa":1, "b":2}'::jsonb || '{}'::jsonb) = pg_column_size('{"aa":1, "b":2}'::jsonb);
  2171. ?column?
  2172. ----------
  2173. t
  2174. (1 row)
  2175. select pg_column_size('{}'::jsonb || '{"aa":1, "b":2}'::jsonb) = pg_column_size('{"aa":1, "b":2}'::jsonb);
  2176. ?column?
  2177. ----------
  2178. t
  2179. (1 row)
  2180. select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'a');
  2181. jsonb_delete
  2182. ------------------
  2183. {"b": 2, "c": 3}
  2184. (1 row)
  2185. select jsonb_delete('{"a":null , "b":2, "c":3}'::jsonb, 'a');
  2186. jsonb_delete
  2187. ------------------
  2188. {"b": 2, "c": 3}
  2189. (1 row)
  2190. select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'b');
  2191. jsonb_delete
  2192. ------------------
  2193. {"a": 1, "c": 3}
  2194. (1 row)
  2195. select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'c');
  2196. jsonb_delete
  2197. ------------------
  2198. {"a": 1, "b": 2}
  2199. (1 row)
  2200. select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'd');
  2201. jsonb_delete
  2202. --------------------------
  2203. {"a": 1, "b": 2, "c": 3}
  2204. (1 row)
  2205. select '{"a":1 , "b":2, "c":3}'::jsonb - 'a';
  2206. ?column?
  2207. ------------------
  2208. {"b": 2, "c": 3}
  2209. (1 row)
  2210. select '{"a":null , "b":2, "c":3}'::jsonb - 'a';
  2211. ?column?
  2212. ------------------
  2213. {"b": 2, "c": 3}
  2214. (1 row)
  2215. select '{"a":1 , "b":2, "c":3}'::jsonb - 'b';
  2216. ?column?
  2217. ------------------
  2218. {"a": 1, "c": 3}
  2219. (1 row)
  2220. select '{"a":1 , "b":2, "c":3}'::jsonb - 'c';
  2221. ?column?
  2222. ------------------
  2223. {"a": 1, "b": 2}
  2224. (1 row)
  2225. select '{"a":1 , "b":2, "c":3}'::jsonb - 'd';
  2226. ?column?
  2227. --------------------------
  2228. {"a": 1, "b": 2, "c": 3}
  2229. (1 row)
  2230. select pg_column_size('{"a":1 , "b":2, "c":3}'::jsonb - 'b') = pg_column_size('{"a":1, "b":2}'::jsonb);
  2231. ?column?
  2232. ----------
  2233. t
  2234. (1 row)
  2235. select '["a","b","c"]'::jsonb - 3;
  2236. ?column?
  2237. -----------------
  2238. ["a", "b", "c"]
  2239. (1 row)
  2240. select '["a","b","c"]'::jsonb - 2;
  2241. ?column?
  2242. ------------
  2243. ["a", "b"]
  2244. (1 row)
  2245. select '["a","b","c"]'::jsonb - 1;
  2246. ?column?
  2247. ------------
  2248. ["a", "c"]
  2249. (1 row)
  2250. select '["a","b","c"]'::jsonb - 0;
  2251. ?column?
  2252. ------------
  2253. ["b", "c"]
  2254. (1 row)
  2255. select '["a","b","c"]'::jsonb - -1;
  2256. ?column?
  2257. ------------
  2258. ["a", "b"]
  2259. (1 row)
  2260. select '["a","b","c"]'::jsonb - -2;
  2261. ?column?
  2262. ------------
  2263. ["a", "c"]
  2264. (1 row)
  2265. select '["a","b","c"]'::jsonb - -3;
  2266. ?column?
  2267. ------------
  2268. ["b", "c"]
  2269. (1 row)
  2270. select '["a","b","c"]'::jsonb - -4;
  2271. ?column?
  2272. -----------------
  2273. ["a", "b", "c"]
  2274. (1 row)
  2275. select jsonb_delete_path('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}', '{n}');
  2276. jsonb_delete_path
  2277. ----------------------------------------------------------
  2278. {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [2, 3]}}
  2279. (1 row)
  2280. select jsonb_delete_path('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}', '{b,-1}');
  2281. jsonb_delete_path
  2282. ------------------------------------------------------------------
  2283. {"a": 1, "b": [1], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null}
  2284. (1 row)
  2285. select jsonb_delete_path('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}', '{d,1,0}');
  2286. jsonb_delete_path
  2287. ------------------------------------------------------------------
  2288. {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [3]}, "n": null}
  2289. (1 row)
  2290. select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{n}';
  2291. ?column?
  2292. ----------------------------------------------------------
  2293. {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [2, 3]}}
  2294. (1 row)
  2295. select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{b,-1}';
  2296. ?column?
  2297. ------------------------------------------------------------------
  2298. {"a": 1, "b": [1], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null}
  2299. (1 row)
  2300. select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{b,-1e}'; -- invalid array subscript
  2301. ERROR: path element at position 2 is not an integer: "-1e"
  2302. select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{d,1,0}';
  2303. ?column?
  2304. ------------------------------------------------------------------
  2305. {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [3]}, "n": null}
  2306. (1 row)
  2307. -- empty structure and error conditions for delete and replace
  2308. select '"a"'::jsonb - 'a'; -- error
  2309. ERROR: cannot delete from scalar
  2310. select '{}'::jsonb - 'a';
  2311. ?column?
  2312. ----------
  2313. {}
  2314. (1 row)
  2315. select '[]'::jsonb - 'a';
  2316. ?column?
  2317. ----------
  2318. []
  2319. (1 row)
  2320. select '"a"'::jsonb - 1; -- error
  2321. ERROR: cannot delete from scalar
  2322. select '{}'::jsonb - 1; -- error
  2323. ERROR: cannot delete from object using integer index
  2324. select '[]'::jsonb - 1;
  2325. ?column?
  2326. ----------
  2327. []
  2328. (1 row)
  2329. select '"a"'::jsonb #- '{a}'; -- error
  2330. ERROR: cannot delete path in scalar
  2331. select '{}'::jsonb #- '{a}';
  2332. ?column?
  2333. ----------
  2334. {}
  2335. (1 row)
  2336. select '[]'::jsonb #- '{a}';
  2337. ?column?
  2338. ----------
  2339. []
  2340. (1 row)
  2341. select jsonb_set('{}','{a}','"b"', false);
  2342. jsonb_set
  2343. -----------
  2344. {}
  2345. (1 row)
  2346. select jsonb_set('[]','{1}','"b"', false);
  2347. jsonb_set
  2348. -----------
  2349. []
  2350. (1 row)
  2351. select jsonb_set('[{"f1":1,"f2":null},2,null,3]', '{0}','[2,3,4]', false);
  2352. jsonb_set
  2353. -------------------------
  2354. [[2, 3, 4], 2, null, 3]
  2355. (1 row)
  2356. -- jsonb_set_lax
  2357. \pset null NULL
  2358. -- errors
  2359. select jsonb_set_lax('{"a":1,"b":2}', '{b}', null, true, null);
  2360. ERROR: null_value_treatment must be "delete_key", "return_target", "use_json_null", or "raise_exception"
  2361. select jsonb_set_lax('{"a":1,"b":2}', '{b}', null, true, 'no_such_treatment');
  2362. ERROR: null_value_treatment must be "delete_key", "return_target", "use_json_null", or "raise_exception"
  2363. \pset null ''
  2364. select jsonb_insert('{"a": [0,1,2]}', '{a, 1}', '"new_value"', true);
  2365. jsonb_insert
  2366. -------------------------------
  2367. {"a": [0, 1, "new_value", 2]}
  2368. (1 row)
  2369. select jsonb_insert('{"a": {"b": {"c": [0, 1, "test1", "test2"]}}}', '{a, b, c, 2}', '"new_value"', true);
  2370. jsonb_insert
  2371. ------------------------------------------------------------
  2372. {"a": {"b": {"c": [0, 1, "test1", "new_value", "test2"]}}}
  2373. (1 row)
  2374. select jsonb_insert('{"a": [0,1,2]}', '{a, 0}', '"new_value"', true);
  2375. jsonb_insert
  2376. -------------------------------
  2377. {"a": [0, "new_value", 1, 2]}
  2378. (1 row)
  2379. select jsonb_insert('{"a": [0,1,2]}', '{a, 2}', '"new_value"', true);
  2380. jsonb_insert
  2381. -------------------------------
  2382. {"a": [0, 1, 2, "new_value"]}
  2383. (1 row)
  2384. select jsonb_insert('{"a": [0,1,2]}', '{a, -1}', '"new_value"', true);
  2385. jsonb_insert
  2386. -------------------------------
  2387. {"a": [0, 1, 2, "new_value"]}
  2388. (1 row)
  2389. select jsonb_insert('[]', '{1}', '"new_value"', true);
  2390. jsonb_insert
  2391. ---------------
  2392. ["new_value"]
  2393. (1 row)
  2394. select jsonb_insert('{"a": []}', '{a, 1}', '"new_value"', true);
  2395. jsonb_insert
  2396. ----------------------
  2397. {"a": ["new_value"]}
  2398. (1 row)
  2399. select jsonb_insert('{"a": {"b": "value"}}', '{a, c}', '"new_value"', true);
  2400. jsonb_insert
  2401. -----------------------------------------
  2402. {"a": {"b": "value", "c": "new_value"}}
  2403. (1 row)
  2404. select jsonb_insert('{"a": {"b": "value"}}', '{a, b}', '"new_value"', true);
  2405. ERROR: cannot replace existing key
  2406. HINT: Try using the function jsonb_set to replace key value.
  2407. create TEMP TABLE test_jsonb_subscript (
  2408. id int,
  2409. test_json jsonb
  2410. );
  2411. insert into test_jsonb_subscript values
  2412. (1, '{}'), -- empty jsonb
  2413. (2, '{"key": "value"}'); -- jsonb with data
  2414. -- NULL as jsonb source
  2415. insert into test_jsonb_subscript values (3, NULL);
  2416. insert into test_jsonb_subscript values (1, '[0]');
  2417. insert into test_jsonb_subscript values (1, '[]');
  2418. insert into test_jsonb_subscript values (1, '{}');
  2419. insert into test_jsonb_subscript values (1, '{}');
  2420. insert into test_jsonb_subscript values (1, '{"b": 1}');
  2421. insert into test_jsonb_subscript values (1, '{}');
  2422. insert into test_jsonb_subscript values (1, '[]');
  2423. insert into test_jsonb_subscript values (1, '{}');
  2424. insert into test_jsonb_subscript values (1, '[]');
  2425. insert into test_jsonb_subscript values (1, '{}');
  2426. insert into test_jsonb_subscript values (1, '{"a": {}}');
  2427. insert into test_jsonb_subscript values (1, '{"a": []}');
  2428. insert into test_jsonb_subscript values (1, '{"a": 1}');
  2429. insert into test_jsonb_subscript values (1, 'null');
  2430. -- casts
  2431. select 'true'::jsonb::bool;
  2432. bool
  2433. ------
  2434. t
  2435. (1 row)
  2436. select '[]'::jsonb::bool;
  2437. ERROR: cannot cast jsonb array to type boolean
  2438. select '1.0'::jsonb::float;
  2439. float8
  2440. --------
  2441. 1
  2442. (1 row)
  2443. select '[1.0]'::jsonb::float;
  2444. ERROR: cannot cast jsonb array to type double precision
  2445. select '12345'::jsonb::int4;
  2446. int4
  2447. -------
  2448. 12345
  2449. (1 row)
  2450. select '"hello"'::jsonb::int4;
  2451. ERROR: cannot cast jsonb string to type integer
  2452. select '12345'::jsonb::numeric;
  2453. numeric
  2454. ---------
  2455. 12345
  2456. (1 row)
  2457. select '{}'::jsonb::numeric;
  2458. ERROR: cannot cast jsonb object to type numeric
  2459. select '12345.05'::jsonb::numeric;
  2460. numeric
  2461. ----------
  2462. 12345.05
  2463. (1 row)
  2464. select '12345.05'::jsonb::float4;
  2465. float4
  2466. ----------
  2467. 12345.05
  2468. (1 row)
  2469. select '12345.05'::jsonb::float8;
  2470. float8
  2471. ----------
  2472. 12345.05
  2473. (1 row)
  2474. select '12345.05'::jsonb::int2;
  2475. int2
  2476. -------
  2477. 12345
  2478. (1 row)
  2479. select '12345.05'::jsonb::int4;
  2480. int4
  2481. -------
  2482. 12345
  2483. (1 row)
  2484. select '12345.05'::jsonb::int8;
  2485. int8
  2486. -------
  2487. 12345
  2488. (1 row)
  2489. select '12345.0000000000000000000000000000000000000000000005'::jsonb::numeric;
  2490. numeric
  2491. ------------------------------------------------------
  2492. 12345.0000000000000000000000000000000000000000000005
  2493. (1 row)
  2494. select '12345.0000000000000000000000000000000000000000000005'::jsonb::float4;
  2495. float4
  2496. --------
  2497. 12345
  2498. (1 row)
  2499. select '12345.0000000000000000000000000000000000000000000005'::jsonb::float8;
  2500. float8
  2501. --------
  2502. 12345
  2503. (1 row)
  2504. select '12345.0000000000000000000000000000000000000000000005'::jsonb::int2;
  2505. int2
  2506. -------
  2507. 12345
  2508. (1 row)
  2509. select '12345.0000000000000000000000000000000000000000000005'::jsonb::int4;
  2510. int4
  2511. -------
  2512. 12345
  2513. (1 row)
  2514. select '12345.0000000000000000000000000000000000000000000005'::jsonb::int8;
  2515. int8
  2516. -------
  2517. 12345
  2518. (1 row)