Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
---
{
"title": "JSON_EACH_OUTER",
"language": "en",
"description": "Expands the top-level JSON object into a set of key/value pairs, where the value column retains JSON type. Unlike json_each, returns one row of NULLs instead of 0 rows when input is NULL or an empty object. Must be used with LATERAL VIEW."
}
---

## Description

The `json_each_outer` table function expands the top-level JSON object into a set of key/value pairs. Each row contains one key (`key`) and its corresponding value (`value`). The `value` column retains the JSON type, so string values are returned with JSON quotes preserved.

Unlike [`json_each`](json-each.md), when the input is NULL or an empty object, `json_each_outer` returns one row of `NULL, NULL` instead of 0 rows.

Must be used with [`LATERAL VIEW`](../../../query-data/lateral-view.md).

## Syntax

```sql
JSON_EACH_OUTER(<json_str>)
```

## Parameters

| Parameter | Description |
| ------------ | ------------------------------------------------------------- |
| `<json_str>` | The JSON string to expand. The content must be a JSON object. |

## Return Value

Returns multi-column, multi-row data. Each row corresponds to one key-value pair in the JSON object:

| Column | Type | Description |
| ------- | ------ | --------------------------------------------------------------------------------------- |
| `key` | String | The key name from the JSON object |
| `value` | JSON | The corresponding value, kept as JSON type (string values include quotes, e.g. `"foo"`) |

Special cases:
- If `<json_str>` is NULL, returns 1 row of `NULL, NULL`
- If `<json_str>` is an empty object (`{}`), returns 1 row of `NULL, NULL`

## Examples

Basic usage: expand a JSON object with string values

```sql
SELECT k, v
FROM (SELECT 1) dummy
LATERAL VIEW json_each_outer('{"a":"foo","b":"bar"}') t AS k, v;
```

```text
+---+-------+
| k | v |
+---+-------+
| a | "foo" |
| b | "bar" |
+---+-------+
```

NULL parameter: returns 1 row of NULL

```sql
SELECT k, v
FROM (SELECT 1) dummy
LATERAL VIEW json_each_outer(NULL) t AS k, v;
```

```text
+------+------+
| k | v |
+------+------+
| NULL | NULL |
+------+------+
```

Empty object: returns 1 row of NULL

```sql
SELECT k, v
FROM (SELECT 1) dummy
LATERAL VIEW json_each_outer('{}') t AS k, v;
```

