Writing to Sanity dropdown fields via script not displaying in Studio UI

10 replies
Last updated: Jul 20, 2022
Hello all 🙂I’m in the process of writing content to Sanity Studio using a script to create new documents. So far I have been able to adjust the shape of my data as necessary, and write new documents successfully. That is except for two fields I have which, in my Sanity Studio, are displayed as a dropdown layout choice of two options each. Even though my field names match, and the data appears to be there when I inspect a particular document, it is not visible in the UI and my dropdown remains blank, instead of displaying the data I have written.
Has anyone had any experience with this?
I’m wondering how to get my data appearing in that dropdown field correctly in the studio.
AI Update

Based on your description, it sounds like you have a string field with a dropdown (list) layout defined in your schema, but when you import data via script, the values aren't showing up in the Studio UI even though they appear in the document data.

The most common cause of this issue is a mismatch between the value you're writing and the values defined in your schema's options.list array.

In Sanity, when you define a dropdown field, you typically use something like:

{
  name: 'myDropdownField',
  type: 'string',
  options: {
    list: [
      {title: 'Option One', value: 'option-one'},
      {title: 'Option Two', value: 'option-two'}
    ],
    layout: 'dropdown'
  }
}

The key issue: Your script must write the exact value (not the title) that's defined in the schema. If your data contains 'Option One' but your schema expects 'option-one', the Studio won't recognize it and the dropdown will appear blank.

Here's how to troubleshoot:

  1. Check your schema definition - Look at the options.list array for those fields and note the exact value properties
  2. Inspect your written data - Check what values your script is actually writing to those fields (you can use Vision plugin or check the raw JSON)
  3. Ensure exact match - The written value must exactly match one of the value properties in your schema's list (case-sensitive)

For example, if your schema has:

options: {
  list: ['draft', 'published', 'archived']
}

Your script must write exactly 'draft', 'published', or 'archived' - not 'Draft' or any variation.

Additional tip: If you're migrating from another system, you might need to create a mapping function in your script to transform your source values into the values expected by your Sanity schema. Something like:

const statusMap = {
  'Draft': 'draft',
  'Published': 'published',
  'Archived': 'archived'
}

const sanitizedData = {
  ...yourData,
  status: statusMap[yourData.status] || 'draft'
}

If you share your schema definition for one of those dropdown fields and an example of the data you're writing, I can help pinpoint the exact mismatch!

Show original thread
10 replies
Hi, can you post your document schema? One thing I notice is that your dropdown shows
Solo
while your data is
Solo Show
- this will have to match using a predefined list of options with an
{ title: 'Solo', value: 'Solo Show' }
item (see documentation ) or otherwise use
Solo
as your data value.
How is your schema defined for these fields? Because looking at it, it seems that the names don’t match between your data and your schema.
Yes, I noticed that I had Solo Show instead of Solo, you’re right that was a mistake. But the other field has the correct options for either External or Gallery, and that field seems to contain data that is not showing.
My schema looks like this:
   {
      name: "location",
      title: "Location",
      type: "string",
      options: {
        list: [
          { title: "Gallery", value: "gallery" },
          { title: "External", value: "external" },
        ],
        layout: "dropdown",
      },
    },
    {
      name: "type",
      title: "Type",
      type: "string",
      options: {
        list: [
          { title: "Solo", value: "solo" },
          { title: "Group", value: "group" },
        ],
        layout: "dropdown",
      },
    },

Your data values mismatch, they are not lowercased, compared to the list option values (
External
vs.
external
)
Right, I was also about to say: be mindful of capitalization. They are 2 different strings.
Ah 🙂 I see, thank you! I will make this change and try again.
AAAaaaand: love to see a tool that logs artist shows in my native Munich! Would you like to share your work with us? I would love to see what you’ve built!
Hey
user J
absolutely 🙂 Once I have finished writing a bit more content to the studio I would be happy to share. I’ll update you back in this thread in the next week or so. And hi from Berlin!
Hello back from Berlin ;)

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?