@@ -653,6 +653,45 @@ where json_type = 'array';
653
653
t
654
654
(1 row)
655
655
656
+ -- corner cases
657
+ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> null::text;
658
+ ?column?
659
+ ----------
660
+
661
+ (1 row)
662
+
663
+ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> null::int;
664
+ ?column?
665
+ ----------
666
+
667
+ (1 row)
668
+
669
+ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 1;
670
+ ERROR: cannot extract array element from a non-array
671
+ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 'z';
672
+ ?column?
673
+ ----------
674
+
675
+ (1 row)
676
+
677
+ select '[{"b": "c"}, {"b": "cc"}]'::json -> 1;
678
+ ?column?
679
+ -------------
680
+ {"b": "cc"}
681
+ (1 row)
682
+
683
+ select '[{"b": "c"}, {"b": "cc"}]'::json -> 3;
684
+ ?column?
685
+ ----------
686
+
687
+ (1 row)
688
+
689
+ select '[{"b": "c"}, {"b": "cc"}]'::json -> 'z';
690
+ ERROR: cannot extract field from a non-object
691
+ select '"foo"'::json -> 1;
692
+ ERROR: cannot extract element from a scalar
693
+ select '"foo"'::json -> 'z';
694
+ ERROR: cannot extract element from a scalar
656
695
-- array length
657
696
SELECT json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]');
658
697
json_array_length
@@ -831,53 +870,161 @@ select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','1'];
831
870
1
832
871
(1 row)
833
872
834
- -- same using array literals
835
- select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>'{f4,f6}';
836
- ?column?
837
- -----------
838
- "stringy"
873
+ -- corner cases for same
874
+ select '{"a": {"b":{"c": "foo"}}}'::json #> array[]::text[];
875
+ ?column?
876
+ ----------
877
+
878
+ (1 row)
879
+
880
+ select '{"a": {"b":{"c": "foo"}}}'::json #> array['a'];
881
+ ?column?
882
+ --------------------
883
+ {"b":{"c": "foo"}}
884
+ (1 row)
885
+
886
+ select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b'];
887
+ ?column?
888
+ --------------
889
+ {"c": "foo"}
839
890
(1 row)
840
891
841
- select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}} '::json#>'{f2}' ;
892
+ select '{"a": {"b":{"c": "foo"}}} '::json #> array['a','b','c'] ;
842
893
?column?
843
894
----------
844
- {"f3":1}
895
+ "foo"
845
896
(1 row)
846
897
847
- select '{"f2":["f3",1],"f4" :{"f5":99,"f6":"stringy"}} '::json#>'{f2,0}' ;
898
+ select '{"a": {"b" :{"c": "foo"}}} '::json #> array['a','b','c','d'] ;
848
899
?column?
849
900
----------
850
- "f3"
901
+
851
902
(1 row)
852
903
853
- select '{"f2":["f3",1],"f4" :{"f5":99,"f6":"stringy"}} '::json#>'{f2,1}' ;
904
+ select '{"a": {"b" :{"c": "foo"}}} '::json #> array['a','z','c'] ;
854
905
?column?
855
906
----------
856
- 1
907
+
857
908
(1 row)
858
909
859
- select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"} }'::json#>>'{f4,f6}' ;
910
+ select '{"a": [{"b": "c"}, {"b": "cc"}] }'::json #> array['a','1','b'] ;
860
911
?column?
861
912
----------
862
- stringy
913
+ "cc"
863
914
(1 row)
864
915
865
- select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"} }'::json#>>'{f2}' ;
916
+ select '{"a": [{"b": "c"}, {"b": "cc"}] }'::json #> array['a','z','b'] ;
866
917
?column?
867
918
----------
868
- {"f3":1}
919
+
869
920
(1 row)
870
921
871
- select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}} '::json#>>'{f2,0}' ;
922
+ select '[{"b": "c"}, {"b": "cc"}] '::json #> array['1','b'] ;
872
923
?column?
873
924
----------
874
- f3
925
+ "cc"
875
926
(1 row)
876
927
877
- select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}} '::json#>>'{f2,1}' ;
928
+ select '[{"b": "c"}, {"b": "cc"}] '::json #> array['z','b'] ;
878
929
?column?
879
930
----------
880
- 1
931
+
932
+ (1 row)
933
+
934
+ select '"foo"'::json #> array['z'];
935
+ ?column?
936
+ ----------
937
+
938
+ (1 row)
939
+
940
+ select '42'::json #> array['f2'];
941
+ ?column?
942
+ ----------
943
+
944
+ (1 row)
945
+
946
+ select '42'::json #> array['0'];
947
+ ?column?
948
+ ----------
949
+
950
+ (1 row)
951
+
952
+ select '{"a": {"b":{"c": "foo"}}}'::json #>> array[]::text[];
953
+ ?column?
954
+ ----------
955
+
956
+ (1 row)
957
+
958
+ select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a'];
959
+ ?column?
960
+ --------------------
961
+ {"b":{"c": "foo"}}
962
+ (1 row)
963
+
964
+ select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b'];
965
+ ?column?
966
+ --------------
967
+ {"c": "foo"}
968
+ (1 row)
969
+
970
+ select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b','c'];
971
+ ?column?
972
+ ----------
973
+ foo
974
+ (1 row)
975
+
976
+ select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b','c','d'];
977
+ ?column?
978
+ ----------
979
+
980
+ (1 row)
981
+
982
+ select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','z','c'];
983
+ ?column?
984
+ ----------
985
+
986
+ (1 row)
987
+
988
+ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #>> array['a','1','b'];
989
+ ?column?
990
+ ----------
991
+ cc
992
+ (1 row)
993
+
994
+ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #>> array['a','z','b'];
995
+ ?column?
996
+ ----------
997
+
998
+ (1 row)
999
+
1000
+ select '[{"b": "c"}, {"b": "cc"}]'::json #>> array['1','b'];
1001
+ ?column?
1002
+ ----------
1003
+ cc
1004
+ (1 row)
1005
+
1006
+ select '[{"b": "c"}, {"b": "cc"}]'::json #>> array['z','b'];
1007
+ ?column?
1008
+ ----------
1009
+
1010
+ (1 row)
1011
+
1012
+ select '"foo"'::json #>> array['z'];
1013
+ ?column?
1014
+ ----------
1015
+
1016
+ (1 row)
1017
+
1018
+ select '42'::json #>> array['f2'];
1019
+ ?column?
1020
+ ----------
1021
+
1022
+ (1 row)
1023
+
1024
+ select '42'::json #>> array['0'];
1025
+ ?column?
1026
+ ----------
1027
+
881
1028
(1 row)
882
1029
883
1030
-- array_elements
0 commit comments