Amit be
Amit be

Reputation: 469

How to provide correctly a tuple parameter to pyDocumentDB query

I tried in many ways to send a tuple parameter like: {"name": "@p", "value": tuple(['a','b']) }

to the query, which is simple as:

select * from c where c.p in @p

I always get a syntax error or no results. when i'm copying the parameter from the console to a query i get the results as expected.

help?

Upvotes: 0

Views: 554

Answers (1)

Peter Pan
Peter Pan

Reputation: 24138

According to the section Request Body of the REST reference Querying DocumentDB resources using the REST API, the requirement for the value of the parameter as below.

parameters Required. A JSON array of parameters specified as name value pairs. The parameter array can contain from zero to many parameters.Each parameter must have the following values:name: the name of the parameter. Parameter names must be valid string literals and begin with ‘@’. value: the value of the parameter. Can be any valid JSON value (string, number, object, array, Boolean or null).

So you can pass a Python tuple parameter for a DocumentDb query, please using an array like ['a', 'b'] instead of tuple(['a', 'b']), or just convert it via the code like list(tuple(['a', 'b'])).


Update:

I reviewed the source code of pydocumentdb and found the parameters in the request body of the DocumentDb query is serialized by using the method json.wraps which will serialize the tuple to list. For example, execute the code json.wraps(('a', 'b')), then get the result ['a', 'b']. So if using pydocumentdb, the only way for your case is that directly embed the tuple value into the query string.

Upvotes: 1

Related Questions