Getting the text of each span within block types using GROQ in Sanity.io
24 replies
Last updated: Feb 8, 2022
N
Hi here, I would like to know how can I get the text of each span within block types using GROQ?
Example:
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
N
I am getting 0 items always
Feb 8, 2022, 6:51 PM
N
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
N
The idea is to access to the text of each element of the
childrenproperty
Feb 8, 2022, 7:09 PM
N
It looks like it’s not working properly on an nested array like this one
Feb 8, 2022, 7:10 PM
N
No, it don’t work
Feb 8, 2022, 7:34 PM
N
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
N
Any suggestion would be appreciated
Feb 8, 2022, 7:40 PM
N
I was thinking in having a single field called
descriptionwith 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
N
user M
Thank you again!Feb 8, 2022, 9:18 PM
N
user M
Thank you again!Feb 8, 2022, 9:18 PM
N
It did the job.
Feb 8, 2022, 9:19 PM
N
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
N
user M
by the way, is there a way to specify a version for the queries?Currently I have many queries using V1and for this case I have to use a different, is that possible?
Feb 8, 2022, 9:34 PM
N
It looks it’s possible 🤔 I will try
Feb 8, 2022, 9:36 PM
N
It looks it’s possible 🤔 I will try
Feb 8, 2022, 9:36 PM
Ah, yes, you shouldn't be using
v1in 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
N
You’re awesome.Thanks again.
Feb 8, 2022, 9:40 PM
N
By the way,
user M
please, a last question.Do you know if the v2021-03-25has backward compatibility with
v1? if that’s the case I could make an upgrade in our service.
Feb 8, 2022, 9:43 PM
N
By the way,
user M
please, a last question.Do you know if the v2021-03-25has backward compatibility with
v1? if that’s the case I could make an upgrade in our service.
Feb 8, 2022, 9:43 PM
N
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.