|
44 | 44 | * - :eql:func:`json_array_unpack`
|
45 | 45 | - :eql:func-desc:`json_array_unpack`
|
46 | 46 |
|
| 47 | + * - :eql:func:`json_object_pack` |
| 48 | + - :eql:func-desc:`json_object_pack` |
| 49 | + |
47 | 50 | * - :eql:func:`json_object_unpack`
|
48 | 51 | - :eql:func-desc:`json_object_unpack`
|
49 | 52 |
|
@@ -76,6 +79,17 @@ Any value in EdgeDB can be cast to a :eql:type:`json` type as well:
|
76 | 79 | db> select <json>cal::to_local_date(datetime_current(), 'UTC');
|
77 | 80 | {Json("\"2022-11-21\"")}
|
78 | 81 |
|
| 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 | +
|
79 | 93 | Additionally, any :eql:type:`Object` in EdgeDB can be cast as a
|
80 | 94 | :eql:type:`json` type. This produces the same JSON value as the
|
81 | 95 | 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
|
457 | 471 | ----------
|
458 | 472 |
|
459 | 473 |
|
| 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 | + |
460 | 505 | .. eql:function:: std::json_object_unpack(json: json) -> \
|
461 | 506 | set of tuple<str, json>
|
462 | 507 |
|
|
0 commit comments