> For the complete documentation index, see [llms.txt](https://docs.postgresql.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.postgresql.tw/19/the-sql-language/functions/functions-json/sqljson-query-functions.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# 9.16.3. SQL/JSON 查詢函式

## 9.16.3. SQL/JSON 查詢函式 [#](#sqljson-query-functions) <a href="#sqljson-query-functions" id="sqljson-query-functions"></a>

[表 9.54](#functions-sqljson-querying) 所述的 SQL/JSON 函式 `JSON_EXISTS()`、`JSON_QUERY()` 與 `JSON_VALUE()` 可用來查詢 JSON 文件。這些函式都會將 *`path_expression`*（SQL/JSON 路徑查詢）套用到 *`context_item`*（文件）上。關於 *`path_expression`* 可以包含哪些內容的詳細資訊，請參閱[第 9.16.2 節](https://docs.postgresql.tw/19/the-sql-language/functions/functions-json/functions-sqljson-path.md#functions-sqljson-path)。*`path_expression`* 也可以參照變數，變數的值是透過每個函式都支援的 `PASSING` 子句，以其各自的名稱來指定。*`context_item`* 可以是 `jsonb` 值，或是可以成功轉換為 `jsonb` 的字元字串。

### 表 9.54. SQL/JSON 查詢函式 [#](#functions-sqljson-querying) <a href="#functions-sqljson-querying" id="functions-sqljson-querying"></a>

<table data-header-hidden><thead><tr><th></th></tr></thead><tbody><tr><td><p>函式簽章</p><p>說明</p><p>範例</p></td></tr><tr><td><pre><code>JSON_EXISTS (
context_item, path_expression
[ PASSING { value AS varname } [, ...]]
[{ TRUE | FALSE | UNKNOWN | ERROR } ON ERROR ]) → boolean
</code></pre><ul><li>如果 SQL/JSON <em><code>path_expression</code></em> 套用到 <em><code>context_item</code></em> 時產生任何項目，就回傳 true，否則回傳 false。</li><li><code>ON ERROR</code> 子句指定在 <em><code>path_expression</code></em> 求值期間發生錯誤時的行為。指定 <code>ERROR</code> 會拋出帶有適當訊息的錯誤。其他選項包括回傳 <code>boolean</code> 值 <code>FALSE</code> 或 <code>TRUE</code>，或是值 <code>UNKNOWN</code>（實際上就是 SQL NULL）。沒有指定 <code>ON ERROR</code> 子句時，預設是回傳 <code>boolean</code> 值 <code>FALSE</code>。</li></ul><p>範例：</p><p><code>JSON_EXISTS(jsonb '{"key1": [1,2,3]}', 'strict $.key1[*] ? (@ > $x)' PASSING 2 AS x)</code> → <code>t</code></p><p><code>JSON_EXISTS(jsonb '{"a": [1,2,3]}', 'lax $.a[5]' ERROR ON ERROR)</code> → <code>f</code></p><p><code>JSON_EXISTS(jsonb '{"a": [1,2,3]}', 'strict $.a[5]' ERROR ON ERROR)</code> →</p><pre><code>ERROR:  jsonpath array subscript is out of bounds
</code></pre></td></tr><tr><td><pre><code>JSON_QUERY (
context_item, path_expression
[ PASSING { value AS varname } [, ...]]
[ RETURNING data_type [ FORMAT JSON [ ENCODING UTF8 ] ] ]
[ { WITHOUT | WITH { CONDITIONAL | [UNCONDITIONAL] } } [ ARRAY ] WRAPPER ]
[ { KEEP | OMIT } QUOTES [ ON SCALAR STRING ] ]
[ { ERROR | NULL | EMPTY { [ ARRAY ] | OBJECT } | DEFAULT expression } ON EMPTY ]
[ { ERROR | NULL | EMPTY { [ ARRAY ] | OBJECT } | DEFAULT expression } ON ERROR ]) → jsonb
</code></pre><ul><li>回傳將 SQL/JSON <em><code>path_expression</code></em> 套用到 <em><code>context_item</code></em> 的結果。</li><li>預設情況下，結果會以 <code>jsonb</code> 型別的值回傳，不過可以使用 <code>RETURNING</code> 子句，以其他可以成功強制轉換的型別回傳。</li><li>如果路徑運算式可能回傳多個值，可能就需要使用 <code>WITH WRAPPER</code> 子句將這些值包裝起來，使其成為有效的 JSON 字串，因為預設行為是不包裝它們，就如同指定了 <code>WITHOUT WRAPPER</code> 一樣。<code>WITH WRAPPER</code> 子句預設被視為 <code>WITH UNCONDITIONAL WRAPPER</code>，這表示即使只有單一結果值也會被包裝。若只想在有多個值時才套用包裝，請指定 <code>WITH CONDITIONAL WRAPPER</code>。如果指定了 <code>WITHOUT WRAPPER</code>，結果中出現多個值會被視為錯誤。</li><li>如果結果是純量字串，預設情況下，回傳的值會以引號括起來，使其成為有效的 JSON 值。可以透過指定 <code>KEEP QUOTES</code> 來明確表示這一點。相反地，可以透過指定 <code>OMIT QUOTES</code> 來省略引號。為了確保結果是有效的 JSON 值，<code>OMIT QUOTES</code> 不能與 <code>WITH WRAPPER</code> 同時指定。</li><li><code>ON EMPTY</code> 子句指定當 <em><code>path_expression</code></em> 的求值產生空集合時的行為。<code>ON ERROR</code> 子句則指定在下列情況發生錯誤時的行為：對 <em><code>path_expression</code></em> 求值時、將結果值強制轉換為 <code>RETURNING</code> 型別時，或是對 <code>ON EMPTY</code> 運算式求值時（此時 <em><code>path_expression</code></em> 的求值回傳了空集合）。</li><li>對於 <code>ON EMPTY</code> 與 <code>ON ERROR</code> 兩者，指定 <code>ERROR</code> 都會拋出帶有適當訊息的錯誤。其他選項包括回傳 SQL NULL、空陣列（<code>EMPTY [ARRAY]</code>）、空物件（<code>EMPTY OBJECT</code>），或是使用者指定的運算式（<code>DEFAULT</code> <em><code>expression</code></em>），且該運算式可以強制轉換為 jsonb 或 <code>RETURNING</code> 中所指定的型別。未指定 <code>ON EMPTY</code> 或 <code>ON ERROR</code> 時，預設是回傳 SQL NULL 值。</li></ul><p>範例：</p><p><code>JSON_QUERY(jsonb '[1,[2,3],null]', 'lax $[*][$off]' PASSING 1 AS off WITH CONDITIONAL WRAPPER)</code> → <code>3</code></p><p><code>JSON_QUERY(jsonb '{"a": "[1, 2]"}', 'lax $.a' OMIT QUOTES)</code> → <code>[1, 2]</code></p><p><code>JSON_QUERY(jsonb '{"a": "[1, 2]"}', 'lax $.a' RETURNING int[] OMIT QUOTES ERROR ON ERROR)</code> →</p><pre><code>ERROR:  malformed array literal: "[1, 2]"
DETAIL:  Missing "]" after array dimensions.
</code></pre></td></tr><tr><td><pre><code>JSON_VALUE (
context_item, path_expression
[ PASSING { value AS varname } [, ...]]
[ RETURNING data_type ]
[ { ERROR | NULL | DEFAULT expression } ON EMPTY ]
[ { ERROR | NULL | DEFAULT expression } ON ERROR ]) → text
</code></pre><ul><li>回傳將 SQL/JSON <em><code>path_expression</code></em> 套用到 <em><code>context_item</code></em> 的結果。</li><li>只有在預期擷取出的值是單一的 SQL/JSON 純量項目時，才使用 <code>JSON_VALUE()</code>；取得多個值會被視為錯誤。如果你預期擷取出的值可能是物件或陣列，請改用 <code>JSON_QUERY</code> 函式。</li><li>預設情況下，結果（必須是單一純量值）會以 <code>text</code> 型別的值回傳，不過可以使用 <code>RETURNING</code> 子句，以其他可以成功強制轉換的型別回傳。</li><li><code>ON ERROR</code> 與 <code>ON EMPTY</code> 子句的語意與 <code>JSON_QUERY</code> 的說明中所述者類似，差別在於用來取代拋出錯誤而回傳的值的集合不同。</li><li>請注意，<code>JSON_VALUE</code> 回傳的純量字串一律會去除其引號，等同於指定了 <code>OMIT QUOTES</code> 的 <code>JSON_QUERY</code>。</li></ul><p>範例：</p><p><code>JSON_VALUE(jsonb '"123.45"', '$' RETURNING float)</code> → <code>123.45</code></p><p><code>JSON_VALUE(jsonb '"03:04 2015-02-01"', '$.datetime("HH24:MI YYYY-MM-DD")' RETURNING date)</code> → <code>2015-02-01</code></p><p><code>JSON_VALUE(jsonb '[1,2]', 'strict $[$off]' PASSING 1 as off)</code> → <code>2</code></p><p><code>JSON_VALUE(jsonb '[1,2]', 'strict $[*]' DEFAULT 9 ON ERROR)</code> → <code>9</code></p></td></tr></tbody></table>

<br>

### 注意 [#](#zhu-yi-2) <a href="#zhu-yi-2" id="zhu-yi-2"></a>

如果 *`context_item`* 運算式的型別還不是 `jsonb`，它會透過隱含的型別轉換轉為 `jsonb`。不過請注意，在該轉換過程中發生的任何剖析錯誤都會無條件拋出，也就是說，不會依照（明確指定或隱含的）`ON ERROR` 子句來處理。

### 注意 [#](#zhu-yi-3) <a href="#zhu-yi-3" id="zhu-yi-3"></a>

如果 *`path_expression`* 回傳 JSON `null`，`JSON_VALUE()` 會回傳 SQL NULL，而 `JSON_QUERY()` 則會原樣回傳 JSON `null`。

[返回 9.16. JSON 函式與運算子](https://docs.postgresql.tw/19/the-sql-language/functions/functions-json.md#functions-json)

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/functions-json.html)（原文版本：18.6；核對日期：2026-09-11）


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.postgresql.tw/19/the-sql-language/functions/functions-json/sqljson-query-functions.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
