You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `JSON_VALUE` syntax extracts a scalar value from a JSON string.
28
+
Use the `JSON_VALUE` syntax to extract a scalar value from a JSON string.
29
29
30
30
To extract an object or an array from a JSON string instead of a scalar value, see [JSON_QUERY](json-query-transact-sql.md). For info about the differences between `JSON_VALUE` and `JSON_QUERY`, see [Compare JSON_VALUE and JSON_QUERY](../../relational-databases/json/validate-query-and-change-json-data-with-built-in-functions-sql-server.md#JSONCompare).
> In [!INCLUDE [sssql22-md](../../includes/sssql22-md.md)] and earlier versions, [RETURNING isn't included](?view=sql-server-ver16&preserve-view=true#syntax).
56
+
57
+
::: moniker-end
58
+
40
59
## Arguments
41
60
42
61
#### *expression*
43
62
44
-
An expression. Typically the name of a variable or a column that contains JSON text.
63
+
An expression that's typically the name of a variable or a column containing JSON text.
45
64
46
65
If `JSON_VALUE` finds JSON that isn't valid in *expression* before it finds the value identified by *path*, the function returns an error. If `JSON_VALUE` doesn't find the value identified by *path*, it scans the entire text and returns an error if it finds JSON that isn't valid anywhere in *expression*.
47
66
48
67
#### *path*
49
68
50
-
A JSON path that specifies the property to extract. For more info, see [JSON Path Expressions in the SQL Database Engine](../../relational-databases/json/json-path-expressions-sql-server.md).
69
+
`A JSON` path that specifies the property to extract. For more info, see [JSON path expressions in the SQL Database Engine](../../relational-databases/json/json-path-expressions-sql-server.md).
51
70
52
71
In [!INCLUDE [sssql17-md](../../includes/sssql17-md.md)] and in [!INCLUDE [ssazure-sqldb](../../includes/ssazure-sqldb.md)], you can provide a variable as the value of *path*.
53
72
54
73
If the format of *path* isn't valid, `JSON_VALUE` returns an error.
55
74
56
75
#### *data_type*
57
76
58
-
Return the value specified in the SQL type. Only supported if the input is a JSON type. The supported SQL types are: **tinyint**, **smallint**, **int**, **bigint**, **decimal**, **numeric**, **float**, **real**, **char**, **varchar**, **varchar(max)**, **nchar**, **nvarchar**, **nvarchar(max)**, **date**, **time**, **datetime2**, and **datetimeoffset**.
77
+
The data type that you want to use for the return value. This type is only supported if the input is a JSON type. The supported data types are: **tinyint**, **smallint**, **int**, **bigint**, **decimal**, **numeric**, **float**, **real**, **char**, **varchar**, **varchar(max)**, **nchar**, **nvarchar**, **nvarchar(max)**, **date**, **time**, **datetime2**, and **datetimeoffset**.
59
78
60
79
## Return value
61
80
62
-
If `RETURNING` isn't included:
81
+
**Applies to**: [!INCLUDE [sssql25-md](../../includes/sssql25-md.md)] and later versions.
82
+
83
+
If you don't include `RETURNING`:
63
84
64
-
- Returns a single text value of type nvarchar(4000). The collation of the returned value is the same as the collation of the input expression.
85
+
- Returns a single text value of type **nvarchar(4000)**. The collation of the returned value matches the collation of the input expression.
65
86
66
-
- If the value is greater than 4000 characters:
87
+
- If the value is greater than 4,000 characters:
67
88
68
89
- In lax mode, `JSON_VALUE` returns `NULL`.
69
90
- In strict mode, `JSON_VALUE` returns an error.
70
91
71
-
If you have to return scalar values greater than 4000 characters, use `OPENJSON` instead of `JSON_VALUE`. For more info, see [OPENJSON](openjson-transact-sql.md).
92
+
If you need to return scalar values greater than 4,000 characters, use `OPENJSON` instead of `JSON_VALUE`. For more information, see [OPENJSON](openjson-transact-sql.md).
72
93
73
-
If `RETURNING` is included:
94
+
If you include `RETURNING`:
74
95
75
-
Returns the value specified in the SQL type. The supported SQL types are: **tinyint**, **smallint**, **int**, **bigint**, **decimal**, **numeric**, **float**, **real**, **char**, **varchar**, **varchar(max)**, **nchar**, **nvarchar**, **nvarchar(max)**, **date**, **time**, **datetime2**, and **datetimeoffset**.
96
+
Returns the value specified in *data_type*. The supported data types are: **tinyint**, **smallint**, **int**, **bigint**, **decimal**, **numeric**, **float**, **real**, **char**, **varchar**, **varchar(max)**, **nchar**, **nvarchar**, **nvarchar(max)**, **date**, **time**, **datetime2**, and **datetimeoffset**.
76
97
77
-
JSON functions work the same whether the JSON document is stored in **varchar**, **nvarchar**, or the native **json** data type.
98
+
JSON functions work the same way whether the JSON document is stored in **varchar**, **nvarchar**, or the native **json** data type.
78
99
79
100
## Remarks
80
101
@@ -99,7 +120,7 @@ SET @jsonInfo = N'{
99
120
}';
100
121
```
101
122
102
-
The following table compares the behavior of `JSON_VALUE` in lax mode and in strict mode. For more info about the optional path mode specification (lax or strict), see [JSON Path Expressions in the SQL Database Engine](../../relational-databases/json/json-path-expressions-sql-server.md).
123
+
The following table compares the behavior of `JSON_VALUE` in lax mode and in strict mode. For more info about the optional path mode specification (lax or strict), see [JSON path expressions in the SQL Database Engine](../../relational-databases/json/json-path-expressions-sql-server.md).
103
124
104
125
| Path | Return value in lax mode | Return value in strict mode | More info |
105
126
| --- | --- | --- | --- |
@@ -118,7 +139,7 @@ The following table compares the behavior of `JSON_VALUE` in lax mode and in str
118
139
The following example uses the values of the JSON properties `town` and `state` in query results. Since `JSON_VALUE` preserves the collation of the source, the sort order of the results depends on the collation of the `jsonInfo` column.
119
140
120
141
> [!NOTE]
121
-
> (This example assumes that a table named `Person.Person` contains a `jsonInfo` column of JSON text, and that this column has the structure shown previously in the discussion of lax mode and strict mode. In the `AdventureWorks` sample database, the `Person` table doesn't in fact contain a `jsonInfo` column.)
142
+
> This example assumes that a table named `Person.Person` contains a `jsonInfo` column of JSON text, and that this column has the structure shown previously in the discussion of lax mode and strict mode. In the `AdventureWorks` sample database, the `Person` table doesn't contain a `jsonInfo` column.
122
143
123
144
```sql
124
145
SELECT FirstName,
@@ -175,5 +196,5 @@ date_value
175
196
176
197
## Related content
177
198
178
-
-[JSON Path Expressions in the SQL Database Engine](../../relational-databases/json/json-path-expressions-sql-server.md)
199
+
-[JSON path expressions in the SQL Database Engine](../../relational-databases/json/json-path-expressions-sql-server.md)
179
200
-[JSON data in SQL Server](../../relational-databases/json/json-data-sql-server.md)
0 commit comments