Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Getting the text of each span within block types using GROQ in Sanity.io

24 replies
Last updated: Feb 8, 2022
Hi here, I would like to know how can I get the text of each span within block types using GROQ?
Example:


{
  // ...
  "description": [
    {
      "_key": "1c68d5f6b2a3",
      "_type": "block",
      "children": [
        {
          "_key": "c33a78ff1695",
          "_type": "span",
          "marks": [],
          "text": "A synergistic blend of bioavailable vitamins C & D, herbs, NAC and zinc designed to help improve your long-term immune response."
        }
      ],
      "markDefs": [],
      "style": "normal"
    },
    // ...
  ],
  // ...
}

Feb 8, 2022, 6:49 PM
I am getting 0 items always
Feb 8, 2022, 6:51 PM
user M
I know that you might have a lot of questions around, but I was wondering if you can help me with this one instead of this , I need a set of products but I can’t get the description easily yet.
Feb 8, 2022, 7:07 PM
The idea is to access to the text of each element of the
children
property
Feb 8, 2022, 7:09 PM
It looks like it’s not working properly on an nested array like this one
Feb 8, 2022, 7:10 PM
Can you try:
"description": description[].children[].text 
Feb 8, 2022, 7:33 PM
No, it don’t work
Feb 8, 2022, 7:34 PM
Are you trying to make each text node its own named field? If so, why?
Feb 8, 2022, 7:36 PM
Good question… 😪 I think I might be doing something wrong, but my intention is to get a JSON file of products with just few props in it, like title, id, description, tags, benefits to use Algolia, but I’m not sure if I am doing something wrong.
Feb 8, 2022, 7:40 PM
Any suggestion would be appreciated
Feb 8, 2022, 7:40 PM
I was thinking in having a single field called
description
with all its texts as a single string value, but I am not sure if this is the right strategy
Feb 8, 2022, 7:42 PM
Got it! I understand now. You'll want to use the built in Portable Text function then. This should convert your description field into plain text:
"description": pt::text(description)
Feb 8, 2022, 8:24 PM
Got it! I understand now. You'll want to use the built in Portable Text function then. This should convert your description field into plain text:
"description": pt::text(description)
Feb 8, 2022, 8:24 PM
user M
Thank you again!
Feb 8, 2022, 9:18 PM
user M
Thank you again!
Feb 8, 2022, 9:18 PM
It did the job.
Feb 8, 2022, 9:19 PM
Interesting, I was using the V1 and it didn’t work either, but reading more about the API Version it enable such outcomes, even some of the old that I was trying.This is now working as an array of string values:

description[].children[][].text,

Feb 8, 2022, 9:20 PM
user M
by the way, is there a way to specify a version for the queries?Currently I have many queries using
V1
and for this case I have to use a different, is that possible?
Feb 8, 2022, 9:34 PM
It looks it’s possible 🤔 I will try
Feb 8, 2022, 9:36 PM
It looks it’s possible 🤔 I will try
Feb 8, 2022, 9:36 PM
Ah, yes, you shouldn't be using
v1
in most cases. In Vision, you can select the one you want to use (usually
v2021-03-25
). When you're query from an external source you can use one of the methods here .
Feb 8, 2022, 9:37 PM
You’re awesome.Thanks again.
Feb 8, 2022, 9:40 PM
By the way,
user M
please, a last question.Do you know if the
v2021-03-25
has backward compatibility with
v1
? if that’s the case I could make an upgrade in our service.
Feb 8, 2022, 9:43 PM
By the way,
user M
please, a last question.Do you know if the
v2021-03-25
has backward compatibility with
v1
? if that’s the case I could make an upgrade in our service.
Feb 8, 2022, 9:43 PM
No worries, I read the docs detailed, it’s super clear.
Feb 8, 2022, 9:49 PM

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?