Skip to content

Commit

Permalink
Document jsonb_pretty, jsonb_object, and regexp_split_to_array (#1435)
Browse files Browse the repository at this point in the history
* Document jsonb_pretty, jsonb_object, and regexp_split_to_array

* jsonb object -> jsonb value

* jsonb object -> jsonb value

* fix

* formatting updates

* Update sql-function-json.md

---------

Co-authored-by: hengm3467 <100685635+hengm3467@users.noreply.github.com>
  • Loading branch information
neverchanje and hengm3467 authored Oct 30, 2023
1 parent 1b68f04 commit ef22b94
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
45 changes: 37 additions & 8 deletions docs/sql/functions-operators/sql-function-json.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,39 @@ number

```

### `jsonb_pretty`

This function takes a `jsonb` value and returns a text representing the formatted, indented JSON value.

```sql title=Syntax
jsonb_pretty ( jsonb JSONB ) → TEXT
```

```sql title=Examples
SELECT jsonb_pretty('[{"f1":1,"f2":null}, 2]');
------RESULT
[
{
"f1": 1,
"f2": null
},
2
]
```

### `jsonb_object`

This function takes an array of text elements and returns a `jsonb` object where adjacent pairs of values are taken as the key and value of an object property.

```sql title=Syntax
jsonb_object ( text_array TEXT[] ) → JSONB
```

```sql title=Examples
jsonb_object('{a, 1, b, def, c, 3.5}' :: text[]) → {"a": "1", "b": "def", "c": "3.5"}
jsonb_object(array['a', null]) → {"a": null}
```

## JSON operators

### `jsonb -> integer`
Expand Down Expand Up @@ -199,17 +232,13 @@ SELECT '{"a": "b"}'::jsonb || '42'::jsonb;

This predicate tests whether an expression can be parsed as JSON, optionally of a specified type. It evaluates the JSON structure and returns a boolean result indicating whether the value matches the specified JSON type.

#### Syntax

```sql
```sql title=Syntax
expression IS [ NOT ] JSON [ VALUE | ARRAY | OBJECT | SCALAR ] → bool
```

If SCALAR, ARRAY, or OBJECT is specified, the test is whether or not the JSON is of that particular type.

#### Example

```sql
```sql title=Example
SELECT js,
js IS JSON "json?",
js IS JSON SCALAR "scalar?",
Expand All @@ -218,8 +247,8 @@ SELECT js,
FROM (VALUES
('123'), ('"abc"'), ('{"a": "b"}'), ('[1,2]'),('abc')) foo(js);
```
```
------RESULT

```markdown title=Result

js | json? | scalar? | object? | array?
------------+-------+---------+---------+---------
Expand Down
16 changes: 16 additions & 0 deletions docs/sql/functions-operators/sql-function-string.md
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,22 @@ regexp_replace('RisingWave', '[aeiou]', 'X', 1, 3, 'i') → RisingWXve
---
### `regexp_split_to_array`
This function splits a string into an array of substrings based on a regular expression pattern.
```sql title=Syntax
regexp_split_to_array ( input_string TEXT, pattern TEXT ) → TEXT[]
```
```sql title=Examples
regexp_split_to_array('apple,banana,orange', ',') → {apple,banana,orange}
regexp_split_to_array('apple.banana!orange', '[.!]') → {apple,banana,orange}
regexp_split_to_array('applebananaorange', ',') → {applebananaorange}
```
---
### `repeat`
Repeats *input_string* specific times. Null is returned when *times_int* is zero, negative, or null.
Expand Down

0 comments on commit ef22b94

Please sign in to comment.