postgresql的json *** 作

postgresql的json *** 作,第1张

概述PostgreSQL 9.3.4 Documentation Prev Up Chapter 9. Functions and Operators Next 9.15. JSON Functions and Operators Table 9-39 shows the operators that are available for use with JSON (see Section 8.14)
PostgreSQL 9.3.4 Documentation
Prev Up Chapter 9. Functions and Operators Next

9.15. JSON Functions and Operators

Table 9-39shows the operators that are available for use with JsON (seeSection 8.14) data.

table 9-39. JsON Operators

Operator Right Operand Type Description Example
-> int Get JsON array element '[1,2,3]'::Json->2
text Get JsON object fIEld '{"a":1,"b":2}'::Json->'b'
->> Get JsON array element as text Get JsON object fIEld as text
#> array of text Get JsON object at specifIEd path '{"a":[1,3],"b":[4,5,6]}'::Json#>'{a,2}'
#>> Get JsON object at specifIEd path as text Table 9-40shows the functions that are available for creating and manipulating JsON (seeSection 8.14) data.

table 9-40. JsON Support Functions

Function Return Type Example Example Result
array_to_Json(anyarray [,pretty_bool]) Json Returns the array as JsON. A Postgresql multIDimensional array becomes a JsON array of arrays. line Feeds will be added between dimension 1 elements ifpretty_boolis true. array_to_Json('{{1,5},{99,100}}'::int[]) [[1,5],[99,100]]
row_to_Json(record [,255); padding:0.5ex"> Returns the row as JsON. line Feeds will be added between level 1 elements ifpretty_boolis true. row_to_Json(row(1,'foo')) {"f1":1,"f2":"foo"}
to_Json(anyelement) Returns the value as JsON. If the data type is not built in,and there is a cast from the type toJson,the cast function will be used to perform the conversion. Otherwise,for any value other than a number,a Boolean,or a null value,the text representation will be used,escaped and quoted so that it is legal JsON. to_Json('Fred saID "Hi."'::text) "Fred saID \"Hi.\""
Json_array_length(Json) int Returns the number of elements in the outermost JsON array. Json_array_length('[1,3,{"f1":1,"f2":[5,6]},4]') 5
Json_each(Json) SetoF key text,value Json Expands the outermost JsON object into a set of key/value pairs. select * from Json_each('{"a":"foo","b":"bar"}')
 key | value-----+------- a   | "foo" b   | "bar" 
Json_each_text(from_Json Json) Expands the outermost JsON object into a set of key/value pairs. The returned value will be of type text. select * from Json_each_text('{"a":"foo",255); padding:0.5ex">
 key | value-----+------- a   | foo b   | bar 
Json_extract_path(from_Json Json,VARIADIC path_elems text[]) Returns JsON object pointed to bypath_elems. Json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"foo"}}','f4') {"f5":99,"f6":"foo"}
Json_extract_path_text(from_Json Json,239); padding:0.5ex"> text Returns JsON object pointed to bypath_elems. Json_extract_path_text('{"f2":{"f3":1},'f4','f6') foo
Json_object_keys(Json) SetoF text Returns set of keys in the JsON object. Only the"outer"object will be displayed. Json_object_keys('{"f1":"abc","f2":{"f3":"a","f4":"b"}}')
 Json_object_keys------------------ f1 f2
Json_populate_record(base anyelement,from_Json Json,[,use_Json_as_text bool=false] anyelement Expands the object infrom_Jsonto a row whose columns match the record type defined by base. Conversion will be best effort; columns in base with no corresponding key infrom_Jsonwill be left null. If a column is specifIEd more than once,the last value is used. select * from Json_populate_record(null::x,'{"a":1,"b":2}')
 a | b---+--- 1 | 2
Json_populate_recordset(base anyelement,255); padding:0.5ex"> SetoF anyelement Expands the outermost set of objects infrom_Jsonto a set whose columns match the record type defined by base. Conversion will be best effort; columns in base with no corresponding key infrom_Jsonwill be left null. If a column is specifIEd more than once,255); padding:0.5ex"> select * from Json_populate_recordset(null::x,'[{"a":1,"b":2},{"a":3,"b":4}]')
 a | b---+--- 1 | 2 3 | 4 
Json_array_elements(Json) SetoF Json Expands a JsON array to a set of JsON elements. Json_array_elements('[1,true,[2,false]]')
   value----------- 1 true [2,false]

Note:TheJsonfunctions and operators can impose stricter valIDity requirements than the type's input functions. In particular,they check much more closely that any use of Unicode surrogate pairs to designate characters outsIDe the Unicode Basic Multilingual Plane is correct.

Note:Many of these functions and operators will convert Unicode escapes in the JsON text to the appropriate UTF8 character when the database enCoding is UTF8. In other enCodings the escape sequence must be for an ASCII character,and any other code point in a Unicode escape sequence will result in an error. In general,it is best to avoID mixing Unicode escapes in JsON with a non-UTF8 database enCoding,if possible.

Note:Thehstoreextension has a cast fromhstoretoJson,so that convertedhstorevalues are represented as JsON objects,not as string values.

See alsoSection 9.20about the aggregate functionJson_aggwhich aggregates record values as JsON efficIEntly.

总结

以上是内存溢出为你收集整理的postgresql的json *** 作全部内容,希望文章能够帮你解决postgresql的json *** 作所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1176924.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)