JSON filtering

This document provides information on how to filter the data in the JSON fields of your Hygraph project.

value_recursiveAnchor

You can filter the data in your JSON fields by using the following value_recursive syntax to find exact matches:

query for {
posts(where: { jsonField_value_recursive: "hallo" }) {
id
jsonField
}
}

The query above would search the JSON fields for an exact match of "hallo" on all values, excluding the json keys.

Using this filter, you can easily filter the content, even if you're not familiar with the json_path syntax.

json_path_existsAnchor

You can filter the data in your JSON fields by using json_path_exists syntax from PostgreSQL.

To be able to query this content, you need to know the JSON structure to navigate through it.

If you're not familiar with the structure, you can query for all content - without passing a filter, or with just a pagination filter depending on number of entries - and look into it. A great way to go about this kind of exploration is using the API Playground, where you can type “json” then use ctrl + space or command + space to display all the filters you can use and select one.

Please note that if the content data is in a JSON field that allows multiple values, the query will return the complete list.

Query syntax examplesAnchor

Imagine we have the following content data in a JSON field which allows multiple values:

"data": {
"testFieldsModels": [
{
"listJsonField": [
{
"items": {
"qty": 24,
"product": "Diaper"
},
"customer": "Lily Bush"
},
{
"items": {
"qty": 1,
"product": "Toy Car"
},
"customer": "Josh William"
},
{
"items": {
"qty": null,
"product": "Headphones"
},
"customer": "James Buyer"
}
]
}
]
},

Please note that in the query examples below, we escape the quotes with a backslash, like this \”. This is because the query is already inside double quotes, so any additional quotes require this format.

Query by quantityAnchor

You can use "$[*].** ? (@ > N)” to filter all values all for integers above a certain number. Just replace N with the number of your choice.

query for {
testFieldsModels(
where: { listJsonField_json_path_exists: "$[*].items.qty ? (@ > 5)" }
) {
listJsonField
}
}

The above query example returns products with a quantity greater than 5.

You can use:

  • >: Greater than
  • >=: Greater than or equal to
  • <: Less than
  • <=: Less than or equal to

Query by stringAnchor

You can use "$.** ? (@ == \"String\")” to filter all values all for the matching string:

query for {
testFieldsModels(
where: {
listJsonField_json_path_exists: "$.items.product ? (@ == \"Toy Car\")"
}
) {
listJsonField
}
}

The above query example returns products with the name “Toy Car”.

Query by nullAnchor

You can use "$[*].** ? (@ == null)” to filter all values all for null.

query for {
testFieldsModels(
where: { listJsonField_json_path_exists: "$[*].items.qty ? (@ == null)" }
) {
listJsonField
}
}

The above query example returns items where quantity (qty) = null.

Query by rangeAnchor

You can use "$[*].** ? (@ > N && @ < N)” to filter all values all for a range of numbers. Just replace N with the numbers of your choice.

query for {
testFieldsModels(
where: {
listJsonField_json_path_exists: "$[*].items.qty ? (@ > 1 && @ < 5)"
}
) {
listJsonField
}
}

The above query example returns items with a quantity between 1 and 5.

You can use:

  • >: Greater than
  • >=: Greater than or equal to
  • <: Less than
  • <=: Less than or equal to