Skip to content

Commit 0c0b7c2

Browse files
authored
Document json_object_pack (geldata#5154)
1 parent 2c59994 commit 0c0b7c2

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

docs/stdlib/json.rst

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ JSON
4444
* - :eql:func:`json_array_unpack`
4545
- :eql:func-desc:`json_array_unpack`
4646

47+
* - :eql:func:`json_object_pack`
48+
- :eql:func-desc:`json_object_pack`
49+
4750
* - :eql:func:`json_object_unpack`
4851
- :eql:func-desc:`json_object_unpack`
4952

@@ -76,6 +79,17 @@ Any value in EdgeDB can be cast to a :eql:type:`json` type as well:
7679
db> select <json>cal::to_local_date(datetime_current(), 'UTC');
7780
{Json("\"2022-11-21\"")}
7881
82+
.. versionadded:: 3.0
83+
84+
The :eql:func:`json_object_pack` function provides one more way to
85+
construct JSON. It constructs a JSON object from an array of key/value
86+
tuples:
87+
88+
.. code-block:: edgeql-repl
89+
90+
db> select json_object_pack({("hello", <json>"world")});
91+
{Json("{\"hello\": \"world\"}")}
92+
7993
Additionally, any :eql:type:`Object` in EdgeDB can be cast as a
8094
:eql:type:`json` type. This produces the same JSON value as the
8195
JSON-serialized result of that said object. Furthermore, this result will
@@ -457,6 +471,37 @@ reversible (i.e., it is not possible to cast a JSON value directly into a
457471
----------
458472

459473

474+
.. eql:function:: std::json_object_pack(pairs: SET OF tuple<str, json>) -> \
475+
json
476+
477+
.. versionadded:: 3.0
478+
479+
Returns the given set of key/value tuples as a JSON object.
480+
481+
.. code-block:: edgeql-repl
482+
483+
db> select json_object_pack({
484+
... ("foo", to_json("1")),
485+
... ("bar", to_json("null")),
486+
... ("baz", to_json("[]"))
487+
... });
488+
{Json("{\"bar\": null, \"baz\": [], \"foo\": 1}")}
489+
490+
If the key/value tuples being packed have common keys, the last value for
491+
each key will make the final object.
492+
493+
.. code-block:: edgeql-repl
494+
495+
db> select json_object_pack({
496+
... ("hello", <json>"world"),
497+
... ("hello", <json>true)
498+
... });
499+
{Json("{\"hello\": true}")}
500+
501+
502+
----------
503+
504+
460505
.. eql:function:: std::json_object_unpack(json: json) -> \
461506
set of tuple<str, json>
462507

0 commit comments

Comments
 (0)