Question

I am currently using Redbranch-Hammock to interact with a CouchDB database. I am able to perform simple queries of CouchDB views but become unstuck when trying to add complex keys to the query and I believe this is to do with how Redbranch-Hammock uses JTokens to represent the start and end keys (via the To(..) and From(...) methods on the Query object).

For example, If I have the following URL:

localhost:5984/db/_design/doc/_view/count_by_tag?startkey=["hyundai"]&endkey=["hyundai",{}]

How would I represent the end key ["hyundai",{}] via a JToken object?

Was it helpful?

Solution

If you're starting from the JSON string value and are trying to get a JToken from it, use JToken.Parse:

JToken token = JToken.Parse("[\"hyundai\",{}]");

Or, you can manually create a JToken that resolves to the JSON string ["hyundai",{}] like this:

JArray token = new JArray();
token.Add(new JValue("hyundai"));
token.Add(new JObject());

To convert a JToken to JSON, just use ToString:

string json = token.ToString(Formatting.None);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top