Skip to content

Commit 27d5b71

Browse files
authored
DOC-8064-C2 -- JSON Results (post-feedback) (#488)
https://issues.couchbase.com/browse/DOC-8064
1 parent 512b10f commit 27d5b71

File tree

8 files changed

+32
-31
lines changed

8 files changed

+32
-31
lines changed

modules/ROOT/pages/_partials/commons/common-query-resultsets.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ To retrieve the results of your query, you need to execute it using `Query.exec
102102
The output from the execution is an array, with each array element representing the data from a document that matched your search criteria.
103103

104104
To unpack the results you need to iterate through this array.
105+
Alternatively, you can convert the result to a JSON string -- see:
105106

106107

107108
[#lbl-acc-all]
Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
11
// Inclusion block
22
[#ex-json]
33
.Using JSON Results
4-
5-
.Caption
6-
[#label]
74
:param-tags: query-access-json
85
:param-leader: pass:q,a[Use {url-api-method-result-toJSON} to transform your result string into a JSON string, which can easily be serialized or used as required in your application. See <<ex-json>> for a working example.]
96
include::{root-partials}_block_tabbed_code_example.adoc[]
107
:param-tags!:
11-
<.> Get the Query result as a JSON string -- this example assumes your query SELECTed specific properties.
12-
<.> Get the Query result as a JSON string -- this example assumes your query SELECTed ALL
8+
<.> Get the Query result as a JSON string -- see <<ex-json-format>>
139
<.> Get a native object from the JSON string
1410
<.> Populate your custom object from the dictionary created from JSON data
1511

16-
// end inclusion block
12+
.JSON String Format
13+
[#ex-json-format]
14+
If your query selects ALL then the JSON format will be:
15+
16+
[source, JSON]
17+
----
18+
{
19+
database-name: {
20+
key1: "value1",
21+
keyx: "valuex"
22+
}
23+
}
24+
----
25+
26+
If your query selects a sub-set of available properties then the JSON format will be:
27+
28+
[source, JSON]
29+
----
30+
{
31+
key1: "value1",
32+
keyx: "valuex"
33+
}
34+
----

modules/android/examples/snippets/app/src/main/java/com/couchbase/code_snippets/Examples.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2642,10 +2642,6 @@ public void testQuerySyntaxAll() throws CouchbaseLiteException {
26422642

26432643
// Get result as JSON string
26442644
String thisJsonString = result.toJSON(); // <.>
2645-
// ALTERNATIVELY
2646-
String thisJsonString =
2647-
result.getDictionary(0).toJSON(); // <.>
2648-
26492645

26502646
// Get Java Hashmap from JSON string
26512647
HashMap<String, Object> dictFromJSONstring =

modules/android/examples/snippets/app/src/main/kotlin/com/couchbase/code_snippets/Examples.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2493,9 +2493,6 @@ class ExamplesP2p(private val context: Context) {
24932493

24942494
// Get result as JSON string
24952495
val thisJsonString1: String = result.toJSON() // <.>
2496-
// ALTERNATIVELY
2497-
val thisJsonString = result.getDictionary(0)?.toJSON() // <.>
2498-
24992496

25002497
// Get Hashmap from JSON string
25012498
val dictFromJSONstring =

modules/csharp/examples/code_snippets/Program.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,9 +1717,7 @@ public static void testQuerySyntaxAll()
17171717
foreach (var result in query.Execute().AsEnumerable()) {
17181718

17191719
// get the result into a JSON String
1720-
var thisDocsJSONString = result.GetDictionary(dbName).ToJSON();// <.>
1721-
// ALTERNATIVELY
1722-
thisDocsJSONString = result.GetDictionary(0).ToJSON();// <.>
1720+
var thisDocsJSONString = result.ToJSON();// <.>
17231721

17241722
// Get a native dictionary object using the JSON string
17251723
var dictFromJSONstring =

modules/java/examples/code_snippets/Examples.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2980,12 +2980,8 @@ public void testQuerySyntaxAll() throws CouchbaseLiteException {
29802980

29812981
// Get result as JSON string
29822982
String thisJsonString = result.toJSON(); // <.>
2983-
// ALTERNATIVELY
2984-
String thisJsonString =
2985-
result.getDictionary(0).toJSON(); // <.>
29862983

2987-
2988-
// Get Java Hashmap from JSON string
2984+
// Get Java Hashmap from JSON string
29892985
HashMap<String, Object> dictFromJSONstring =
29902986
mapper.readValue(thisJsonString, HashMap.class); // <.>
29912987

modules/objc/examples/code_snippets/SampleCodeTest.m

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,9 +1642,6 @@ - (void) dontTestQuerySyntaxJson {
16421642

16431643
NSString* thisJsonString =
16441644
[result toJSON]; // <.>
1645-
// ALTERNATIVELY
1646-
NSString* thisJsonString =
1647-
[[result valueAtIndex:0 ] toJSON]; // <.>
16481645

16491646
// Get an native Obj-C object from the Json String
16501647
NSDictionary *thisDictFromJSON =

modules/swift/examples/code_snippets/SampleCodeTest.swift

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,7 @@ class SampleCodeTest {
704704
}
705705

706706

707+
707708
func dontTestToJson-Blob() throws {
708709
database = self.db
709710
// demonstrate use of JSON string
@@ -2275,11 +2276,8 @@ class Query {
22752276
var results = try! listQuery.execute()
22762277
for row in results {
22772278

2278-
// If Query selected ALL,
2279-
// unpack items from encompassing dictionary // <.>
2280-
let jsonString = row.dictionary(at: 0)!.toJSON()
2281-
// ALTERNATIVELY: If Query selected specific items
2282-
let jsonString = row.toJSON()
2279+
// get the result into a JSON String
2280+
let jsonString = row.toJSON() // <.>
22832281

22842282
let thisJsonObj:Dictionary =
22852283
try! (JSONSerialization.jsonObject(
@@ -2288,13 +2286,13 @@ class Query {
22882286
as? [String: Any])! // <.>
22892287

22902288
// Use Json Object to populate Native object
2291-
// Use Codable class to unpack JSON data to native object // <.>
2289+
// Use Codable class to unpack JSON data to native object
22922290
let this_hotel:Hotel =
22932291
(try JSONDecoder().decode(
22942292
Hotel.self,
22952293
from: jsonString.data(using: .utf8)!
22962294
)
2297-
)
2295+
) // <.>
22982296

22992297
// ALTERNATIVELY unpack in steps
23002298
this_hotel.id = thisJsonObj["id"] as! String

0 commit comments

Comments
 (0)