Expand my Community achievements bar.

July 31st AEM Gems Webinar: Elevate your AEM development to master the integration of private GitHub repositories within AEM Cloud Manager.

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

Avatar

Level 4

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.

5 Replies

Avatar

Community Advisor

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{
      
      }
    }
  }
}

 

 

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.

Avatar

Community Advisor

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  

I don't think that is true. I've had bad answers marked as the answer a lot. Thanks for your attempt but it doesn't provide a solution. I want to be able to get the value of a sub field based on the field name. If I knew the value, as you have in the filter, then I don't need the query.

Avatar

Level 1

Hey, Did you get the answer to the question you asked by any chance? Got a similar case for me as well.