```text
+------+------+
| k | v |
+------+------+
| NULL | NULL |
+------+------+
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
---
{
"title": "JSON_EACH_TEXT_OUTER",
"language": "en",
"description": "Expands the top-level JSON object into a set of key/value pairs, where the value column is returned as plain text. Unlike json_each_text, returns one row of NULLs instead of 0 rows when input is NULL or an empty object. Must be used with LATERAL VIEW."
}
---

## Description

The `json_each_text_outer` table function expands the top-level JSON object into a set of key/value pairs. Each row contains one key (`key`) and its corresponding value (`value`). Unlike [`json_each_outer`](json-each-outer.md), the `value` column is of type TEXT, so string values are returned **without** JSON quotes.

Unlike [`json_each_text`](json-each-text.md), when the input is NULL or an empty object, `json_each_text_outer` returns one row of `NULL, NULL` instead of 0 rows.

Must be used with [`LATERAL VIEW`](../../../query-data/lateral-view.md).

## Syntax

```sql
JSON_EACH_TEXT_OUTER(<json_str>)
```

## Parameters

| Parameter | Description |
| ------------ | ------------------------------------------------------------- |
| `<json_str>` | The JSON string to expand. The content must be a JSON object. |

## Return Value

Returns multi-column, multi-row data. Each row corresponds to one key-value pair in the JSON object:

| Column | Type | Description |
| ------- | ---- | -------------------------------------------------------------------------------- |
| `key` | TEXT | The key name from the JSON object |
| `value` | TEXT | The corresponding value as plain text (string values have no quotes, e.g. `foo`) |

Special cases:
- If `<json_str>` is NULL, returns 1 row of `NULL, NULL`
- If `<json_str>` is an empty object (`{}`), returns 1 row of `NULL, NULL`
- A JSON `null` value is returned as SQL `NULL`

## Examples

Basic usage: expand a JSON object with string values

```sql
SELECT k, v
FROM (SELECT 1) dummy
LATERAL VIEW json_each_text_outer('{"a":"foo","b":"bar"}') t AS k, v;
```

```text
+---+-----+
| k | v |
+---+-----+
| a | foo |
| b | bar |
+---+-----+
```

> The `value` column is of TEXT type, so string values have **no** JSON quotes (unlike `json_each_outer`).

NULL parameter: returns 1 row of NULL

```sql
SELECT k, v
FROM (SELECT 1) dummy
LATERAL VIEW json_each_text_outer(NULL) t AS k, v;
```

```text
+------+------+
| k | v |
+------+------+
| NULL | NULL |
+------+------+
```

Empty object: returns 1 row of NULL

```sql
SELECT k, v
FROM (SELECT 1) dummy
LATERAL VIEW json_each_text_outer('{}') t AS k, v;
```

```text
+------+------+
| k | v |
+------+------+
| NULL | NULL |
+------+------+
```
99 changes: 99 additions & 0 deletions docs/sql-manual/sql-functions/table-functions/json-each-text.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
---
{
"title": "JSON_EACH_TEXT",
"language": "en",
"description": "Expands the top-level JSON object into a set of key/value pairs, where the value column is returned as plain text. Must be used with LATERAL VIEW."
}
---

## Description

The `json_each_text` table function expands the top-level JSON object into a set of key/value pairs. Each row contains one key (`key`) and its corresponding value (`value`). Unlike [`json_each`](json-each.md), the `value` column is of type TEXT, so string values are returned **without** JSON quotes.

Must be used with [`LATERAL VIEW`](../../../query-data/lateral-view.md).

## Syntax

```sql
JSON_EACH_TEXT(<json_str>)
```

## Parameters

| Parameter | Description |
| ------------ | ---------------------------------------------------- |
| `<json_str>` | The JSON string to expand. The content must be a JSON object. |

## Return Value

Returns multi-column, multi-row data. Each row corresponds to one key-value pair in the JSON object:

| Column | Type | Description |
|---------|------|--------------------------------------------------------------------------------------|
| `key` | TEXT | The key name from the JSON object |
| `value` | TEXT | The corresponding value as plain text (string values have no quotes, e.g. `foo`) |

Special cases:
- If `<json_str>` is NULL, returns 0 rows
- If `<json_str>` is an empty object (`{}`), returns 0 rows
- A JSON `null` value is returned as SQL `NULL`

## Examples

Basic usage: expand a JSON object with string values

```sql
SELECT k, v
FROM (SELECT 1) dummy
LATERAL VIEW json_each_text('{"a":"foo","b":"bar"}') t AS k, v;
```

```text
+---+-----+
| k | v |
+---+-----+
| a | foo |
| b | bar |
+---+-----+
```

> The `value` column is of TEXT type, so string values have **no** JSON quotes (unlike `json_each`).

JSON object with multiple value types

```sql
SELECT k, v
FROM (SELECT 1) dummy
LATERAL VIEW json_each_text('{"str":"hello","num":42,"bool":true,"null_val":null}') t AS k, v;
```

```text
+----------+-------+
| k | v |
+----------+-------+
| str | hello |
| num | 42 |
| bool | true |
| null_val | NULL |
+----------+-------+
```

> JSON `null` values map to SQL `NULL`.

NULL parameter: returns 0 rows

```sql
SELECT k, v
FROM (SELECT 1) dummy
LATERAL VIEW json_each_text(NULL) t AS k, v;
-- Empty set
```

Empty object: returns 0 rows

```sql
SELECT k, v
FROM (SELECT 1) dummy
LATERAL VIEW json_each_text('{}') t AS k, v;
-- Empty set
```
97 changes: 97 additions & 0 deletions docs/sql-manual/sql-functions/table-functions/json-each.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
---
{
"title": "JSON_EACH",
"language": "en",
"description": "Expands the top-level JSON object into a set of key/value pairs, where the value column retains JSON type. Must be used with LATERAL VIEW."
}
---

## Description

The `json_each` table function expands the top-level JSON object into a set of key/value pairs. Each row contains one key (`key`) and its corresponding value (`value`). The `value` column retains the JSON type, so string values are returned with JSON quotes preserved.

Must be used with [`LATERAL VIEW`](../../../query-data/lateral-view.md).

## Syntax

```sql
JSON_EACH(<json_str>)
```

## Parameters

| Parameter | Description |
| ------------ | ---------------------------------------------------- |
| `<json_str>` | The JSON string to expand. The content must be a JSON object. |

## Return Value

Returns multi-column, multi-row data. Each row corresponds to one key-value pair in the JSON object:

| Column | Type | Description |
|---------|--------|-----------------------------------------------------------------------------|
| `key` | String | The key name from the JSON object |
| `value` | JSON | The corresponding value, kept as JSON type (string values include quotes, e.g. `"foo"`) |

Special cases:
- If `<json_str>` is NULL, returns 0 rows
- If `<json_str>` is an empty object (`{}`), returns 0 rows

## Examples

Basic usage: expand a JSON object with string values

```sql
SELECT k, v
FROM (SELECT 1) dummy
LATERAL VIEW json_each('{"a":"foo","b":"bar"}') t AS k, v;
```

```text
+---+-------+
| k | v |
+---+-------+
| a | "foo" |
| b | "bar" |
+---+-------+
```

> The `value` column is of JSON type, so string values retain their JSON quotes.

JSON object with multiple value types

```sql
SELECT k, v
FROM (SELECT 1) dummy
LATERAL VIEW json_each('{"str":"hello","num":42,"bool":true,"null_val":null,"arr":[1,2]}') t AS k, v;
```

```text
+----------+---------+
| k | v |
+----------+---------+
| str | "hello" |
| num | 42 |
| bool | true |
| null_val | NULL |
| arr | [1,2] |
+----------+---------+
```

NULL parameter: returns 0 rows

```sql
SELECT k, v
FROM (SELECT 1) dummy
LATERAL VIEW json_each(NULL) t AS k, v;
-- Empty set
```

Empty object: returns 0 rows

```sql
SELECT k, v
FROM (SELECT 1) dummy
LATERAL VIEW json_each('{}') t AS k, v;
-- Empty set
```
Loading