# Custom JSON Query Not Working

**URL:** <https://forums.losant.com/t/custom-json-query-not-working/5984>\
**Category:** Help\
**Tags:** workflow\
**Created:** [March 27, 2026, 6:23pm UTC](https://forums.losant.com/t/custom-json-query-not-working/5984 "2026-03-27T18:23:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karim\_Kabbara](https://avatars.discourse-cdn.com/v4/letter/k/8edcca/32.png) [@Karim\_Kabbara](https://forums.losant.com/u/Karim_Kabbara)\
**Post date:** [March 27, 2026, 6:23pm UTC](https://forums.losant.com/t/custom-json-query-not-working/5984/1 "2026-03-27T18:23:35Z")

</div>

I am trying to query a data table using the deviceId column. Why wont this work?

Query:

```json
{
  "$and": [
    {
      "deviceId": {
        "$in": "{{working.deviceIds}}"
      }
    }
  ]
}

```

Payload:

```json
{
  "time": "2026-03-27T18:09:38.527Z",
   "working": {
      "deviceIds": ["68a8fe050edfd2a2b5a64ddb", "693c54e5a4c89b7235b66f50"]
   }
}

```

Result

```json
{
  "working": {
    "deviceIds": ["68a8fe050edfd2a2b5a64ddb", "693c54e5a4c89b7235b66f50"],
    "tableResult": {
      "error": {
        "type": "Validation",
        "message": "deviceId: value for $in must be an array"
      }
    }
  }
}

```

Workflow:

 ![image](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/f/ff666aadebbe3fa41de2fa61d5f97d20bc63e843.png)

---

<div class="post-metadata">

**Author:** ![Dylan\_Schuster](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/dylan_schuster/32/2850_2.png) [@Dylan\_Schuster](https://forums.losant.com/u/Dylan_Schuster)\
**Post date:** [March 27, 2026, 6:31pm UTC](https://forums.losant.com/t/custom-json-query-not-working/5984/2 "2026-03-27T18:31:08Z")

</div>

If you use the [template tester](https://docs.losant.com/workflows/platform-template-tester/) that is built into the workflow editor, you will see that your query resolves to the following:

 ![Screenshot 2026-03-27 at 2.28.43 PM](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/b/b83f3edd43c50d62387c08ec12400a955a58c511.png)

Notice how `{{working.deviceIds}}`, when the referenced value is an array, concatenates all items as a string separated by commas. (Similar to the [JavaScript Join method](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join).)

What you need to do instead is not wrap the value in quotes and to make use of the [`{{jsonEncode}}` helper](https://docs.losant.com/references/templating/#jsonencode-val-spacerstr) like so:

```json
{
  "$and": [
    {
      "deviceId": {
        "$in": {{jsonEncode working.deviceIds}}
      }
    }
  ]
}

```

---

<div class="post-metadata">

**Author:** ![Karim\_Kabbara](https://avatars.discourse-cdn.com/v4/letter/k/8edcca/32.png) [@Karim\_Kabbara](https://forums.losant.com/u/Karim_Kabbara)\
**Post date:** [March 27, 2026, 7:15pm UTC](https://forums.losant.com/t/custom-json-query-not-working/5984/3 "2026-03-27T19:15:38Z")

</div>

Thanks, that did it.

If I may suggest, the doc page should show an example of jsonEncode when it comes to Advanced Mode. That was my first go to.

> **[Advanced Mode​ - Advanced Queries | Losant IoT Platform Documentation](https://docs.losant.com/references/advanced-queries/#advanced-mode)**
>
> A number of application resources can be retrieved using an advanced query, which allows for finding resources that meet more criteria than can be set through traditional query methods. | Learn more about building advanced queries in the Losant...

Thanks again.

---

<div class="post-metadata">

**Author:** ![Dylan\_Schuster](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/dylan_schuster/32/2850_2.png) [@Dylan\_Schuster](https://forums.losant.com/u/Dylan_Schuster)\
**Post date:** [March 27, 2026, 8:33pm UTC](https://forums.losant.com/t/custom-json-query-not-working/5984/4 "2026-03-27T20:33:48Z")

</div>

Fair point. I’ll file a ticket about getting an example into the docs. Thanks.
