🎤 Builder Talk: The Story Behind Lady Gaga’s Digital Experience – Register now

Troubleshooting GROQ query for generating author initials in Sanity

6 replies
Last updated: Jun 28, 2024
Why doesn't this work?
"initials": _firstName_[0]
+ _lastName_[0]
😞
Jun 23, 2024, 4:52 AM
Work where? is this a Groq query? What does your schema look like?
Jun 23, 2024, 11:29 AM
*[_type == "author"] {
  name,
  "avatarUrl": select(
    defined(image) => image.asset->url,
    "<https://ui-avatars.com/api/?name=>" + 
    coalesce(
      substr(firstName, 0, 1),
      ""
    ) + 
    coalesce(
      substr(lastName, 0, 1),
      ""
    ) +
    "&background=random"
  )
}
Jun 23, 2024, 3:20 PM
Assuming a groq query and not javasript
Jun 23, 2024, 3:21 PM
likely need to do
firstname.split()[0] + lasname.split()[0]
to turn string into array first.
Jun 23, 2024, 3:26 PM
Yes a groq query. We can do that in JS so I just thought it would be nice and simple here, but instead my projection looks like this:
"initials": array::join(string::split(firstName, "")[0...1], "") + array::join(string::split(lastName, "")[0...1], ""),
This works, but it feels icky.
Jun 24, 2024, 4:06 PM
*[_type=="vendor"] {
  "raw" : string::split(vendorName,""),
  "lowercase" : string::split(lower(vendorName),""),
}{
  "initials" : raw[!(@ in ^.lowercase)]
}

// leads to
initials:[…] 2 items
  0:J
  1:B
Is another approach. A different version of this can be a good way to capture duplicates, comparing a regular array and an array::unique() array and accounting for the difference.
Jun 28, 2024, 5:18 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.

Was this answer helpful?

Related answers

Get more help in the community Slack

TopicCategoriesFeaturedRepliesLast Updated
Clarification on creating drafts from a published document in Sanity.io.Dec 14, 2023
Trouble updating Sanity version, seeking help and guidance.Jan 24, 2024
GROQ query for getting references from arrays in another array in Sanity.ioApr 9, 2020
Upgrading to Sanity v3 causing ECONNRESET error, resolved by updating Node versionMay 23, 2023
How to fetch an array of images using groq in Sanity.ioMay 21, 2023
Discussion about using Sanity.io as a backend solution for small projects and its featuresApr 18, 2020
Clarification on obtaining image orientation and aspect ratio in Sanity.ioJul 22, 2020
Isolating datasets in different studios using Spaces in Sanity.ioJul 21, 2020
Issue setting value of JSON field with document action in Sanity StudioApr 22, 2020
Getting the average color of an image in Sanity CMS using metadata palette info.Dec 3, 2020

Related contributions

Turbo Start Sanity
- Template

The battle-tested Sanity template that powers Roboto Studio's websites

Go to Turbo Start Sanity

Schema UI - Next.js Sanity Starter
- Template

A Next.js starter template with Next.js 15, Tailwind CSS, shadcn/ui, and Sanity CMS with Live Editing. Get production-ready React components with matching Sanity schemas and queries. Build dynamic pages faster while keeping full control over customization.

Serge Ovcharenko
Go to Schema UI - Next.js Sanity Starter

The Swaddle
- Made with Sanity

A new brand identity to represent a more mature company, to signify The Swaddle’s evolution from publisher to production house, combined with an easier to navigate platform that can surface multiple content types - drawing readers through The Swaddle’s content offering.

Nightjar
Go to The Swaddle