Raw N1QL query produces valid list of strings on the couchbase server interface, but java SDK gives string with extra quotes

I have a simple query like so:

SELECT raw e.name
FROM `travelers` AS e 

This produces a list like so:

[
 "peter",
  "Ronda",
  "James"
]

However when the query result is received through the couchbase SDK, the list looks like this:

[
 ""peter"",
 ""Ronda"",
  ""James""
]

Does anyone know how can I fix this?

Can you indicate which version of the SDK you’re using? 2.x or 3.x? And maybe a snippet of what you’re doing with the results?

We’ve enhanced the way we process query results quite a bit in 3.x, so it could be something there we got wrong or didn’t consider. Unfortunately, I can’t quite tell what you’re doing from just the results.

@ingenthr,
When I query CB like so:

  N1qlQuery parameterizedN1qlQuery = N1qlQuery.parameterized(query, jsonObject);
  N1qlQueryResult result = bucket.query(parameterizedN1qlQuery);

This is the list that is in the result.allRows():


Notice how every item is not a string but a string within a string.
I am runing on sdk v.2.7.0

Hi Carlos,

This looks normal to me. The actual value of the row is "some-uuid" (including the quotes, since it’s a JSON string value). When your IDE displays the value, it includes another set of quotes to indicate the value is a string… that’s why when you look at it in the debugger you see ""some-uuid"". When you access the row in your java code, I expect you’ll only see one set of quotes as expected for a JSON string value.

You might need to jump through some hoops to parse this JSON value into a Java String, since in SDK 2.x the N1qlQueryRow interface doesn’t provide a nice accessor when the row is just a JSON string. There’s a value() method that return a JsonObject, but that method will throw an exception in this scenario since the value is not a JSON Object node. :frowning:

Instead you can call row.byteValue() to get the raw JSON bytes, then use the JSON parser of your choice to parse the JSON string into a Java string. If you want to use Jackson, that could look something like this:

private static final ObjectMapper mapper = new ObjectMapper();
...
for (N1qlQueryRow row : rows) {
  String value = mapper.readValue(row.byteValue(), String.class);
  System.out.println(value);
}

In SDK 3 this has been much improved; there you can call rowsAs(String.class) directly on the QueryResult object to get the result as a list of Strings.

Thanks,
David