# Query variable binding error

**URL:** <https://community.stardog.com/t/query-variable-binding-error/2541>\
**Category:** Support\
**Created:** [June 23, 2020, 3:48pm UTC](https://community.stardog.com/t/query-variable-binding-error/2541 "2020-06-23T15:48:42Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![arwesterinen](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/arwesterinen/32/40_2.png) [@arwesterinen](https://community.stardog.com/u/arwesterinen)\
**Post date:** [June 23, 2020, 3:48pm UTC](https://community.stardog.com/t/query-variable-binding-error/2541/1 "2020-06-23T15:48:42Z")

</div>

I have two queries with variable bindings (running in python with pystardog).

The first has only numeric values for the bound variables and works correctly. The following returns an error that the bound value is malformed. Here is the query:

```auto
PREFIX : <urn:xxx:ontology:> 
SELECT ?propValue WHERE { 
       BIND(IRI(CONCAT("urn:xxx:ontology:", ?prop)) as ?predicate) . 
      ?s ?predicate ?propValue } LIMIT 10

```

And the binding:

```auto
 encoder = json.JSONEncoder()
 queryBindings = encoder.encode({ "prop": "foo" })

```

And, here is the dump from the stardog.log:

```auto
WARN 2020-06-23 15:40:55,075 [stardog-user-3] com.complexible.stardog.protocols.http.server.StardogUndertowErrorHandler:accept(64): Unexpected exception was handled by the server
java.lang.IllegalArgumentException: The query binding 'foo' is a malformed Turtle value
        at com.complexible.stardog.protocols.http.server.ProtocolUtils.parseValue(ProtocolUtils.java:979) ~[stardog-protocols-http-server-7.3.1.jar:?]
        at com.complexible.stardog.protocols.http.server.ProtocolUtils.lambda$parseQuery$22(ProtocolUtils.java:967) ~[stardog-protocols-http-server-7.3.1.jar:?]
        at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183) ~[?:1.8.0_252]
        at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175) ~[?:1.8.0_252]
        at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175) ~[?:1.8.0_252]
        at java.util.stream.Streams$StreamBuilderImpl.forEachRemaining(Streams.java:419) ~[?:1.8.0_252]
        at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:647) ~[?:1.8.0_252]
        at java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:272) ~[?:1.8.0_252]
        at java.util.TreeMap$EntrySpliterator.forEachRemaining(TreeMap.java:2969) ~[?:1.8.0_252]
        at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482) ~[?:1.8.0_252]
        at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472) ~[?:1.8.0_252]
        at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150) ~[?:1.8.0_252]
        at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173) ~[?:1.8.0_252]

```

Obviously, I am missing something obvious. What is it?

Andrea

---

<div class="post-metadata">

**Author:** ![zachary.whitley](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/zachary.whitley/32/94_2.png) [@zachary.whitley](https://community.stardog.com/u/zachary.whitley)\
**Post date:** [June 23, 2020, 4:02pm UTC](https://community.stardog.com/t/query-variable-binding-error/2541/2 "2020-06-23T16:02:42Z")

</div>

Try passing your query bindings as a dict rather than json.

---

<div class="post-metadata">

**Author:** ![arwesterinen](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/arwesterinen/32/40_2.png) [@arwesterinen](https://community.stardog.com/u/arwesterinen)\
**Post date:** [June 23, 2020, 4:49pm UTC](https://community.stardog.com/t/query-variable-binding-error/2541/3 "2020-06-23T16:49:48Z")

</div>

Sorry, I wasn't clear and copied the wrong lines ... My query and parameters are passed via a REST API interface to the PyStardog code that calls the Stardog server.

I have to encode a Python dictionary to a JSON string to pass it (that's the JSONEncode code above) and then I have to decode it before I make the .graph call. Here is that code:

```auto
decoder = json.JSONDecoder()
queryDict = decoder.decode(queryBindings)
selectResults = conn.select(selectQuery, content_type='application/sparql-results+json',
                                        bindings=queryDict)

```

queryDict is indeed a dictionary and the code works for numeric bindings.

Andrea

---

<div class="post-metadata">

**Author:** ![jess](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/jess/32/10_2.png) [@jess](https://community.stardog.com/u/jess)\
**Post date:** [June 23, 2020, 9:55pm UTC](https://community.stardog.com/t/query-variable-binding-error/2541/4 "2020-06-23T21:55:10Z")

</div>

The binding substitution must be syntactically valid SPARQL. `foo` is not. You will need to quote it or use prefix/brackets if it's an IRI, eg. `"foo"` or `p:foo`.

---

<div class="post-metadata">

**Author:** ![arwesterinen](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/arwesterinen/32/40_2.png) [@arwesterinen](https://community.stardog.com/u/arwesterinen)\
**Post date:** [June 24, 2020, 2:28am UTC](https://community.stardog.com/t/query-variable-binding-error/2541/5 "2020-06-24T02:28:02Z")

</div>

Jess, I am using the value "foo" as a string and it is quoted in the Python dictionary. I was hoping that the following BIND could be used to create the predicate part of a triple in a WHERE clause ...

BIND(IRI(CONCAT("urn:xxx:ontology:", ?prop)) as ?predicate.

I thought with the variable binding that would create an IRI of urn:xxx:ontology:volume which is indeed a valid predicate in our ontology.

What am I missing? Do you mean that I should define the dictionary value as "'foo'" (using both single and double quotes)? That seems very odd.

Andrea

---

<div class="post-metadata">

**Author:** ![jess](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/jess/32/10_2.png) [@jess](https://community.stardog.com/u/jess)\
**Post date:** [June 24, 2020, 3:19am UTC](https://community.stardog.com/t/query-variable-binding-error/2541/6 "2020-06-24T03:19:46Z")

</div>

Just imagine that the string value you provide is substituted syntactically in the query. If you provide `foo` (which must be quoted in Python as `'foo'`), then the SPARQL you get would be `BIND(..., foo)`. This is not valid SPARQL syntax. For this reason, you must add quotes if it's a string. Written in Python, this could be look like `'"foo"'`. When placed in your SPARQL query, you would then expect something like `BIND(..., "foo")` which is valid syntax.

---

<div class="post-metadata">

**Author:** ![arwesterinen](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/arwesterinen/32/40_2.png) [@arwesterinen](https://community.stardog.com/u/arwesterinen)\
**Post date:** [June 25, 2020, 10:05pm UTC](https://community.stardog.com/t/query-variable-binding-error/2541/7 "2020-06-25T22:05:34Z")

</div>

OK. Obviously, I totally spaced on that.

Will give it a try and report back.

Sorry to be dense!

Andrea

---

<div class="post-metadata">

**Author:** ![arwesterinen](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/arwesterinen/32/40_2.png) [@arwesterinen](https://community.stardog.com/u/arwesterinen)\
**Post date:** [July 7, 2020, 7:43pm UTC](https://community.stardog.com/t/query-variable-binding-error/2541/8 "2020-07-07T19:43:04Z")

</div>

Just to close this out .... Adding quotes worked.

Andrea

---

<div class="post-metadata">

**Author:** ![jess](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/jess/32/10_2.png) [@jess](https://community.stardog.com/u/jess)\
**Post date:** [July 7, 2020, 9:11pm UTC](https://community.stardog.com/t/query-variable-binding-error/2541/9 "2020-07-07T21:11:13Z")

</div>

Thanks for the update. Glad it's working!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)\
**Post date:** [July 21, 2020, 9:24pm UTC](https://community.stardog.com/t/query-variable-binding-error/2541/10 "2020-07-21T21:24:18Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
