Skip to content

Commit e552e9b

Browse files
committed
add doc for working with JSON
1 parent e7c3292 commit e552e9b

File tree

4 files changed

+283
-172
lines changed

4 files changed

+283
-172
lines changed

articles/cosmos-db/TOC.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,15 @@
168168
- name: OFFSET LIMIT
169169
displayName: offset limit
170170
href: sql-query-offset-limit.md
171+
- name: Working with JSON
172+
displayName: alias, JSON, projection
173+
href: sql-query-working-with-json.md
171174
- name: Subquery
172175
displayName: subquery, exists
173176
href: sql-query-subquery.md
174177
- name: Joins
175178
displayName: join
176179
href: sql-query-join.md
177-
- name: Aliasing
178-
displayName: alias, aliasing
179-
href: sql-query-aliasing.md
180180
- name: Arrays and objects
181181
displayName: object and array creation, object and array
182182
href: sql-query-object-array.md

articles/cosmos-db/sql-query-aliasing.md

Lines changed: 0 additions & 45 deletions
This file was deleted.

articles/cosmos-db/sql-query-select.md

Lines changed: 8 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
22
title: SELECT clause in Azure Cosmos DB
33
description: Learn about SQL SELECT clause for Azure Cosmos DB. Use SQL as an Azure Cosmos DB JSON query language.
4-
author: ginarobinson
4+
author: timsander1
55
ms.service: cosmos-db
66
ms.topic: conceptual
7-
ms.date: 06/10/2019
8-
ms.author: girobins
7+
ms.date: 05/08/2020
8+
ms.author: tisande
99
---
1010

1111
# SELECT clause in Azure Cosmos DB
@@ -17,12 +17,12 @@ Every query consists of a SELECT clause and optional [FROM](sql-query-from.md) a
1717
```sql
1818
SELECT <select_specification>
1919

20-
<select_specification> ::=
21-
'*'
22-
| [DISTINCT] <object_property_list>
20+
<select_specification> ::=
21+
'*'
22+
| [DISTINCT] <object_property_list>
2323
| [DISTINCT] VALUE <scalar_expression> [[ AS ] value_alias]
2424

25-
<object_property_list> ::=
25+
<object_property_list> ::=
2626
{ <scalar_expression> [ [ AS ] property_alias ] } [ ,...n ]
2727

2828
```
@@ -44,7 +44,7 @@ SELECT <select_specification>
4444
- `VALUE`
4545

4646
Specifies that the JSON value should be retrieved instead of the complete JSON object. This, unlike `<property_list>` does not wrap the projected value in an object.
47-
47+
4848
- `DISTINCT`
4949

5050
Specifies that duplicates of projected properties should be removed.
@@ -93,122 +93,6 @@ The results are:
9393
}]
9494
```
9595

96-
### Quoted property accessor
97-
You can access properties using the quoted property operator []. For example, `SELECT c.grade` and `SELECT c["grade"]` are equivalent. This syntax is useful to escape a property that contains spaces, special characters, or has the same name as a SQL keyword or reserved word.
98-
99-
```sql
100-
SELECT f["lastName"]
101-
FROM Families f
102-
WHERE f["id"] = "AndersenFamily"
103-
```
104-
105-
### Nested properties
106-
107-
The following example projects two nested properties, `f.address.state` and `f.address.city`.
108-
109-
```sql
110-
SELECT f.address.state, f.address.city
111-
FROM Families f
112-
WHERE f.id = "AndersenFamily"
113-
```
114-
115-
The results are:
116-
117-
```json
118-
[{
119-
"state": "WA",
120-
"city": "Seattle"
121-
}]
122-
```
123-
### JSON expressions
124-
125-
Projection also supports JSON expressions, as shown in the following example:
126-
127-
```sql
128-
SELECT { "state": f.address.state, "city": f.address.city, "name": f.id }
129-
FROM Families f
130-
WHERE f.id = "AndersenFamily"
131-
```
132-
133-
The results are:
134-
135-
```json
136-
[{
137-
"$1": {
138-
"state": "WA",
139-
"city": "Seattle",
140-
"name": "AndersenFamily"
141-
}
142-
}]
143-
```
144-
145-
In the preceding example, the SELECT clause needs to create a JSON object, and since the sample provides no key, the clause uses the implicit argument variable name `$1`. The following query returns two implicit argument variables: `$1` and `$2`.
146-
147-
```sql
148-
SELECT { "state": f.address.state, "city": f.address.city },
149-
{ "name": f.id }
150-
FROM Families f
151-
WHERE f.id = "AndersenFamily"
152-
```
153-
154-
The results are:
155-
156-
```json
157-
[{
158-
"$1": {
159-
"state": "WA",
160-
"city": "Seattle"
161-
},
162-
"$2": {
163-
"name": "AndersenFamily"
164-
}
165-
}]
166-
```
167-
## Reserved keywords and special characters
168-
169-
If your data contains properties with the same names as reserved keywords such as "order" or "Group" then the queries against these documents will result in syntax errors. You should explicitly include the property in `[]` character to run the query successfully.
170-
171-
For example, here's a document with a property named `order` and a property `price($)` that contains special characters:
172-
173-
```json
174-
{
175-
"id": "AndersenFamily",
176-
"order": [
177-
{
178-
"orderId": "12345",
179-
"productId": "A17849",
180-
"price($)": 59.33
181-
}
182-
],
183-
"creationDate": 1431620472,
184-
"isRegistered": true
185-
}
186-
```
187-
188-
If you run a queries that includes the `order` property or `price($)` property, you will receive a syntax error.
189-
190-
```sql
191-
SELECT * FROM c where c.order.orderid = "12345"
192-
```
193-
```sql
194-
SELECT * FROM c where c.order.price($) > 50
195-
```
196-
The result is:
197-
198-
`
199-
Syntax error, incorrect syntax near 'order'
200-
`
201-
202-
You should rewrite the same queries as below:
203-
204-
```sql
205-
SELECT * FROM c WHERE c["order"].orderId = "12345"
206-
```
207-
208-
```sql
209-
SELECT * FROM c WHERE c["order"]["price($)"] > 50
210-
```
211-
21296
## Next steps
21397

21498
- [Getting started](sql-query-getting-started.md)

0 commit comments

Comments
 (0)