GraphQL how to filter on a field that is a JSON object | Community
Skip to main content
Robert-Harper
Level 3
February 1, 2023

GraphQL how to filter on a field that is a JSON object

  • February 1, 2023
  • 1 reply
  • 3640 views

I have a content fragment template where there is a JSON type field that holds a list of image URLs. Each entry is a named field with the URL to an image as the value. This was done due to the maximum numbers of URLs that may be checked in validation of the text fields in a content fragment that look to be a URL.

 

What I'd like to do is reduce the size of the images JSON by selecting which field I want.

 

The normal returned response would be something like:

{ "data": { "testModel": { "items": [ { "itemNumber": "369769", "description": "Some description for the fragment", "images": { "image1": "/content/dam/images/image1.jpg", "image2": "/content/dam/images/image2.jpg", "image3": "/content/dam/images/image3.jpg", "image4": "/content/dam/images/image4.jpg", "image5": "/content/dam/images/image5.jpg", "image6": "/content/dam/images/image6.jpg", "image7": "/content/dam/images/image7.jpg", "image8": "/content/dam/images/image8.jpg", "image9": "/content/dam/images/image9.jpg", "image10": "/content/dam/images/image10.jpg", "image11": "/content/dam/images/image11.jpg", "image12": "/content/dam/images/image12.jpg", "image13": "/content/dam/images/image13.jpg", "image14": "/content/dam/images/image14.jpg", "image15": "/content/dam/images/image15.jpg", "image16": "/content/dam/images/image16.jpg", "image17": "/content/dam/images/image17.jpg", "image18": "/content/dam/images/image18.jpg", "image19": "/content/dam/images/image19.jpg", } } ] } } }

What I would like is to have a way of getting the itemNumber, description, and images.image12 and not having to get the entire images object in the response.

 

Has anyone done this before or know of a way to do this?

Thanks in advance.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

1 reply

Saravanan_Dharmaraj
Community Advisor
Community Advisor
February 1, 2023

Please give a try like this, just filtering "image1". you can use filter to the json with that specific value, but i dont think it will remove other values in the array when giving the response. 

 

 

query { testModel(filter: { images: { _match: { image1: { _expressions: [ { value: "/content/dam/images/image1.jpg" } ] } } } }) { items { itemNumber description images{ } } } }

 

 

Robert-Harper
Level 3
February 6, 2023

I don't appreciate when people post what they think is the answer and then mark their own answer as THE answer. This doesn't really work because I would have to know the value for the given field beforehand. What I need is to get the value for a given field name. Since there are many configurations, I want to get the value under each configuration and the image path will be different for each one. This solution simply doesn't work as shown.
Not valid as THE answer.

Saravanan_Dharmaraj
Community Advisor
Community Advisor
February 6, 2023

The person who is responding/trying to help can NOT select their response as "Answer". Either the original post(question) owner or the admin can select an answer if the admin feels the response is fine. Just FYI.

@kautuk_sahni