Contentful logo

Contentful Community

Queries on `JSON object` fields

Hello.
I have a field of type JSON object called technical_details. It stores stuff that are not requires by all entities, but still common among many. Here’s an example:

{
	"weight": {
		"units": "kg",
		"value": 16300
	},
	"width": {
		"units": "m",
		"value": 2.5
	},
	"heigth": {
		"units": "m",
		"value": 3.24
	},
	"length": {
		"units": "m",
		"value": 5.78
	},
	"workingheigth": {
		"units": "m",
		"value": 26.5
	},
... more technical details ...
}

I want to query on these fields. Get all items where width is greater that X and length smaller than Y.

Something like this:

https://cdn.contentful.com/spaces/u...space_id..7e/entries?access_token=fe7..token..3a&content_type=product&fields.artNr[gte]=10

This works, as the artNr field is an Integer field. But I cannot seem to the this same logic to work on a JSON object field:

https://cdn.contentful.com/spaces/u3..id..7e/entries?access_token=fe7..token..73a&content_type=product&fields.technical_details.Bredde.value[gte]=10

Any version of these range operators I’ve testes on a JSON object field gives an error like this:

{
    "sys": {
        "type": "Error",
        "id": "InvalidQuery"
    },
    "message": "The query you sent was invalid. Probably a filter or ordering specification is not applicable to the type of a field.",
    "details": {
        "errors": [
            {
                "name": "unknown",
                "path": [
                    "fields",
                    "technicalDetails",
                    "nb-NO",
                    "Bredde",
                    "value"
                ],
                "details": "The path \"fields.technicalDetails.nb-NO.Bredde.value\" is not recognized"
            }
        ]
    },
    "requestId": "ff5...b9"
}

Are these range queries not applicable to JSON object fields?

Hey @eirik.stavelin,

At the moment, it is not possible to extend your queries or use search parameters within your JSON object fields.

Still, I completely how that’d be helpful for your use case, and I’m promptly creating a feature request for this :slightly_smiling_face:

2 Likes

Thanks.

(Glad to hear someone say it not possible, because it seems like a thing that should work, and it was not clear why it didn’t).

We don’t make that very explicit in the documentation, but we do mention that our search parameters work with any of our collection endpoints, which includes entries, content types, assets, but not directly JSON object fields themselves.

https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters

https://www.contentful.com/developers/docs/concepts/data-model/

Anyway, I’m glad this helped clearing things out and let me know if you have any other questions :smile:

@gabriel Any news on that feature request? That would be really helpful to do queries on any content stored in the JSON field. :slight_smile:

1 Like

@gabriel same for me, i would be absolutely glad is this feature was released.