Go Behind the Experience to see how Tecovas brings the West to life with Sanity 🤠 July 18th

Handling potential null values when combining properties in API endpoint

3 replies
Last updated: Feb 23, 2023
How do you handle potential null values that you may want to combine into one property. For example:
  "address": address.line1 + '\\n'
    +address.line2 + '\\n'
    +address.city + ', MA '
If address.line2 is null then the whole address returns null. Is there a way to do that part conditionally that there is data in line2?
Feb 23, 2023, 5:11 PM
You can a combination of
, and
, etc. Generally I would recommend doing this of string manipulation with JavaScript, though.
Feb 23, 2023, 5:33 PM
user L
. I know I'm doing something a little weird. Storytime:I had an old homemade api endpoint that a vendor was using to pull data into a mobile app. We are cutting over to a new software system and website, and I'm trying to coordinate it all. The mobile app vendor didn't have time to make the change to using our sanity api with new data structure, so I'm using groq to reshape the data to look like the old (poorly designed by me) api
Feb 23, 2023, 7:56 PM
coalesce() is doing the job
Feb 23, 2023, 7:58 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?