diff --git a/modules/ROOT/pages/_partials/commons/common-query-resultsets.adoc b/modules/ROOT/pages/_partials/commons/common-query-resultsets.adoc index a8020e96c..e2b1ed292 100644 --- a/modules/ROOT/pages/_partials/commons/common-query-resultsets.adoc +++ b/modules/ROOT/pages/_partials/commons/common-query-resultsets.adoc @@ -102,6 +102,7 @@ To retrieve the results of your query, you need to execute it using `Query.exec The output from the execution is an array, with each array element representing the data from a document that matched your search criteria. To unpack the results you need to iterate through this array. +Alternatively, you can convert the result to a JSON string -- see: [#lbl-acc-all] diff --git a/modules/ROOT/pages/_partials/commons/common-querybuilder-jsonresult.adoc b/modules/ROOT/pages/_partials/commons/common-querybuilder-jsonresult.adoc index f611878a0..f70a51b82 100644 --- a/modules/ROOT/pages/_partials/commons/common-querybuilder-jsonresult.adoc +++ b/modules/ROOT/pages/_partials/commons/common-querybuilder-jsonresult.adoc @@ -1,16 +1,34 @@ // Inclusion block [#ex-json] .Using JSON Results - -.Caption -[#label] :param-tags: query-access-json :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 <> for a working example.] include::{root-partials}_block_tabbed_code_example.adoc[] :param-tags!: -<.> Get the Query result as a JSON string -- this example assumes your query SELECTed specific properties. -<.> Get the Query result as a JSON string -- this example assumes your query SELECTed ALL +<.> Get the Query result as a JSON string -- see <> <.> Get a native object from the JSON string <.> Populate your custom object from the dictionary created from JSON data -// end inclusion block \ No newline at end of file +.JSON String Format +[#ex-json-format] +If your query selects ALL then the JSON format will be: + +[source, JSON] +---- +{ + database-name: { + key1: "value1", + keyx: "valuex" + } +} +---- + +If your query selects a sub-set of available properties then the JSON format will be: + +[source, JSON] +---- +{ + key1: "value1", + keyx: "valuex" +} +---- \ No newline at end of file diff --git a/modules/android/examples/snippets/app/src/main/java/com/couchbase/code_snippets/Examples.java b/modules/android/examples/snippets/app/src/main/java/com/couchbase/code_snippets/Examples.java index 1ee3ea939..df230cff8 100644 --- a/modules/android/examples/snippets/app/src/main/java/com/couchbase/code_snippets/Examples.java +++ b/modules/android/examples/snippets/app/src/main/java/com/couchbase/code_snippets/Examples.java @@ -2642,10 +2642,6 @@ public void testQuerySyntaxAll() throws CouchbaseLiteException { // Get result as JSON string String thisJsonString = result.toJSON(); // <.> - // ALTERNATIVELY - String thisJsonString = - result.getDictionary(0).toJSON(); // <.> - // Get Java Hashmap from JSON string HashMap dictFromJSONstring = diff --git a/modules/android/examples/snippets/app/src/main/kotlin/com/couchbase/code_snippets/Examples.kt b/modules/android/examples/snippets/app/src/main/kotlin/com/couchbase/code_snippets/Examples.kt index 9d5abdcb1..92af38391 100644 --- a/modules/android/examples/snippets/app/src/main/kotlin/com/couchbase/code_snippets/Examples.kt +++ b/modules/android/examples/snippets/app/src/main/kotlin/com/couchbase/code_snippets/Examples.kt @@ -2493,9 +2493,6 @@ class ExamplesP2p(private val context: Context) { // Get result as JSON string val thisJsonString1: String = result.toJSON() // <.> - // ALTERNATIVELY - val thisJsonString = result.getDictionary(0)?.toJSON() // <.> - // Get Hashmap from JSON string val dictFromJSONstring = diff --git a/modules/csharp/examples/code_snippets/Program.cs b/modules/csharp/examples/code_snippets/Program.cs index f83d2d23c..15b9880cd 100644 --- a/modules/csharp/examples/code_snippets/Program.cs +++ b/modules/csharp/examples/code_snippets/Program.cs @@ -1717,9 +1717,7 @@ public static void testQuerySyntaxAll() foreach (var result in query.Execute().AsEnumerable()) { // get the result into a JSON String - var thisDocsJSONString = result.GetDictionary(dbName).ToJSON();// <.> - // ALTERNATIVELY - thisDocsJSONString = result.GetDictionary(0).ToJSON();// <.> + var thisDocsJSONString = result.ToJSON();// <.> // Get a native dictionary object using the JSON string var dictFromJSONstring = diff --git a/modules/java/examples/code_snippets/Examples.java b/modules/java/examples/code_snippets/Examples.java index e9a553269..8a2004851 100644 --- a/modules/java/examples/code_snippets/Examples.java +++ b/modules/java/examples/code_snippets/Examples.java @@ -2980,12 +2980,8 @@ public void testQuerySyntaxAll() throws CouchbaseLiteException { // Get result as JSON string String thisJsonString = result.toJSON(); // <.> - // ALTERNATIVELY - String thisJsonString = - result.getDictionary(0).toJSON(); // <.> - - // Get Java Hashmap from JSON string + // Get Java Hashmap from JSON string HashMap dictFromJSONstring = mapper.readValue(thisJsonString, HashMap.class); // <.> diff --git a/modules/objc/examples/code_snippets/SampleCodeTest.m b/modules/objc/examples/code_snippets/SampleCodeTest.m index b0fcfc04d..ceb196bd2 100644 --- a/modules/objc/examples/code_snippets/SampleCodeTest.m +++ b/modules/objc/examples/code_snippets/SampleCodeTest.m @@ -1642,9 +1642,6 @@ - (void) dontTestQuerySyntaxJson { NSString* thisJsonString = [result toJSON]; // <.> - // ALTERNATIVELY - NSString* thisJsonString = - [[result valueAtIndex:0 ] toJSON]; // <.> // Get an native Obj-C object from the Json String NSDictionary *thisDictFromJSON = diff --git a/modules/swift/examples/code_snippets/SampleCodeTest.swift b/modules/swift/examples/code_snippets/SampleCodeTest.swift index 891b624db..19c518ed5 100644 --- a/modules/swift/examples/code_snippets/SampleCodeTest.swift +++ b/modules/swift/examples/code_snippets/SampleCodeTest.swift @@ -704,6 +704,7 @@ class SampleCodeTest { } + func dontTestToJson-Blob() throws { database = self.db // demonstrate use of JSON string @@ -2275,11 +2276,8 @@ class Query { var results = try! listQuery.execute() for row in results { - // If Query selected ALL, - // unpack items from encompassing dictionary // <.> - let jsonString = row.dictionary(at: 0)!.toJSON() - // ALTERNATIVELY: If Query selected specific items - let jsonString = row.toJSON() + // get the result into a JSON String + let jsonString = row.toJSON() // <.> let thisJsonObj:Dictionary = try! (JSONSerialization.jsonObject( @@ -2288,13 +2286,13 @@ class Query { as? [String: Any])! // <.> // Use Json Object to populate Native object - // Use Codable class to unpack JSON data to native object // <.> + // Use Codable class to unpack JSON data to native object let this_hotel:Hotel = (try JSONDecoder().decode( Hotel.self, from: jsonString.data(using: .utf8)! ) - ) + ) // <.> // ALTERNATIVELY unpack in steps this_hotel.id = thisJsonObj["id"] as! String