Quickstart

Quickstart for extracting and filtering values inside JSON fields with the json() function and _json filter operator.

Directus provides two ways of working with JSON fields in queries:

  • json(field, path): A selection function to extract a value from a JSON document. It can be used in the fields, sort, and alias parameters.
  • _json: A filter operator that lets you filter records based on values within a JSON document. It can be used within the filter parameter.

Both use the same path notation and work across REST, GraphQL, and the SDK.

JSON filtering is also available visually in the Studio filter UI. See Filtering JSON Fields.

Using the json() function

Function Syntax:

json(field, path)

Example:

Extract the color key from a metadata JSON field:

import { createDirectus, rest, readItems } from "@directus/sdk";
const directus = createDirectus("https://directus.example.com").with(rest());

const result = await directus.request(
  readItems("articles", {
    fields: ["id", "title", "json(metadata, color)"],
  }),
);

Response:

{
  "data": [
    {
      "id": 1,
      "title": "An Article",
      "metadata_color_json": "blue"
    }
  ]
}

For REST and SDK, the extracted value is returned under the alias {field}_{path}_json with ., [, and ] replaced by underscores.

Filtering with _json

Operator Syntax:

{
    "field": {
        "_json": {
            "path": {
                "_operator": value
            }
        }
    }
}

Example:

Find articles where the color key inside metadata equals "blue":

import { createDirectus, rest, readItems } from "@directus/sdk";
const directus = createDirectus("https://directus.example.com").with(rest());

const result = await directus.request(
  readItems("articles", {
    filter: {
      metadata: {
        _json: { color: { _eq: "blue" } },
      },
    },
  }),
);

Response:

{
  "data": [
    { "id": 1, "title": "An Article" },
    { "id": 4, "title": "Another Article" }
  ]
}

Refer to the Supported Inner Operations section for a list of available operators within the _json operator.

Path Notation

Paths use dot notation for object keys and bracket notation for array indices.

PatternExampleMeaning
keycolorTop-level object key
a.b.csettings.theme.colorNested object key
[n]tags[0]Array element at index n
a[n].bitems[0].nameMixed object/array access

Wildcards (*, [*]) and other special characters are not currently supported. See the Unsupported Path Expressions section for a complete list of unsupported characters.

More information

For advanced usage details and additional examples, see Advanced JSON Querying.

Get once-a-month release notes & real‑world code tips...no fluff. 🐰