Concatenating first character of fields in Sanity GROQ query

6 replies
Last updated: Nov 29, 2025
Why doesn't this work?
"initials": _firstName_[0]
+ _lastName_[0]
😞
AI Update

I can see the issue! The problem is with how you're trying to concatenate strings in GROQ. You're using _firstName_[0] which is trying to reference a field with underscores wrapping it, but that's not the correct syntax.

In GROQ, you need to use the + operator between string values, and you should reference fields by their actual field names. Here's the correct syntax:

"initials": firstName[0..1] + lastName[0..1]

Or if you want just the first character of each:

"initials": firstName[0] + lastName[0]

Key differences:

  1. Field names: Use firstName and lastName (not _firstName_ and _lastName_) unless your actual field names have underscores
  2. String slicing: [0..1] gets the first character as a string, while [0] also works for single characters
  3. Concatenation: The + operator between the two field references combines them

Full example in a projection:

*[_type == "person"]{
  firstName,
  lastName,
  "initials": firstName[0..1] + lastName[0..1]
}

This would turn someone named "John Doe" into initials "JD".

If your field names actually do have underscores (like first_name), then use those exact names. The important part is removing the extra underscores that were wrapping the field names in your original attempt, and making sure the + operator is positioned correctly between the two values you want to concatenate.

Show original thread
6 replies

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?