Writing GROQ queries to flatten nested objects and transform object entries into an array
38 replies
Last updated: Oct 17, 2021
P
How do I write a GROQ query for using that attribute. The path is something like
root[":items"].child[":items"]
Oct 16, 2021, 7:56 AM
P
{ "title": "Address", "lastModifiedDate": 1622813281583, "templateName": "spa-sso-template", "designPath": "/libs/settings/wcm/designs/default", "cssClassNames": "page basicpage", "PublishInstance": true, "language": "en", ":type": "pwa/components/page", ":items": { "root": { "columnClassNames": { "responsivegrid": "aem-GridColumn aem-GridColumn--default--12" }, "gridClassNames": "aem-Grid aem-Grid--12 aem-Grid--default--12", "columnCount": 12, ":items": { "responsivegrid": { "columnClassNames": { "add_new_address": "aem-GridColumn aem-GridColumn--default--12", "analytics_page_data": "aem-GridColumn aem-GridColumn--default--12" }, "gridClassNames": "aem-Grid aem-Grid--12 aem-Grid--default--12", "columnCount": 12, ":items": { "add_new_address": { "pageName": "addresslist", "category": "Address", "addNewAddressLabel": "Add new", "editAddressPageUrl": "/addresses/address-details", "backBtnUrl": "/homepage", "recName": "address", "componentTitle": "Address", "widgetName": "Address", "widgetId": "Address", "componentPosition": 1, ":type": "pwa/components/pwa/my-account/add-new-address" }, "analytics_page_data": { "pageTitle": "Address", "navTitle": "Address", "pageName": "addresslist", "category": "Address", "subSection1": "", ":type": "pwa/components/pwa/common/analytics-page-data" } }, ":itemsOrder": ["add_new_address", "analytics_page_data"], ":type": "wcm/foundation/components/responsivegrid" } }, ":itemsOrder": ["responsivegrid"], ":type": "wcm/foundation/components/responsivegrid" } }, ":itemsOrder": ["root"], ":path": "/addresses/address", ":hierarchyType": "page" }
Oct 16, 2021, 8:12 AM
P
root can have
:items, which can further have
:itemsnested
Oct 16, 2021, 8:13 AM
P
I want to pull out all the
:itemsand flatten it out
Oct 16, 2021, 8:13 AM
P
Wondering if I can do it with GROQ
Oct 16, 2021, 8:14 AM
Understand! Yes, it seems like you can access the paths like you would in JS, so if you want to get to the
add_new_addressobject, it’s:
[":items"].root[":items"].responsivegrid[":items"].add_new_address
Oct 16, 2021, 8:17 AM
P
I tried it on groq.dev and its returning null
Oct 16, 2021, 8:18 AM
P
Oct 16, 2021, 8:19 AM
P
ah its working now
Oct 16, 2021, 8:19 AM
P
forgot the * in front
Oct 16, 2021, 8:20 AM
P
also had to refresh the page
Oct 16, 2021, 8:20 AM
P
thanks
user Y
, you have unblocked me 🙂Oct 16, 2021, 8:20 AM
P
BTW, I am going to propose Sanity as our CMS...making sure all our use cases work
Oct 16, 2021, 8:21 AM
P
will bug you more for sure 🙂
Oct 16, 2021, 8:21 AM
You’re always welcome to ask here or in help (or in any of the more specialized channels) 👍
Oct 16, 2021, 8:22 AM
P
thank you! excited about exploring this
Oct 16, 2021, 8:22 AM
P
is there a way to transform all entries of an object into an array.
Oct 16, 2021, 9:21 AM
P
{ "add_new_address": { "pageName": "addresslist", "category": "Address", "addNewAddressLabel": "Add new", "editAddressPageUrl": "/addresses/address-details", "backBtnUrl": "/homepage", "recName": "address", "componentTitle": "Address", "widgetName": "Address", "widgetId": "Address", "componentPosition": 1, ":type": "pwa/components/pwa/my-account/add-new-address" }, "analytics_page_data": { "pageTitle": "Address", "navTitle": "Address", "pageName": "addresslist", "category": "Address", "subSection1": "", ":type": "pwa/components/pwa/common/analytics-page-data" } }
Oct 16, 2021, 9:21 AM
P
converting the above to [{}, {}] ?
Oct 16, 2021, 9:22 AM
P
not sure if we can project entries of an object as an array ?
Oct 16, 2021, 9:24 AM
as in, to this?
?
[{ "pageName": "addresslist", "category": "Address", "addNewAddressLabel": "Add new", "editAddressPageUrl": "/addresses/address-details", "backBtnUrl": "/homepage", "recName": "address", "componentTitle": "Address", "widgetName": "Address", "widgetId": "Address", "componentPosition": 1, ":type": "pwa/components/pwa/my-account/add-new-address" }, { "pageTitle": "Address", "navTitle": "Address", "pageName": "addresslist", "category": "Address", "subSection1": "", ":type": "pwa/components/pwa/common/analytics-page-data" }]
Oct 16, 2021, 9:32 AM
P
yup...can we do it ?
Oct 16, 2021, 10:06 AM
P
btw, there is an array of property names in
:itemsOrder
Oct 16, 2021, 10:07 AM
P
but if we can project the
:itemsdirectly to an array, we are golden
Oct 16, 2021, 10:08 AM
P
Hi Geoff, I was thinking of making this more automatic where the property names can be picked up from the
:itemsobject
Oct 16, 2021, 5:32 PM
P
I have to run a script that needs to do it automatically
Oct 16, 2021, 5:32 PM
P
recursively if possible
Oct 16, 2021, 5:46 PM
P
but we can just start with the root for now
Oct 16, 2021, 5:46 PM
P
take the
:itemsand convert the values into an array of objects
Oct 16, 2021, 5:46 PM
Is this any help?
*{ 'result': [{...@[":items"].root[":items"].responsivegrid[":items"]}] }
Oct 16, 2021, 5:46 PM
P
user Y
got what I wanted here: https://sanity-io-land.slack.com/archives/C01T1B5GVEG/p1634376779482500?thread_ts=1634370996.477200&cid=C01T1B5GVEG Oct 16, 2021, 5:48 PM
P
This is the closest I could get: https://groq.dev/yvB2bHzUeilM41oJ0TauNs
I was hoping the
I was hoping the
@would do the trick...but not working
Oct 17, 2021, 7:50 AM
P
idea is to select all the property names from the ":itemsOrder" and use that to pick the item from the ":items" object
Oct 17, 2021, 7:50 AM
P
I was hoping this would work:
*[":items"].root[":itemsOrder"] | {"item": *[":items"].root[":items"][@]}
Oct 17, 2021, 8:48 AM
Sanity– build remarkable experiences at scale
Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.